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

Plot No. 64, PU-4, Scheme 54, Behind C21 Mall near Hotel Holiday , AB Road, Indore Pin-code:452001

Email Us

contact@codebetter.in

Call Us

+91 88230 75444, +91 99939 28766

Loading
Your message has been sent. Thank you!