Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Nov 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Explain: particular WHILE condition

    I'm trying to understand why this particular while loop executes if I decide to enter 11 as the TopNumber prompt. From my understanding the while loops only execute if the conditions are TRUE. It checks to see if TopNumber is < 1 or > 100. If I enter 11 this would make the conditions false and the while loop should not execute? I'm having a difficult time understand any clear explanation would be very helpful.

    PHP Code:
    <!DOCTYPE html>
    <
    html lang="en">
    <
    head>
        <
    meta charset="UTF-8">    
        <
    title>Sum the Numbers</title>
           <
    script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>     
           <script>
            alert("Sum the Numbers from 1 through entry");
            
            do {
                var topNumber = prompt("Enter top number to sum", 5);
                topNumber = parseInt(topNumber);
                if (isNaN(topNumber) || topNumber < 1 || topNumber > 100) {
                    alert("Top number must be greater than zero and less than 100");
                }
            }
            while (isNaN(topNumber) || topNumber < 1 || topNumber > 100);
            
            var sum = 0;
            for (i = 1; i <= topNumber; i++) {
                sum += i;
            }
            alert("Sum of numbers from 1 through " + topNumber + " = " + sum);
            
        </script>
    </head>
    <body>
        <section>
            <h1>This page is displayed after the JavaScript is executed</h1>
        </section>
    </body>
    </html> 

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 Posts
    I accidentially double posted ... see answer below

  • #3
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 Posts
    Yes, a while loop would work like this, but you are using a do/while

    - A while loop will evaluate the condition first, then execute the code if the condition is still met
    - A do/while loop will execute the code first, then evaluate the condition, then execute the code only if the condition is still met

  • #4
    New to the CF scene
    Join Date
    Nov 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    so the do loop *must execute once and if a number from [1,100] is not entered then the appropriate if condition will be accessed followed by the corresponding message. and the prompt message will ask again for another number between 1 and 100 correct? and then program will exit successfully if a *correct number is entered, for example 11?

    I hope I'm right.

  • #5
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,462
    Thanks
    0
    Thanked 633 Times in 623 Posts
    A do/while loop tests the condition at the end of the loop and so always runs at least once. A while loop does the test at the top of the loop and so will not run at all unless the condition is satisfied.

    Note that alert and prompt in live scripts became obsolete when Netscape 4 died and now that all browsers have a debugger built in there isn't even a use for them for debugging. About the only remaining use for alert is as a placeholder for the part of the code that hasn't been written yet while testing the part you have written.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #6
    New Coder donna1's Avatar
    Join Date
    Nov 2012
    Location
    london
    Posts
    99
    Thanks
    9
    Thanked 4 Times in 4 Posts
    I like the prompt command, cant see the benefit of the DOM / document / .inner html method that makes it 10x more complex!

    How do you activate the browsers debugger??
    That would be v useful

  • #7
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,462
    Thanks
    0
    Thanked 633 Times in 623 Posts
    Quote Originally Posted by donna1 View Post
    I like the prompt command, cant see the benefit of the DOM / document / .inner html method that makes it 10x more complex!

    How do you activate the browsers debugger??
    That would be v useful
    prompt has been obsolete since browsers were able to access values from form fields. Also some browsers assume that it is being used for debugging and display extra checkboxes. It can also be easily disabled in the browser. The same issues apply to confirm and alert. My browser is set to never display confirm or prompt boxes and to only allow alert to display when running a local script.



    Using innerHTML is far simpler than creating your own custom alert (so as to ensure you are not asking your visitors if they want to turn off JavaScript).



    The only time it makes sense to use alert() is for unit testing of JavaScript functions before adding them to the rest of your code. There is no circumstance where it is necessary to use confirm() or prompt() any more.

    In most browsers the debugger is accessed via the F12 key. In all browsers there is an option in the browser menu that will open the debugger and which tells you the key that opens it directly.

    Also with just a couple of extremely rare exceptions ALL JavaScript should be placed just before the </body> tag. You are placing your script in the wrong place and that's why using the correct calls is more difficult.
    Last edited by felgall; 11-11-2013 at 09:47 PM.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • Users who have thanked felgall for this post:

    donna1 (11-12-2013)


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •