Wiredcraft / test-backend

39 stars 76 forks source link

How to convert an Array of strings to number? Keeps returning NaN #56

Closed patrick-aguiar closed 3 years ago

patrick-aguiar commented 3 years ago

Hello guys, I'm trying to make my code multiple every second number by 2 starting from position 1 and sum every second number starting from position 0, but the code keeps returning NaN, I try to use parseInt() and Number() on arraySin but not working. The input of sin is 046454286 const sin = req.body.sin let arraySin = sin.toString().replace(/\s+/g, '').split('') // this return an array of strings [ '0', '4', '6', '4', '5', '4', '2', '8', '6' ] let resultSinSecondDigit let resultSin for(i = 1; i<9; ){ parseInt(arraySin[i], 10) resultSinSecondDigit += (arraySin[i], 10) * 2 i += 2 //result of the for should be 40 ( 8 + 8 + 8 + 16 ) } for(i = 0; i<9;){ resultSin += arraySin[i] i += 2 //result of the for should be 19 (0 + 6 + 5 + 2 + 6) } console.log(resultSin) console.log(resultSinSecondDigit)

Does somebody know what I did wrong? Thanks