简体   繁体   中英

How to sum numbers from 1 through user's entry, using while-loop, do-loop?

I am trying to complete the following code to achieve the sum of numbers from 1 to 100(user's entry must be 1-100), using whileloop or doloop. I am new to this so, any help is much appreciated!

In the following code, I used prompt method to get the user entry. Wrote the code, to sum numbers; from 1 through the user's entry. I displayed the result in an alert box. Now, my challenge is I want to display an error message if the user's entry outside the 1-100 range. And after that, I do not want to do any calculations if user clicks cancel and stop displaying the prompt box.

<!DOCTYPE html>
        <html>
        <head>
        <title>Sum of Numbers</title>
     <script>
      var numbers = prompt("Enter a  number 1-100");
    
      while (numbers!=null && (isNaN(parseInt(numbers)) || parseInt(numbers) >100 || parseInt(numbers) <1)) {
          numbers = prompt("Try again.Enter a  number 1-100");
      }
      if (numbers !=null){
            alert("Finally you entered a correct number");
      }
     
      var sum = 0;
      var numOfLoops = numbers;
      var counter = 1;
      do {
        sum+=counter;
        counter++;
      } while (counter<=numOfLoops)
      alert ("sum=" +sum);
    
     </script>
    
    </head>
    <body>
        <script>
        document.write("<h1>Sum of Numbers</h1>");
            document.write("The sum of numbers from 1 to = " + numbers +  "&nbsp;is = "  +
                + sum + "<br><br>");
        </script>
    
    </body>
    </html>

Simply move the calculation logic inside the condition where the user enters the correct input. This will make sure that the prompt closes automatically when you click on the cancel button (Prompt returns null when the user clicks on cancel)

    <script>
        var numbers = prompt("Enter a  number 1-100");

        while (numbers != null && (isNaN(parseInt(numbers)) || parseInt(numbers) > 100 || parseInt(numbers) < 1)) {
            numbers = prompt("Try again.Enter a  number 1-100");
        }
        if (numbers != null) {
            alert("Finally you entered a correct number");

            var sum = 0;
            var numOfLoops = numbers;
            var counter = 1;
            do {
                sum += counter;
                counter++;
            } while (counter <= numOfLoops)
            alert("sum=" + sum);
        }

    </script>

You could simply use a do…while to solve your problem, eg :

 let n = null; do { n = parseInt(prompt('Enter an int number between 1 and 100')); } while (isNaN(n) || (n < 1 || n > 100)); let sum = n * (n + 1) / 2; alert('The sum of all int numbers from 1 to ' + n + ' is: ' + sum);

NB The sum of the first n integer numbers can be computed as n * ( n + 1) / 2, with O (1) complexity - reducing the O ( n ) complexity of your for loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM