function addStrNums(num1, num2) {
// write your code here
let parsed1 = parseInt(num1);
console.log(parsed1)
let parsed2 = parseInt(num2);
console.log(parsed2)
if (isNaN(parsed1) || isNaN(parsed2) ) { return -1; }
return parsed1 + parsed2;
}
console.log(addStrNums('2k', 'd3d'))
// 2 NaN why '2k' is an integer here and in the second is a NaN?
console.log(addStrNums('k2', '3d'))
//NaN 3
I don’t understand what you are asking. Can you please explain more?
ty for editing my post and sorry for the mess! what am asking about is what make the inputs in the first line considered as NaN and in the second not considered as NaN ; “2k” in the first is converted to an integer but in the second line is NaN
Look at what parseInt
does with a string that starts with a number:
1 Like
ty this is exactly what i need to read to understand this topic!
1 Like
There are some confusing little corners like this all over Javascript. MDN does a pretty good job providing examples and discussion.
1 Like
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.