This question already has an answer here:
- Strange javascript addition problem 2 answers
I am working in a redux function where i want for each element in array the sum with n number
This is the code
let neWd = array.map(x => {
if (x === 'M' || x === 'L'){
return x;
}else{
return x + 5;
}
}).join(' ')
At the moment return x + 5
is adding the 5
number to any element of the array but not the sum.
How can i achieve it?
Assuming you have a string and it is splitted with
' '
and then you get each element as string. You need to cast it to a number, in this example with an unary+
for incrementing with5
.This proposal splits on white space, which can be more than one characters long.