-1

I tried a code to check the postfix increment unary operator and i want the incrementation to be applied to all of the array content. the problem is that when i do so the incrementation is not applied to all of the variables inside the array.

i tried to put the ++ unary operator behiend the NewArray

 var array = ["2","mina",false,true,1.1,{Age:28}];
 var i = 0;
 var NewArray = "";
 var text = ""
 while(i < array.length){
     NewArray = array[i];
     text = NewArray++
     console.log(text);
     i++;
 }

I expected the results to be : "3 , NaN ,0, 1 , 2.1 , NaN"

the actual results is : "2 , NaN , 0 , 1 , 1.1 , NaN"

  • `x++` returns the value before incrementing whereas `++x` returns the value after incrementing - your expectations match `++NewArray` – ic3b3rg Dec 28 '18 at 20:31
  • You need to use `++NewArray` instead of `NewArray++`, this post explains this in detail (https://stackoverflow.com/questions/3469885/somevariable-vs-somevariable-in-javascript) – Ryan Wilson Dec 28 '18 at 20:31
  • There's no reason to use auto-increment at all, since you don't use the updated `NewArray`. Just write `text = NewArray + 1` or `text = array[i] + 1`. – Barmar Dec 28 '18 at 20:36

1 Answers1

0

You can do this. Hope that help you

var array = [2,"mina",false,true,1.1,{Age:28}];
 var i = 0;
 var text;
 var NewArray;
 while(i < array.length){
     NewArray = array[i];
     //console.log(NewArray);
     text = NewArray == true || NewArray == false ? parseFloat(++NewArray - 1): parseFloat(++NewArray);
     console.log(text);
     i++;
 }
soumare
  • 409
  • 5
  • 9