user2982634
user2982634

Reputation:

How to add a value randomly to an array using JavaScript

I am new to JavaScript and i am going through a task where i have to randomly disperse an input value between 12 loads. The other side is that each element of the array cannot differ from the next by more than one.

so for example if i have an amount of 30 i need to distribute this amount between 12 camels. I have so far written the code below, but i am using TextPad as requested i am not sure how to print out the result on the same line.

var amount = 30;

var camels = [0,0,0,0,0,0,0,0,0,0,0,0]

var div = amount/12;

var mod = amount%12;

var x = mod / 12;


for(i=0;i<camels.length;i++){
    WScript.echo(camels[i] + "|" + Math.floor(div) + "|" + mod + "|" + x)
}

please comment if you need anymore info , Thanks

Upvotes: 4

Views: 109

Answers (2)

Robby Cornelissen
Robby Cornelissen

Reputation: 97130

Here's my take on it. Note that for the requirement that states that the array values cannot differ from the next one by more than one, I consider the array to be looping, i.e. the value after the last value is the first value again.

var amount = 30;
var camels = [0,0,0,0,0,0,0,0,0,0,0,0];

while (amount > 0) {
    var index = Math.floor(Math.random() * camels.length);
    var previous = (camels.length + index - 1) % camels.length;
    var next = (index + 1) % camels.length;

    if (Math.abs(camels[index] + 1 - camels[previous]) <= 1
        && Math.abs(camels[index] + 1 - camels[next]) <= 1) {

        camels[index]++;
        amount--;
    }
}

Update

As requested by the OP, here's an annotated version:

// the amount that needs to be distributed among the camels
var amount = 30;

// the actual values for all 12 camels, initially all zero
var camels = [0,0,0,0,0,0,0,0,0,0,0,0];

// as long as we have something to distribute
while (amount > 0) {

    // get a random current index in the array, i.e. a value between 0 and 11
    var index = Math.floor(Math.random() * camels.length);

    // calculate the index previous to the current index;
    // in case the current index is 0, the previous index will be 11
    var previous = (camels.length + index - 1) % camels.length;

    // calculate the index next to the current index;
    // in case the current index is 11, the next index will be 0
    var next = (index + 1) % camels.length;

    // if adding 1 to the camel at the current index makes it so that
    //     the difference with the camel at the previous index is 1 or lower
    //     the difference with the camel at the next index is 1 or lower
    if (Math.abs(camels[index] + 1 - camels[previous]) <= 1
        && Math.abs(camels[index] + 1 - camels[next]) <= 1) {

        // go ahead and add 1 to that camel
        camels[index]++;

        // and decrement the amount accordingly
        amount--;
    }
}

Upvotes: 2

Luca De Nardi
Luca De Nardi

Reputation: 2321

By adding an outer cycle you can correctly add the remaining part of the amount.

while(amount > 0){
    //add amount to camels
}

Check if this Fiddle is what you want to achieve.

Upvotes: 0

Related Questions