Created
October 12, 2022 20:25
-
-
Save lbland94/4998d9d4e9e3aca0608dff38cba7cb92 to your computer and use it in GitHub Desktop.
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
| function incrementInt(num) { | |
| switch(+num) { | |
| case 0: | |
| return [1, 0]; | |
| case 1: | |
| return [2, 0]; | |
| case 2: | |
| return [3, 0]; | |
| case 3: | |
| return [4, 0]; | |
| case 4: | |
| return [5, 0]; | |
| case 5: | |
| return [6, 0]; | |
| case 6: | |
| return [7, 0]; | |
| case 7: | |
| return [8, 0]; | |
| case 8: | |
| return [9, 0]; | |
| case 9: | |
| return [0, 1]; | |
| } | |
| } | |
| function decrementInt(num) { | |
| switch(+num) { | |
| case 0: | |
| return [9, 1]; | |
| case 1: | |
| return [0, 0]; | |
| case 2: | |
| return [1, 0]; | |
| case 3: | |
| return [2, 0]; | |
| case 4: | |
| return [3, 0]; | |
| case 5: | |
| return [4, 0]; | |
| case 6: | |
| return [5, 0]; | |
| case 7: | |
| return [6, 0]; | |
| case 8: | |
| return [7, 0]; | |
| case 9: | |
| return [8, 0]; | |
| } | |
| } | |
| function increment(numInt, total = []) { | |
| const num = String(numInt).split(''); | |
| if (num.length === 0) return combine(total); | |
| const inc = incrementInt(num.at(-1)); | |
| if (!inc[1]) return combine([...num.slice(0, -1), inc[0], ...total]); | |
| else { | |
| if (num.slice(0, -1).length === 0) { | |
| return combine([String(increment(combine([0]), [...total, inc[0]]))]); | |
| } | |
| return combine([String(increment(combine(num.slice(0, -1)), [...total, inc[0]]))]); | |
| } | |
| } | |
| function decrement(numInt, total = []) { | |
| const num = String(numInt).split(''); | |
| if (num.length === 0) return combine(total); | |
| const dec = decrementInt(num.at(-1)); | |
| if (!dec[1]) return combine([...num.slice(0, -1), dec[0], ...total]); | |
| else { | |
| if (num.slice(0, -1).length === 0) { | |
| return combine([String(decrement(combine([]), [...total, dec[0]]))]); | |
| } | |
| return combine([String(decrement(combine(num.slice(0, -1)), [...total, dec[0]]))]); | |
| } | |
| } | |
| function combine(numArr) { | |
| return +numArr.join(''); | |
| } | |
| function add(num1, num2) { | |
| if (num2 == 0) return num1; | |
| return add(increment(num1), decrement(num2)); | |
| } | |
| function subtract(num1, num2, neg = false) { | |
| if (num2 == 0) { | |
| if (neg) { | |
| return -num1; | |
| } | |
| return num1; | |
| }; | |
| if (num1 == 0) neg = true; | |
| return subtract(!neg ? decrement(num1) : increment(num1), decrement(num2), neg); | |
| } | |
| function multiply(num1, num2, acc = '') { | |
| if (num2 == 0) { | |
| return acc; | |
| } | |
| return multiply(num1, decrement(num2), add(num1, acc)); | |
| } | |
| (() => { | |
| const m1 = process.argv[2]; | |
| const m2 = process.argv[3]; | |
| let neg = false; | |
| if (m1 == 0 || m2 == 0) { | |
| console.log(0); | |
| return; | |
| } | |
| if (m1 == 1) { | |
| console.log(m2); | |
| return; | |
| } | |
| if (m2 == 1) { | |
| console.log(m1); | |
| return; | |
| } | |
| if ((m1 < 0 && m2 > 0) || (m2 < 0 && m1 > 0)) { | |
| neg = true; | |
| } | |
| console.log(`${neg ? '-' : ''}${multiply(Math.abs(m1), Math.abs(m2))}`); | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment