0

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.

3
  • 1
    You're missing some curly braces {} to define the body of the for loop. And why >=? The task is clearly in its requirement "...except for the numbers 29, 71, and 105..." and not "...every number above 29..." ;) Commented Jul 6, 2015 at 5:55
  • Thank makes sense! Thanks for helping. My logic was flawed in thinking that the text would only display if the random integer selected was 29, 71 or 105. How would you remove the number 29, 71 and 105 to replace with text? I used the curly braces and changed my >= to == and it still includes the numbers Commented Jul 6, 2015 at 6:13
  • Exactly as Arun wrote it in his update to your comment :) Commented Jul 6, 2015 at 6:16

3 Answers 3

3

You have a problem in the loop, since you have not used a block for the loop only the immediate statement after for is executed in the loop.

Also your if conditions are not corrent as they compare of greater than or equals to instead of checking for equality.

But you can simplify the logic as given below

var display = Math.floor(1 + Math.random() * 110),
  specials = {
    29: 'twenty-nine',
    71: 'seventy-one',
    105: 'one-hundred-five'
  };

for (var i = 1; i < display; i++)
  document.writeln(specials[i] || i);


To make your code work

var display = Math.floor(1 + Math.random() * 110);
for (var i = 1; i < display; i++) {//for block
  if (i == 29) {
    document.writeln("twenty-nine");
  } else if (i == 71) {
    document.writeln("seventy-one ");
  } else if (i == 105) {
    document.writeln("one-hundred-five ");
  } else {
    document.writeln(i);
  }
}

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, this works wonderfully. Is there a way to incorporate the specials into a conditional statement?
0

Close! Two pointers:

  1. Use the curly braces { and } so that statement blocks get executed, not a single statement, for loops and conditionals. You should probably have for ... { ... }, rather than for ... ....

  2. Do you mean i >= 29? Or perhaps a different operator?

2 Comments

I wasn't sure which one was correct, but this code just writes the text at the end of the loop instead of replacing the value of 29 with "twenty-nine"
Different operator! So the i and the 29 are called operands, and the operator operates on the operands :) That one evaluates as true if the left operand is greater than or equal to the right operand. Is that want we want? Hint hint not quite! The loop issue is because of the lack of curly braces. So it executes the line document.writeln(i); within the loop, but the other parts get executed only once. Add some braces around the part that you want to repeat!
0

You could make a general solution for it. If you split the numbers, then you could loop through it.

function numberToWords(value) {
  var numbers = value.split("");
  var decs =;["zero","one","two","three","four","five","six","seven","eight","nine"]
  var tens = ["ten","twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninety"];
  var s="";
  for(var i=0;i<numbers.length;i++)
  {
    var ri=numbers.lenght-i; //reverse index
    if (ri==0) s+= dec[ri];
    else if (ri==1) s+= tens[ri];
    else if (ri==2) s+= dec[ri]+"-hundred";
    s+="-";
  }
  return s;
}

Then use numberToWords(28).

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.