Please help me with ideas how to solve this task

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fool1
    New Member
    • Apr 2015
    • 5

    Please help me with ideas how to solve this task

    Produce JavaScript code (and any necessary HTML) that will perform the following functions:
    1. Using the prompt and alert functions, invite the user to input 5 marks (integers) in the range 0–100.
    2. Calculate the average (mean) of the 5 marks as a rounded integer or a floating point number.
    3. If the average mark is 0–39, then display a message saying that the grade is a Fail, 40–69 is a Pass, and 70–100 is a Distinction. If the value is less than 0 or greater than 100, display a message saying that there has been an input error.
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    1. use an HTML form with the number type input elements.
    2. JavaScript only knows floats. simple way uses a loop, advanced way can use array functions
    3. use if() conditions

    Comment

    • fool1
      New Member
      • Apr 2015
      • 5

      #3
      please can you show example? I am stupid and new to JavaScript

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        stupidity and newness are no excuse to not put in some effort.

        Comment

        • fool1
          New Member
          • Apr 2015
          • 5

          #5
          Code:
          <script>
              function promptForNumber( text)
          {
              if(text == '' ){
               text = "Please enter a number from 1 to 100";   
              }
              var number = parseInt(window.prompt(text, ""));
              checkNumber(number);
          
          }
          function checkNumber(number){
          
              if (number <= 100 && number >= 1) {
              
          } else if (isNaN(number)) {
              promptForNumber("Please enter a number from 1 to 100", "");
          } else {
              promptForNumber("Your number (" + number + ") is not between 1 and 100", "");
          }
          
          }
          </script>
          Last edited by Dormilich; Apr 16 '15, 02:33 PM. Reason: please use code tags

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            that’s definitely something to work on.

            although there is a crucial unclearness in the task description. is the user supposed to input 5 numbers at once or one after another?

            that would greatly influence your application design.

            Comment

            • fool1
              New Member
              • Apr 2015
              • 5

              #7
              I beleive the numbers should be entered one after another and then button to calculate mean

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                you will never come to use the button because you have one prompt after another.

                if your input solely relies on prompts, then you must find a condition when to not show again a prompt. at that time (when no more input comes) you can start the calculation right away.

                for me it would make more sense to use a form for input (and it would be more user-friendly as well), esp. since you have a defined number of inputs. additionally you could use the built-in validation.

                I am aware that that would do away the prompt and alert functions altogether. but those shouldn’t be used for UI design anyways.

                Comment

                • fool1
                  New Member
                  • Apr 2015
                  • 5

                  #9
                  ok thanks a lot. what if the number are to be computed at once ? could help with some part of the code?

                  Comment

                  • Dormilich
                    Recognized Expert Expert
                    • Aug 2008
                    • 8694

                    #10
                    first you need to get the numbers.

                    once you have the numbers together it doesn’t matter any more how you got them.

                    Comment

                    Working...