-1

Write a function named "indexed_kvs" that doesn't take any parameters and returns a new key-value store containing the integers from 0 to 47 as values each stored at a key which is a string containing the digits of the integer. For example the key-value "0":0 will be in your returned key-value store (include both 0 and 47 in your list) (My code below)

function indexed_kvs(){
    var d = (dict = []);
    for (var i of Array(47).keys()) {
        d = dict.push(i);
    }
    return d;
}

I keep on returning the input 47, instead of the keys and values ranging from 0 to 47. How can I fix this?

2

2 Answers 2

0

This should work for you.

function makeKeys() {
    var d = {};
    for (var i = 0; i < 48; i++) {
      d[i] = i;
    }
    return d;
}

console.log(makeKeys())

Sign up to request clarification or add additional context in comments.

1 Comment

A good answer should explain what he did wrong and how your solution fixes it.
0

Just use a simple while loop and iterate from the end or use a for loop.

function indexed_kvs() {
    var object = {},
        i = 48;

    while (i--) object[i] = i;
    return object;
}

console.log(indexed_kvs());

A shorter approach by generating an array and then create an object of the array.

function indexed_kvs() {
    return Object.assign({}, [...Array(48).keys()]);
}

console.log(indexed_kvs());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.