Number of keys in object?

Number of keys in object?
0

#1

This is a tricky one for me:
Instructions:

Write a function named growingKeys that takes a number and a string and creates that many number of new keys according to the following example. The values for all of the keys will just be true.

Example:

If you pass 2,"d" it should return {"d": true, "dd": true}

My code:

function growingKeys(num, str) {
    let obj = {};
    
    for(let i = 0; i <= num; i++) {
        let letter = str.repeat(i);
        obj[letter] = true;
    }
    
    return obj;
};

EDIT: Nevermind, solved:

function growingKeys(num, str) {
    let obj = {};
    let emptyStr = '';
    
    for (let i = 0; i < num; i++){
        if (num === 1){
            obj[str[i]] = true
        } else {
            emptyStr += str;
            obj[emptyStr] = true
        }
    }
    
    
    return obj;
}

#2
const growingKeys = (num, str) => {
  const obj = {};
  for (let count = 1; count <= num;) {
    obj[str.repeat(count++)] = true;
  }
  return obj;
};

OR a more functional approach (though maybe not as readable)

const growingKeys = (num, str) => Array(num).fill(true)
  .reduce((obj, val, idx) => (obj[str.repeat(idx + 1)] = val, obj), {});