0

I am trying to convert string to number but I am unable to do that , after passing each number from foreach

function sum(num){ num.toString().split('').forEach(add); } function add(value , key) { console.log(value); // 2,3 Number(value); console.log(typeof(value)) // string string } sum(23); 

In the code - >

After I did Number(value); in function add(value, key) I am still getting string console.log(typeof(value)) // string

3
  • 1
    What problem you are facing with this code? Commented Jun 22, 2019 at 10:25
  • 2
    You should do var n = Number(value); console.log(typeof(n)); Commented Jun 22, 2019 at 10:28
  • check this const crossSum = num => String(num).split("").reduce((a,b) => a+Number(b), 0); Commented Jun 22, 2019 at 14:29

1 Answer 1

2

You're not assigning it to a new variable / reassigning the variable:

function sum(num){ num.toString().split('').forEach(add); } function add(value , key) { console.log( { value } ); const newValue = Number(value); // or alternatively value = Number(value); console.log( typeof newValue ); } sum(23);

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.