Why my solution didn't work

ok I understand the challenge requirements completely and solved it but it didn’t work here is my solution

  // Only change code below this line
  for(let i = 0; i < this.length; i++){
    newArray.push(this[i])
    callback(newArray[i])
  }

  // Only change code above this line

challenge link

What does this line do?

What does this line do?

Think carefully about what you are pushing into the new array…

this is pushing the item from the array to the newArray

Right. But you don’t want the original item.

1 Like

yes I do not want the original items I want to apply the callback function I already applies it in the next line

callback(newArray[i])

But is that what is being pushed into the new array?

Right now that line says “call the callback function on the current array item and throw away the return value”.

1 Like

ok how can I edit the newArray using the callback

Why edit it? Why not put the thing that you compute there in the first place?

1 Like

it worked now for me

  for(let i = 0; i < this.length; i++){
    newArray.push(this[i])
    newArray[i] = callback(newArray[i]);
    
  }

so it did not work for me because the value from the callback function has not been assigned to the current array am I right

Yeah, you need to put the result of the callback into the new array. But

This says, in words
“Push onto the new array this old value that I don’t want.
Then replace the old value that I don’t want with the return value of the callback function.”

Why the extra step? You want the return value of the callback function in the new array, so just put that there instead of the extra step of putting the old value into the new array.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.