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
@@ -24,15 +24,24 @@
const numbers = [99, 5, 8, 12, 111, 123];
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;
};
const getSortedArrayByDigitSum = (numbers) => {};
const getSortedArrayByDigitSum = (numbers) => {
return [...numbers].sort((a, b) => {
return getDigitSum(a) - getDigitSum(b);
});
};
console.log('numbers: ', numbers);
console.log(getSortedArrayByDigitSum([4, 12])); //=> [12, 4]
console.log(getSortedArrayByDigitSum(numbers)); //=> [12, 111, 5, 123, 8, 99]
console.log(numbers);
</script>
</body>
</html>