In the following tasks, some programmer has written code and may have made mistakes in it. You should check if the code does what is described. If the code does not work correctly, you need to fix the errors.
The code should find the sum of the digits of a number:
let num = 12345;
let arr = num.split('');
let sum = 0;
for (let digit of arr) {
sum += digit;
}
console.log(sum);
The code should find the sum of the digits of a number:
let num = 12345;
let arr = String(num).split('');
let sum = 0;
for (let digit of arr) {
sum += digit;
}
console.log(sum);
The code should find the sum of the digits of a number:
let num = 12345;
let arr = String(num).split('');
let sum = 0;
for (let digit of arr) {
sum = Number(digit);
}
console.log(sum); // for some reason it prints 5 and not 15
The code should find the sum of the digits of a number:
let num = 12345;
let arr = String(num).split('');
let sum = '';
for (let digit of arr) {
sum += Number(digit);
}
console.log(sum);
The code should find the product of the digits of a number:
let num = 12345;
let arr = String(num).split('');
let prod = 0;
for (let digit of arr) {
prod *= digit;
}
console.log(prod);