2

I'm a beginner with Javascript and can't get the code below to work. When I click the button, nothing happens. Could someone point out what I did wrong? Thanks!

<html>
<head>
<title>Sarah's Puppy Game</title>
</head>
<body>
    <div id="input">
        <input id="puppyNumber" size=30>
        <button onClick="doLoop()"> Get your puppies </button>
    </div>
    <script type="text/html">
        function doLoop() {
        var number = document.getElementById('puppyNumber').value;
            var puppy = [];
            while (var i = 0; i < parseInt(number); i++) {
                puppy = puppy.push('puppy<br>');
            }
            alert(puppy);


        }
    </script>
</body>
</html>
4
  • What do you expect to happen? Commented Jan 2, 2012 at 3:23
  • an alert box to popup with the word puppy printed the number of times i entered into the input Commented Jan 2, 2012 at 3:24
  • Things you can try when "nothing happens": 1) Check your browser's JavaScript error log 2) Add alert statements to get a glimpse of what's happening. Commented Jan 2, 2012 at 3:31
  • did you check the error log of browser?is the function not firing at all??? Commented Jan 2, 2012 at 4:59

2 Answers 2

5

Three problems I see.

  1. First, use text/javascript for the <script type='text/javascript'>

  2. Second, change while to for in your loop. A while loop is for testing against a condition, but you have setup a counter.

  3. Thrid, don't assign puppy with the push() method. .push() acts directly on the array.

Here's the corrected version in action.

<!-- type="text/javascript" not "text/html" -->
<script type="text/javascript">
    function doLoop() {
    var number = document.getElementById('puppyNumber').value;
        var puppy = [];

        // This is a for loop, rather than a while loop
        // Also for good measure, use the second param to parseInt() for a
        // decimal radix to avoid getting binary or octal numbers accidentally.
        for (var i = 0; i < parseInt(number, 10); i++) {
            // Don't assign the result back to puppy
            puppy.push('puppy<br>');
        }
        alert(puppy);


    }
</script>
Sign up to request clarification or add additional context in comments.

Comments

1

Instead of...

puppy = puppy.push('puppy<br>');

...just say...

puppy.push('puppy<br>');

push returns the element that was pushed, not the array to which it was pushed.

Your code will cause the variable puppy to be set to the string "puppy<br>". Then you'll try to do "puppy<br>".push("puppy<br>"), which is obviously invalid.

3 Comments

Michael's answer is the more complete answer here, and I have marked it as such.
you guys are awesome thanks so much! is there a reason to use the for rather than while loop in general in javascript?
You can use a while loop, but the syntax for while loops is different. You would need to do while(i < number) { puppy.push(str); i++ }

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.