Update: Added Unary Operator in SumOfDigits algorithm (#1348)

* Update: Added Unary Operator in SumOfDigits algorithm

* Update: Added Unary Operator in SumOfDigits algorithm
This commit is contained in:
Chetan Nada
2023-08-15 01:24:00 +05:30
committed by GitHub
parent 53b1f667ee
commit 9b32db29d8

View File

@ -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)))
}
/*