0

How can I get 1 to 10 to show by fixing this code??

<script type="text/javascript"> 
 var count = 0; 
 var numbers = new Array(10);
 while (count <=10) { 
 numbers[count] = count;
 ++count;
 }
 count = 0;
 while (count <=10) {
 document.write(numbers[count] + 1 + "<br />");
 +count();
 }
</script> 

i am new to this, any help would be much appreciated

dif
2,5036 gold badges38 silver badges51 bronze badges
asked Feb 23, 2012 at 2:58
1
  • What's the behavior right now? I have an idea of what to do but I'd like to know if my expected behavior is occurring. Commented Feb 23, 2012 at 3:01

2 Answers 2

2

The main problem is this line:

+count();

This is a syntax error since you don't have a function called count, so execution simply stops as soon as it reaches that line. Replace it with:

++count;

(As done in the previous while loop.)

The second problem is that your loops are running from 0 to 10 inclusive, so you end up with 11 iterations. Change <= 10 to < 10.

Having said that, the whole thing seems a bit pointless. You create an array where item 0 holds the value 0, item 1 holds the value 1, etc., and then you print those values out? Why bother with the array at all? There's no point looking up the item at any particular array index if you already know in advance the value will be the same as the index.

If all you want is to display the numbers 1 through 10 then this will work:

for (var i=1; i <= 10; i++)
 document.write(i + "<br />");
answered Feb 23, 2012 at 3:01
Sign up to request clarification or add additional context in comments.

Comments

0

this will suffice:

var count = 0;
while (count <=9) {
 document.write(numbers[count] + 1 + "<br />");
 count++;
}
answered Feb 23, 2012 at 3:02

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.