Skip to content

Commit 9b32db2

Browse files
authored
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy