-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay11.js
More file actions
31 lines (21 loc) · 728 Bytes
/
Day11.js
File metadata and controls
31 lines (21 loc) · 728 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// 11: Write a function that takes a number as input and returns the sum of its digits.
const sumOfDigits = (num) =>{
let sum = 0;
while(num!=0)
{
let digit = num%10;
sum = sum + digit;
num = Math.floor(num /10);
}
return sum;
}
// Example usage:
console.log(sumOfDigits(1234)); // Output: 10
console.log(sumOfDigits(4321)); // Output: 10
console.log(sumOfDigits(123456)); // Output: 21
// Constraints:
// - The input number will always be a positive integer.
// - The input number can have multiple digits.
// - The output should be the sum of all the digits in the input number.
// Homework:
// This function calculates the sum of digits without converting the number to a string.