Reputation:
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.
Upvotes: 0
Views: 87
Reputation: 136074
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.
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:
Math.random
Upvotes: 6