Question

In: Computer Science

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".

Code:
function reverse_kvs(store) {
    var reverse_store = {};
    for (var key in store) {
        if (store.hasOwnProperty(key)) {
            reverse_store[store[key]] = key;
        }
    }
    return reverse_store;
}

I need to have this code return an output other than an empty result. Does anyone know how to fix the code?

*** UPDATE: The code is not correct. It returns an empty result, such as this "{}"***

Solutions

Expert Solution

Solution for the given question are as follows -

Note : I am using online java script compiler to run java script code. ( https://rextester.com/l/js_online_compiler )

Code:

function reverse_kvs(store) {
var reverse_store = {};
for (var key in store) {
if (store.hasOwnProperty(key)) {
reverse_store[store[key]] = key;
}
}
return reverse_store;
}
result = reverse_kvs({"name":"vikram"});
print("Result = " + JSON.stringify(result))

Code Screen Shot :

Output :


Related Solutions

Write a function named "characters" that takes a string as a parameter and returns the number...
Write a function named "characters" that takes a string as a parameter and returns the number of characters in the input string
Define a JavaScript function named showBins which has one parameter which is a JSON blob (JSON...
Define a JavaScript function named showBins which has one parameter which is a JSON blob (JSON encoding). The parameter encodes an array of Numbers. Your function should display the value at index 0 of the array in a text element whose id is "small", display the value at index 1 of the array in a text element whose id is "med", and display the value at index 2 of the array in a text element whose id is "large".
Define a function in Javascript named secondHighest which accepts an array of numbers, and returns the...
Define a function in Javascript named secondHighest which accepts an array of numbers, and returns the second highest number from the array. If the highest value occurs more than once, then it is also the second highest (see example). Assume the input array will always have length at least two (so there is always a second highest value). The original array must NOT be modified by this function. Example secondHighest([5,3,8,6,2,7,4]) must return 7, and secondHighest([5,3,8,6,2,7,8]) must return 8. I have...
6.15 LAB: JavaScript arrays Write the function divideArray() in script.js that has a single numbers parameter...
6.15 LAB: JavaScript arrays Write the function divideArray() in script.js that has a single numbers parameter containing an array of integers. The function should divide numbers into two arrays, evenNums for even numbers and oddNums for odd numbers. Then the function should sort the two arrays and output the array values to the console. Ex: The function call: var nums = [4, 2, 9, 1, 8]; divideArray(nums); produces the console output: Even numbers: 2 4 8 Odd numbers: 1 9...
Create a function named ‘dividing’. This function will take one parameter value, which will be an...
Create a function named ‘dividing’. This function will take one parameter value, which will be an integer. For the function you will use ‘Exception Handling’ with the use of the try and except statements to try and return the results of a number divided by the parameter value given to the function. If the parameter passed to the function is the integer value of zero then your function should return ‘You tried to divide by zero!’ through the use of...
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
write an algorithm function called balance() in javascript that takes in a string and returns a...
write an algorithm function called balance() in javascript that takes in a string and returns a strinf with balanced parantheses only. the string should be able to contain only parantheses, numbers, and letters. include comments of each portion of your code balance(“()()”) should return “()()” balance(“())”) should return “()” balance(“a(b)c”) should return “a(b)c” balance(“a(b)c())”) should return “a(b)c()”
(Javascript) Modify the JavaScript file to implement a function named calculateTotalPrice. At the bottom of the...
(Javascript) Modify the JavaScript file to implement a function named calculateTotalPrice. At the bottom of the file are sample inputs and outputs to test your implementation. /* * The price per ticket depends on the number of tickets * purchased, there are 4 ticket pricing tiers. Given the * number of tickets return the total price, formatted to * exactly two decimal places with a leading dollar sign. * Tier 1: *   Minimum number of tickets: 1 *   Price per...
Write a GLM function named getInverse that returns the inverse of A. If A does not...
Write a GLM function named getInverse that returns the inverse of A. If A does not have an inverse, the function returns the identify matrix. Assume A is 3x3.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT