Created
September 16, 2024 08:29
-
-
Save trykhov/2b61d680e9ef096ecc64528a15219c93 to your computer and use it in GitHub Desktop.
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 triangularSum(nums) { | |
// duplicate the input array, nums | |
let newNums = new Array(nums.length); | |
for(let i = 0; i < nums.length; i++) { | |
newNums[i] = nums[i]; | |
} | |
// while loop ensures the process continues until newNums.length == 1 | |
while(newNums.length > 1) { | |
// create a dummy array | |
// this will store the sums of the previous array | |
let newArr = new Array(newNums.length - 1); | |
// loop through newArr and fill each index | |
for(let i = 0; i < newNums.length - 1; i++) { | |
newArr[i] = (newNums[i] + newNums[i + 1]) % 10; | |
} | |
// replace newNums with the result of newArr | |
newNums = newArr; | |
} | |
// return the triangular sum | |
return newNums[newNums.length - 1]; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment