Why this code is returning undefined? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
24th Sep 2021, 4:23 AM
sue hlainge
2 Answers
+ 5
returns undefined because the variable you want to return is not recognized outside the for block. and your checkid function doesn't do what you really want, the for condition is always false, the num is incremented by strings. to fix this do this: function checkId(customerId) { var num = 0; for (let i = 0;i < customerId.length;i++){ num += parseInt(customerId[i]); }; return num; }
24th Sep 2021, 4:32 AM
Erlénio.RS
Erlénio.RS - avatar
+ 1
Erlenio.RS thanks a lot.Your explanation leads me to right direction.
25th Sep 2021, 7:52 AM
sue hlainge