Javascript algorithm problem | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Javascript algorithm problem

https://code.sololearn.com/WrCqVk6emfXQ/?ref=app Why arr variable not store the add up values?

18th May 2020, 8:58 PM
Mr Robot
Mr Robot - avatar
5 Answers
+ 7
Saad Mughal ahh, ok. Only replace in line 4: let arr; Instead let arr=0; Cause' when you declarate let arr; arr has "undefined" value, so if you try arr+=2; (undefined+2) you get NaN
18th May 2020, 9:17 PM
CoffeeByte
CoffeeByte - avatar
+ 2
Try this: function oddOrEven(array) { return array.map( num => num % 2 === 0?"even":"odd"); } console.log(oddOrEven([0,5,9])); //Output will be //even,odd, odd
18th May 2020, 9:06 PM
CoffeeByte
CoffeeByte - avatar
+ 1
luis calderón Thanks it's working now
18th May 2020, 9:26 PM
Mr Robot
Mr Robot - avatar
0
I want To sum all the Elements in array and find there sum is even or odd And I just want To know in my logic what is the problem
18th May 2020, 9:11 PM
Mr Robot
Mr Robot - avatar
0
Intialize arr with 0; When you do arr+=array[i] You are basically adding arr=arr+array[i] While arr has no value and it is returning NaN um I didn't noticed that Luis already gave an answer ,really slow net
18th May 2020, 9:34 PM
Abhay
Abhay - avatar