Invert values function

Hello FCC campers, need some help. i want to inverse values into an array exp :
invert([1,2,3,4,5]) = [-1,-2,-3,-4,-5] and that’s what i did :

function invert(array) {
  for(let i = 0 ; i < array.length; i++){
    array[i] = array[i] * (-1) 
  }
  return array
}

Do you have a question? It seems to do what you want it to do.

2 Likes

I do find it odd that you’re mutating the array and returning the mutated original. I would either mutate it or return a new array, not both.

2 Likes

hahaha it seems but it doesn’t ! The thing is that i didn’t work on JS for about 2 months so i forgot some things ! that’s why am struggling even while try what you’re suggesting to me thank you for the help ! :smiley:

I don’t understand what you mean when you say it doesn’t. When I run this code:

function invert(array) {
  for(let i = 0 ; i < array.length; i++){
    array[i] = array[i] * (-1) 
  }
  return array
}

console.log(invert([1, -2, 3]))

I get this in the console:

[-1, 2, -3]

In what way doesn’t it work?

1 Like

Ah ! then there was a bug with codewars :sweat_smile: i feel stupid for not verifying multiple times, even thank you so much for your help !!!