This commit is contained in:
Philippe Torrel
2026-09-18 09:52:05 +02:00
parent 968ebe05bd
commit 818f064f2e
2 changed files with 21 additions and 6 deletions
+7 -1
View File
@@ -24,7 +24,13 @@
const number = 4566;
const getDigitSum = (n) => {};
const getDigitSum = (n) => {
const s = n
.toString() // => '4242'
.split('') // => ['4','2','4','2']
.reduce((n1, n2) => Number(n1) + Number(n2), 0);
return s;
};
console.log(getDigitSum(4242)); //=> 12
console.log(getDigitSum('13')); //=> 4