JavaScript Practice Questions And Answer

Problem: Take the numbers from user (until ten +ve numbers entered by the user), and print sum and average of these numbers.
Solution:
<html>
 <head>
  <script>
    function sum10()
  {
    var count = 0
    var sum= 0
    while(count<10)
     {
       var n = +prompt("Enter number")
        if(n>0)
         {
           count = count + 1
            sum = sum + n
          }
      }
        var avg=sum/10
        var result = document.getElementById("result")
        result.innerHTML = "Total = " + sum  +"avg ="+avg
  }
  </script>
 </head>
      <body>
        <input type="button" value="Go" onclick="sum10()"/>
        <div id="result">
            sum=0, avg=0
        </div>
      </body>
</html>
          

Contact Us

Our Address

Office no.- 401,Shekhar Central Building ,Palasiya, Pin-code:452001, Indore

Email Us

contact@codebetter.in

Call Us

+91 88230 75444, +91 99939 28766

Loading
Your message has been sent. Thank you!