mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-04 15:39:42 +08:00
feat: Test running overhaul, switch to Prettier & reformat everything (#1407)
* chore: Switch to Node 20 + Vitest * chore: migrate to vitest mock functions * chore: code style (switch to prettier) * test: re-enable long-running test Seems the switch to Node 20 and Vitest has vastly improved the code's and / or the test's runtime! see #1193 * chore: code style * chore: fix failing tests * Updated Documentation in README.md * Update contribution guidelines to state usage of Prettier * fix: set prettier printWidth back to 80 * chore: apply updated code style automatically * fix: set prettier line endings to lf again * chore: apply updated code style automatically --------- Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Lars Müller <34514239+appgurueu@users.noreply.github.com>
This commit is contained in:
@ -9,17 +9,20 @@
|
||||
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
|
||||
*/
|
||||
function sumOfDigitsUsingString (number) {
|
||||
function sumOfDigitsUsingString(number) {
|
||||
if (number < 0) number = -number
|
||||
|
||||
return +(number.toString().split('').reduce((a, b) => (+a) + (+b)))
|
||||
return +number
|
||||
.toString()
|
||||
.split('')
|
||||
.reduce((a, b) => +a + +b)
|
||||
}
|
||||
|
||||
/*
|
||||
The input is divided by 10 in each iteration, till the input is equal to 0
|
||||
The sum of all the digits is returned (The res variable acts as a collector, taking the remainders on each iteration)
|
||||
*/
|
||||
function sumOfDigitsUsingLoop (number) {
|
||||
function sumOfDigitsUsingLoop(number) {
|
||||
if (number < 0) number = -number
|
||||
let res = 0
|
||||
|
||||
@ -34,7 +37,7 @@ function sumOfDigitsUsingLoop (number) {
|
||||
/*
|
||||
We use the fact that the sum of the digits of a one digit number is itself, and check whether the number is less than 10. If so, then we return the number. Else, we take the number divided by 10 and floored, and recursively call the function, while adding it with the number mod 10
|
||||
*/
|
||||
function sumOfDigitsUsingRecursion (number) {
|
||||
function sumOfDigitsUsingRecursion(number) {
|
||||
if (number < 0) number = -number
|
||||
|
||||
if (number < 10) return number
|
||||
@ -42,4 +45,8 @@ function sumOfDigitsUsingRecursion (number) {
|
||||
return (number % 10) + sumOfDigitsUsingRecursion(Math.floor(number / 10))
|
||||
}
|
||||
|
||||
export { sumOfDigitsUsingRecursion, sumOfDigitsUsingLoop, sumOfDigitsUsingString }
|
||||
export {
|
||||
sumOfDigitsUsingRecursion,
|
||||
sumOfDigitsUsingLoop,
|
||||
sumOfDigitsUsingString
|
||||
}
|
||||
|
Reference in New Issue
Block a user