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.

Exactly. When a return statement is executed, it exits the current function in which it is contained.

so the return statement should be out of the loop?

There should be a return statement outside of your for loop, but it will not be returning the value you are currently showing in your codpen link. Your final returned value should be an object.

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

Why are you creating a string? You are supposed to be returning an object.

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

No. Why would you think so? According to the instructions and examples, the function is supposed to return an object with the same property names as the object passed to the function. The only thing that should change is the value of each property, which should be increased by the amount specified by the second argument (n) passed to the function.