1

I am really new to JavaScript and i am stuck on an exercise i have been given. I am using TextPad as this is what i have been asked to use for this exercise.

Basically i need to have one input parameter that is a number/integer and then have 12 output parameters that show the input has been divided by 12 and that each output parameter has a value that does not exceed the previous or next one by more than 1.

I have written the below code so far using an array and a for loop but i just dont know how or what to do in terms of getting the output parameters to show each value of the array divided evenly.

var amount = "30";

var camels = ["cam1", "cam2", "cam3", "cam4", "cam5", "cam6", "cam7", "cam8", "cam9", "cam10", "cam11", "cam12"]

for(i=0;i<camels.length;i++){
    WScript.echo(camels[i] + "|" + amount/12);
}

for(i=1; i<13; i++){
    n = Math.random();
    o = n * 12;
    p = o + 1;
    q = Math.floor(p);
}

This is really confusing me so any help would be appreciated but please bare in mind i am a complete novice.

Cerbrus
  • 60,471
  • 15
  • 115
  • 132
  • 3
    Can you give an example of the expected output? – 0xMB Oct 08 '14 at 10:21
  • yes sorry, so for example if the input is 30 the output should be something like 2 3 3 2 2 3 2 3 3 2 3 2 –  Oct 08 '14 at 10:22
  • 2
    ... How? What is the exact requirement / assignment? – Cerbrus Oct 08 '14 at 10:23
  • 1
    This is the assignment i was given... A trader is taking his goods to market on a train of 12 camels. He wants to divide the load evenly, but since the number of goods might not be an exact multiple of 12, some camels will have to carry one extra. To be fair on the animals, the trader wants to distribute the extra load randomly. Write a procedure that distributes the load. It should have 1 input parameter that defines the number of goods, and have 12 output parameters - one for each camel. The sum of the outputs should be the same as the input. Each output should be an integer, –  Oct 08 '14 at 10:24
  • and no output should differ by the others by more than 1. –  Oct 08 '14 at 10:25
  • Makes a lot more sense given the actual assignmnet. But this is homework, so dont expect a full working solution - thats what you're supposed to do. – Jamiec Oct 08 '14 at 10:26
  • Of course i understand that im just looking for some help on where to go after the code i have written like what functions or processes i could use to obtain the output –  Oct 08 '14 at 10:30

1 Answers1

6

This is a homework question, and you gain nothing by getting a complete solution (other than perhaps an A which you wouldn't deserve!)

Instead I can give you some pointers, which will hopefully get you to the right answer.

The first thing to be aware of is that when doing mathematical calculations you want to be dealing with numbers, so change your first line appropriately

var amount = 30;

(Be aware that if you are letting the user input this number you will want to ensure its numeric and convert it to a number - user input is almost always a string. Use parseInt(<user input>,10)).

The second thing to do is divide your amount by 12. Floor this number using Math.floor to get an integer. In this example you will get 24.

(Another way to think of the above is to investigate the modulo operator %, which can be used instead)

At this point you can create an array, with 12 elements (one for each camel) each element should have a count of 2 (representing 2 cargo items each). (see ref1)

With this example you will end up with a remainder of 6 and the only thing left to do is distribute the remaining 6 randomly among your camels.

You can achieve this by putting all 12 camels into an array, loop 6 (or, whatever your remainder was) times and pick a random number between 0 and {length of camel array}-1. Assign that cargo to the selected camel and remove that camel from the camel array. This ensures that no one camel randomly gets assigned more than one extra cargo.

As with programming in general, that last bit should be broken down into individual manageable steps.

  1. Create an array with 12 elements (where 12 is the number of camels). The elements of this array should be numbers 0-11 representing the index in the array earlier
  2. Create a loop to repeat the following code once for each remainder
  3. Inside the loop :
    • pick a random number (see ref2) between 0 and the length of the array created in 1.
    • use that random number to increment the count in the array created earlier
    • remove the element from the array created in 1.

Don't get confused; there are 2 arrays here. One you created much earlier - it has 12 elements and tracks the number of cargos on each camel, the other is temporary, used for tracking which camel has been assigned an "extra" cargo. The former never changes length, whereas the latter gets reduced in size as camels are assigned an extra cargo.


reference material:

Community
  • 1
  • 1
Jamiec
  • 118,012
  • 12
  • 125
  • 175