Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Maths/SumOfDigits.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@
/*
The given input is converted to a string, split into an array of characters.
This array is reduced to a number using the method <Array>.reduce
NOTE: The final parseInt is just there in cases where 1 digit numbers are given, since without that it would result in a String output.
*/
function sumOfDigitsUsingString (number) {
if (number < 0) number = -number

return Number.parseInt(number.toString().split('').reduce((a, b) => Number(a) + Number(b)))
return +(number.toString().split('').reduce((a, b) => (+a) + (+b)))
}

/*
Expand Down