Created
October 20, 2020 02:54
-
-
Save forrestblade/0cfb907e51cbcd4852f1b0f49670c98f to your computer and use it in GitHub Desktop.
no carry
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
const noCarry = (val1, val2) => { | |
val1 = val1.toString().split('').map(x => parseInt(x)) | |
val2 = val2.toString().split('').map(x => parseInt(x)) | |
const f = (arr1, arr2) => { | |
const a = arr1.length > arr2.length ? arr1 : arr2; | |
const b = arr1.length > arr2.length ? arr2 : arr1; | |
let result = [] | |
a.reverse().forEach((e, i) => { | |
const aux = b.length - 1; | |
result[i] = e + (b[aux - i] ? b[aux - i] : 0); | |
}) | |
return result.reverse() | |
} | |
const solution = f(val1, val2).map(x => x.toString()).reduce((sum, val) => sum + +val) | |
return solution | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment