How to resolve the algorithm Sum digits of an integer step by step in the JavaScript programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Sum digits of an integer step by step in the JavaScript programming language
Table of Contents
Problem Statement
Take a Natural Number in a given base and return the sum of its digits:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Sum digits of an integer step by step in the JavaScript programming language
-
First implementation:
- The
sumDigits
function takes a numbern
as an argument. - It converts
n
to a string and iterates over its characters. - For each character, it converts it to an integer using
parseInt(n.charAt(i), 36)
and adds it to the sums
. - Finally, it returns the sum of all the digits.
- The
-
Second implementation:
- The
digitsSummed
function is a curried function that takes a number as an argument and returns a function that takes a base as an argument. - The function that is returned converts the number to a string, splits it into an array of characters, and iterates over the characters.
- For each character, it converts it to an integer using
parseInt(digit, base)
and adds it to the suma
. - Finally, it returns the sum of all the digits.
- The
-
Third implementation:
- The
digitsSummed
function is a curried function that takes a number as an argument and returns a function that takes a base as an argument. - The function that is returned converts the number to a string, splits it into an array of characters, and iterates over the characters.
- For each character, it converts it to an integer using
parseInt(digit, base)
and adds it to the sumsofar
. - Finally, it returns the sum of all the digits.
- The
Usage:
All three implementations of the digitsSummed
function can be used in the same way. For example, to sum the digits of the number 12345, you would call the function as follows:
digitsSummed(12345)
This would return the value 15.
Source code in the javascript programming language
function sumDigits(n) {
n += ''
for (var s=0, i=0, e=n.length; i<e; i+=1) s+=parseInt(n.charAt(i),36)
return s
}
for (var n of [1, 12345, 0xfe, 'fe', 'f0e', '999ABCXYZ']) document.write(n, ' sum to ', sumDigits(n), '<br>')
(function () {
'use strict';
// digitsSummed :: (Int | String) -> Int
function digitsSummed(number) {
// 10 digits + 26 alphabetics
// give us glyphs for up to base 36
var intMaxBase = 36;
return number
.toString()
.split('')
.reduce(function (a, digit) {
return a + parseInt(digit, intMaxBase);
}, 0);
}
// TEST
return [1, 12345, 0xfe, 'fe', 'f0e', '999ABCXYZ']
.map(function (x) {
return x + ' -> ' + digitsSummed(x);
})
.join('\n');
})();
(() => {
"use strict";
// -------------- INTEGER DIGITS SUMMED --------------
// digitsSummed :: (Int | String) -> Int
const digitsSummed = number => {
// 10 digits + 26 alphabetics
// give us glyphs for up to base 36
const intMaxBase = 36;
return `${number}`
.split("")
.reduce(
(sofar, digit) => sofar + parseInt(
digit, intMaxBase
),
0
);
};
// ---------------------- TEST -----------------------
return [1, 12345, 0xfe, "fe", "f0e", "999ABCXYZ"]
.map((x) => `${x} -> ${digitsSummed(x)}`)
.join("\n");
})();
You may also check:How to resolve the algorithm Enumerations step by step in the JScript.NET programming language
You may also check:How to resolve the algorithm 24 game step by step in the Ada programming language
You may also check:How to resolve the algorithm Forest fire step by step in the Tcl programming language
You may also check:How to resolve the algorithm Determine if a string is collapsible step by step in the APL programming language
You may also check:How to resolve the algorithm Sudoku step by step in the BCPL programming language