Finding errors in code with standard methods in JavaScript
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.
Задание 1
The code should find the sum of the digits of a number:
javascriptlet num = 12345;
let arr = num.split('');
let sum = 0;
for (let digit of arr) {
sum += digit;
}
console.log(sum);
Задание 2
The code should find the sum of the digits of a number:
javascriptlet num = 12345;
let arr = String(num).split('');
let sum = 0;
for (let digit of arr) {
sum += digit;
}
console.log(sum);
Задание 3
The code should find the sum of the digits of a number:
javascriptlet 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
Задание 4
The code should find the sum of the digits of a number:
javascriptlet num = 12345;
let arr = String(num).split('');
let sum = '';
for (let digit of arr) {
sum += Number(digit);
}
console.log(sum);
Задание 5
The code should find the product of the digits of a number:
javascriptlet num = 12345;
let arr = String(num).split('');
let prod = 0;
for (let digit of arr) {
prod *= digit;
}
console.log(prod);