Skip to content

Commit

Permalink
Add solution to day 26
Browse files Browse the repository at this point in the history
  • Loading branch information
AhmedGamal2212 committed Apr 26, 2023
1 parent 5c128df commit adaaf8b
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions 04- April/26- Add Digits/26- Add Digits (Ahmed Gamal).js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Autor: Ahmed Gamal

// the idea for this solution is so simple, we will loop until the number is less than 10
// in each iteration we will calculate the sum of the digits of the number

/**
* @param {number} num
* @return {number}
*/
var addDigits = function(num) {
// loop until the number is less than 10
while(num > 10) {
// calculate the sum of the digits of the number
// x: copy of the number to not change the original number
// sum: the sum of the digits of the number

// loop until the number is 0
// in each iteration we will add the last digit to the sum
// and remove the last digit from the number

let x = num, sum = 0;
while(x) {
sum += x % 10;
x = Math.floor(x / 10);
}

// update the number with the sum
num = sum;
}

// return the number
return num;
};

0 comments on commit adaaf8b

Please sign in to comment.