I just need help with a simple problem in JS.
I get a number from the user and i want to calculate the sum of its digits. here’s the code:
let num = Number(prompt("Enter a number to see the sum of its digits:"));
if (isNaN(num)) {
alert("please enter a number");
} else {
let sum = 0;
for (let i = 0; i < num.length; i++) {
sum = sum + num[i];
}
console.log(sum);
alert("The result is : " + sum);
}
Thank U, as a beginner.
the program resulted in 0 number as the sum of digits and it’s, it’s just not working
New contributor
1