Trying to print off an array of objects in a function but keep getting an undefined

;

My Code and the output of my Code:


the link to my code

if it says return maybe your function needs to actually return something

Hello there.

You are console.log()-ing a function that has console.log() as its only use. So, the for loop runs the console.logs, and then this line console.logs what your function returns:

console.log(afterNYears({
  "Joel" : 32,
  "Fred" : 44,
  "Reginald" : 65,
  "Susan" : 33,
  "Julian" : 13
},1));

Hope this helps

when I change it to a return statement, it keeps given me only one value now instead of iterating through all of the values in the object.

"
function afterNYears(names, n) {
for(const property in names){
return {${property}: ${names[property]+Math.abs(n)}};

}

}
"

  1. The undefined in the image you have in the first post is the default return value that console.log always returns. You are logging the objects just fine. (edit, it might also be the function return, but console.log does always return undefined)

  2. In the new code you posted if you return, you return out of the loop and function so you just get one return value.

but the return statement is within the loop.

so the return statement should be out of the loop?

Am I closer to a new solution :

function afterNYears(names, n) {
let newObject={

};

for(const property in names){

newObject+=`{${property}: ${names[property]+Math.abs(n)}}`;

}
return newObject;

}

https://codepen.io/noblegas/pen/jOPeavE

So creating an empty object is the same as creating a string?