Skip to content

Instantly share code, notes, and snippets.

@trykhov
Created September 16, 2024 08:29
Show Gist options
  • Save trykhov/2b61d680e9ef096ecc64528a15219c93 to your computer and use it in GitHub Desktop.
Save trykhov/2b61d680e9ef096ecc64528a15219c93 to your computer and use it in GitHub Desktop.
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