Question

JavaScript Write a function named "reverse_kvs" that has a key-value store as a parameter and returns...

JavaScript

Write a function named "reverse_kvs" that has a key-value store as a parameter and returns a new key-value store. Your function should add each key-value pair in the parameter to the new key-value store EXCEPT reversing the key and value. For example, if the key-value "extreme":45 were in the parameter then the returned key-value store should contain the key-value pair 45:"extreme".

The following code doesn't work:

function reverse_kvs(d) {
    var new_d = {};
    for (var k in d) {
        new_d[d[k]] = k;
    }
    return new_d;
}

Error message:

returned: {}

expected: {33: 'particle', 75: 'fear', -36: 'champion'}

Homework Answers

Answer #1

const d = {"particle":33, "fear":75, "champion":-33}
       function reverse_kvs(d)
       {
           const keys = Object.keys(d);
           const new_d = {};
           for (const key of keys) {
              new_d[d[key]] = key;
           }
           return new_d;
       }
       console.log(reverse_kvs(d));

If you have any doubts please comment and please don't dislike.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
JavaScript Write a function named "reverse_kvs" that has a key-value store as a parameter and returns...
JavaScript Write a function named "reverse_kvs" that has a key-value store as a parameter and returns a new key-value store. Your function should add each key-value pair in the parameter to the new key-value store EXCEPT reversing the key and value. For example, if the key-value "extreme":45 were in the parameter then the returned key-value store should contain the key-value pair 45:"extreme".
Need HTML and JS code. Write a Javascript function named RandomString(randomStringLength) that returns a string filled...
Need HTML and JS code. Write a Javascript function named RandomString(randomStringLength) that returns a string filled with random characters where the length of the random string is defined by the input parameter. HINT: Use a for-loop, Math.random, and the fact that the alphabet has 26 characters (remembering that we can have both lower-case and upper-case characters in a string). This is what I have: <html> <!-- inputbox.html Jerry Davis --> <!-- Web page that sets the correct letter value. -->...
javascript 1.Write a function delay that accepts two arguments, a callback and the wait time in...
javascript 1.Write a function delay that accepts two arguments, a callback and the wait time in milliseconds. Delay should return a function that, when invoked waits for the specified amount of time before executing. HINT - research setTimeout(); 2.Create a function saveOutput that accepts a function (that will accept one argument), and a string (that will act as a password). saveOutput will then return a function that behaves exactly like the passed-in function, except for when the password string is...
In the previous assessment, you used a static set of named variables to store the data...
In the previous assessment, you used a static set of named variables to store the data that was entered in a form to be output in a message. For this assessment, you will use the invitation.html file that you modified in the previous assessment to create a more effective process for entering information and creating invitations for volunteers. Rather than having to enter each volunteer and create an invitation one at a time, you will create a script that will...
6.12 LAB: Grade distribution In this lab, you will write a JavaScript program that generates a...
6.12 LAB: Grade distribution In this lab, you will write a JavaScript program that generates a bar graph of letter grades from a distributions of scores. Implement the parseScores function (1 point) Implement the parseScores function to take a space-separated string of scores as an argument and return an array of score strings. Each score is a number in the range [0, 100]. Ex: "45 78 98 83 86 99 90 59" → ["45","78","98","83","86","99","59"] Hint: JavaScript's string split() function can...
Please create a python module named homework.py and implement the functions outlined below. Below you will...
Please create a python module named homework.py and implement the functions outlined below. Below you will find an explanation for each function you need to implement. When you are done please upload the file homework.py to Grader Than. Please get started as soon as possible on this assignment. This assignment has many problems, it may take you longer than normal to complete this assignment. This assignment is supposed to test you on your understanding of reading and writing to a...
Write in C++. Define a class called Text whose objects store lists of words. The class...
Write in C++. Define a class called Text whose objects store lists of words. The class Text will be just like the class StringVar except that the class Text will use a dy- namic array with base type StringVar rather than base type char and will mark the end of the array with a StringVar object consisting of a single blank, rather than using '\0' as the end marker. Intuitively, an object of the class Text represents some text consisting...
**[70 pts]** You will be writing a (rather primitive) online store simulator. It will have these...
**[70 pts]** You will be writing a (rather primitive) online store simulator. It will have these classes: Product, Customer, and Store. All data members of each class should be marked as **private** (a leading underscore in the name). Since they're private, if you need to access them from outside the class, you should do so via get or set methods. Any get or set methods should be named per the usual convention ("get_" or "set_" followed by the name of...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
Complete a Java program named ARMgr that maintains customer accounts receivable in a database. The code...
Complete a Java program named ARMgr that maintains customer accounts receivable in a database. The code to initialize the CustomerAccountsDB database table and add a set of customer accounts is provided. Finish the code in these 3 methods in CustomerAccountDB.java to update or query the database: -purchase(double amountOfPurchase) -payment(double amountOfPayment) -getCustomerName() Hint: For getCustomerName(), look at the getAccountBalance() method to see an example of querying data from the database. For the purchase() and payment() methods, look at the addCustomerAccount() method...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT