Last active
July 29, 2022 20:56
-
-
Save asfo/86647232bd7592cdc5e1164ef6643fcf 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
/** | |
* This code is stored to save my exam to Wizeline that I took for fun, sorry Wizeline if someone finds this... | |
* Technically it could be pretty simple, a "join" of the array, then parse to int, then sum 1, then split again the numbers | |
* and parse each to integer, and that's it, but the rules are: | |
* the numbers are going to be between 1 and 9, no trailing zero...like: [1,2,3,4,5,6,7,8,9] in the array but... | |
* the array size can be up to 10,000 (10e4 -> 10 * 10 * 10 * 10) so we can have a digits.length of 10,000 which means, we can have | |
* a loooooooooooooot of numbers that are going to "sum" a really big number, if we use parseInt then, after the "join" we are going | |
* to end with a "32131e1221" which is something that we don't want to... | |
* and the "+1" rule so for example: [9,9,9] is converted to 999 + 1 = 1000 and we need to return [1,0,0,0] | |
* | |
* First as I already said, we don't want to use "parseInt" because it doesn't work for us, so we are going to use BigInt and | |
* that's it... | |
* | |
* @param {array} digits The digits in a way like "[1,2,3]" | |
* @return {array} The new array with the "+1" | |
*/ | |
function addOne(digits) { | |
return (BigInt(digits.join('')) + BigInt(1)).toString().split('').map(el => parseInt(el, 10)); | |
} | |
/** | |
* Simple tests: | |
*/ | |
console.log(addOne([9,9,9])); // Returns [1,0,0,0] | |
console.log(addOne([1,2,3])); // Returns [1,2,4] | |
/** | |
* This function actually return random, so I don't know what value is going to return but... | |
* Uncomment the conditionals to test that if we add "9" at the end, the previous actually works! | |
* | |
* @return {array} | |
*/ | |
function test() { | |
/** | |
* An array to create a random test | |
* @type {array} | |
*/ | |
const _test = []; | |
/** | |
* A "function" to generate random numbers between two...you can change this but well, you know, follow the rule | |
* but I leave it with the chance to be modified so you can test it with whatever you want... | |
* Just don't add "0" in min or negative numbers, and don't add 10 or more in the max...also, always min is lower | |
* than max...so don't do something like "3,2" because well, I did this to test it fast ¬¬ don't complain... | |
* | |
* @type {number} | |
* @param {number} min The min number (like 1...or 2) | |
* @param {number} max The max number (like 8...or 9) | |
* @return {number} A random value between "min" and "max" numbers | |
*/ | |
const rand = (min, max) => Math.floor(Math.random() * (max - min) + min); | |
for(let j = 1; j < 100; j++) { | |
_test.push(rand(0,9)); | |
} | |
return _test; | |
} | |
console.log(addOne(test())); // Returns ...don't know sorry :P |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment