4

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

4
  • Are you looking for something like this: jsfiddle.net/abhitalks/73sojfps/1 ? Commented Nov 12, 2014 at 10:07
  • JScript != Javascript. en.wikipedia.org/wiki/JScript Commented Nov 12, 2014 at 10:08
  • How important is the randomness? Is it OK to divide the amount equally and randomly distribute the remainder? Or, is the constrained random process itself required to completely determine the outcome? Commented Nov 12, 2014 at 10:18
  • Also, WScript.echo isn't typical Javascript. Ordinarily JS code would use console.log(string) or document.write(string). Do you have any Javascript that you have tried? Commented Nov 12, 2014 at 10:21

2 Answers 2

2

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--;
    }
}
Sign up to request clarification or add additional context in comments.

7 Comments

Beat me to it. But I was going to give him your var index line and show how to index an array and add a unit, suggesting he complete the rest of it himself.
@Paul Yeah, I got carried away a little bit :-)
@RobbyCornelissen Please could you help me explain how each part of the code works as it works but i dont quite understand the full syntax
@user2982634 I'll update my answer with an annotated version.
@user2982634 please see my post stackoverflow.com/questions/27144840/…
|
0

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.

1 Comment

There's no randomness in your code. The post says "randomly disperse". Also, it is a good idea to post your entire code. Otherwise, the jsFiddle link might disappear for future visitors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.