Build a "simple" Algorithm. (Modolus and randomness)

Job ID: 35012396

Budget: $10 – $30 USD

Hi i have an algorithn, thats supposed to add random numbers to an array based on input.



let itemsReceived = [];
let itemsLeft = 10;
let items = [];

const addToArray = (random) => {
let randomResult = random % itemsLeft; //only take random number between 1 and items left.
if(randomResult == 0) { randomResult = 1; }

if (items[randomResult] == undefined) {
//lets imagine randomResult = 1
itemsReceived.push(randomResult); //pushing 1 to winnerArray
items[randomResult] = itemsLeft; //pushing 10 as items[1] because if next time someone chooses it, he will receive item 10.
} else {
//item is 1 again, but now the items[randomResult] == 10;
itemsReceived.push(items[randomResult]); //winner becomes 10
items[randomResult] = itemsLeft; //pushing 9 as items[1] becuase if next time someone chooses it, he will receive item 9.
}

itemsLeft--;
}

I need this function to count down from the itemsLeft and add randomness we receive.
The idea is that a number cannot come twice, and we cannot use for loops, in needs to be done in a similiar way.


I'm too tired to think straight atm , and i need to finish this urgently.
Related categories: JavaScript Algorithm Software Architecture