Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9b32db2

Browse files
Update: Added Unary Operator in SumOfDigits algorithm (TheAlgorithms#1348)
* Update: Added Unary Operator in SumOfDigits algorithm * Update: Added Unary Operator in SumOfDigits algorithm
1 parent 53b1f66 commit 9b32db2

File tree

1 file changed

+1
-2
lines changed

1 file changed

+1
-2
lines changed

‎Maths/SumOfDigits.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@
88
/*
99
The given input is converted to a string, split into an array of characters.
1010
This array is reduced to a number using the method <Array>.reduce
11-
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.
1211
*/
1312
function sumOfDigitsUsingString (number) {
1413
if (number < 0) number = -number
1514

16-
return Number.parseInt(number.toString().split('').reduce((a, b) => Number(a) + Number(b)))
15+
return +(number.toString().split('').reduce((a, b) => (+a) + (+b)))
1716
}
1817

1918
/*

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /