Created
December 28, 2017 01:56
-
-
Save jlarocque/aa48f5e702e7c8bb0bf5cfbb9ef83de5 to your computer and use it in GitHub Desktop.
Sum of Digits (modulo)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src='https://scottdalessandro.github.io/jasmine-total/js/jasmine-suite.min.js'></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Sum Of Digits: | |
Create the function **sumOfDigits** that adds individual digits of a number, and returns the sum. | |
<br> | |
####Examples: | |
```js | |
- INPUT: sumOfDigits(414); | |
- OUTPUT: 9 | |
- INPUT: sumOfDigits(2913); | |
- OUTPUT: 15 | |
``` | |
<br> | |
####Tip: | |
You may need to use the following methods or operators in your solution: | |
- [Math.floor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor) - Rounds any floating point number (decimal) down to closest integer value (non-decimal number). For example: | |
>> ```js | |
>> Math.floor(5.99) ==> 5 | |
>> Math.floor(10.2) ==> 10 | |
>> ``` | |
*/ | |
// Write Code Below: | |
function sumOfDigits(num){ | |
var sum = 0; | |
while (num > 0){ | |
sum += num % 10; | |
num = Math.floor(num/10); | |
} | |
return sum; | |
} | |
describe('sumOfDigits', function(){ | |
it('sumOfDigits is a function', function(){ | |
expect(typeof sumOfDigits).toEqual('function'); | |
}); | |
it('sumOfDigits returns a number value', function(){ | |
expect(typeof sumOfDigits(201)).toEqual('number'); | |
}); | |
it('adds each individual digit of the number argument and returns the sum', function(){ | |
expect(sumOfDigits(212)).toEqual(5); | |
expect(sumOfDigits(2913)).toEqual(15); | |
expect(sumOfDigits(568912)).toEqual(31); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment