Created
April 1, 2024 15:21
-
-
Save lndgalante/2743fda27b8a14ead1bb753c5ae287a7 to your computer and use it in GitHub Desktop.
Given an array of numbers, add all of the values together but only if the number doesn't repeat a digit.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function hasRepeatedDigits(value: number): boolean { | |
const stringValue = String(value); | |
return new Set(stringValue).size !== stringValue.length; | |
} | |
function uniqueSum(values: number[]): number { | |
return values.reduce((accumulator, value) => hasRepeatedDigits(value) ? accumulator : accumulator + value, 0) | |
} | |
console.log(uniqueSum([1, 2, 3])) // 6 | |
console.log(uniqueSum([11, 22, 33])) // 0 | |
console.log(uniqueSum([101, 2, 3])) // 5 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment