dimanche 5 juillet 2015

How can I replace a number with a conditional statement in a for loop?

I need to replace a value using an if statement in my for loop. The exact instructions are:

Add a <script></script> element to the <head> section of the page which first selects a random integer between 1 (inclusive) and 110 (inclusive) using the Math.random() function. Your code should then write each integer from 1 up to (but not including) the random integer to the page except for the numbers 29, 71, and 105, which should be spelled out (i.e., “twenty-nine”, “seventy-one”, “one-hundred-five”). You’ll need to use a while loop or for loop for iteration and a conditional statement to print the special cases. Example:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 twenty-nine 30 31 32 33 34 35

So far I have this for my script

<script>
var display = Math.floor(1 + Math.random() * 110);

for(var i=1; i < display; i++)
document.writeln(i);



if (i >= 29) 
{
//[29] = "twenty-nine"
    document.write(i[29].replace(29, "twenty-nine"));
}
if (i >= 71)
{
    document.write("seventy-one ");
}
if (i >= 105) 
{
    document.write("one-hundred-five ");
}

</script>

I can't figure out how to replace the numbers when they aren't in a specified array.

Aucun commentaire:

Enregistrer un commentaire