Created
February 12, 2025 17:55
-
-
Save jamescurran/426ad0460bcdca864c693d2cd416460a to your computer and use it in GitHub Desktop.
Daily Challenge: JS-96 Sum Digit Sequence
This file contains hidden or 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
| /* The Challenge | |
| Difficulty: Easy | |
| Topic: Array manipulation | |
| Description | |
| Given an array of numbers, calculate the sum of digits for each number | |
| and return a new array containing these sums in the same order. | |
| */ | |
| void Main() | |
| { | |
| int[] array = { 123, 456, 789}; | |
| array.Select(x => GetDigits(x).Sum()).ToArray().Dump(); | |
| } | |
| IEnumerable<int> GetDigits(int num) | |
| { | |
| while(num > 0) | |
| { | |
| yield return num % 10; | |
| num = num / 10; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment