By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,677 Members | 1,506 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,677 IT Pros & Developers. It's quick & easy.

Please help me with ideas how to solve this task

P: 5
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 0100.
2. Calculate the average (mean) of the 5 marks as a rounded integer or a floating point number.
3. If the average mark is 039, then display a message saying that the grade is a Fail, 4069 is a Pass, and 70100 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.
Apr 16 '15 #1
Share this Question
Share on Google+
9 Replies

Expert Mod 5K+
P: 8,639
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
Apr 16 '15 #2

P: 5
please can you show example? I am stupid and new to JavaScript
Apr 16 '15 #3

Expert Mod 5K+
P: 8,639
stupidity and newness are no excuse to not put in some effort.
Apr 16 '15 #4

P: 5
Expand|Select|Wrap|Line Numbers
  1. <script>
  2.     function promptForNumber( text)
  3. {
  4.     if(text == '' ){
  5.      text = "Please enter a number from 1 to 100";   
  6.     }
  7.     var number = parseInt(window.prompt(text, ""));
  8.     checkNumber(number);
  10. }
  11. function checkNumber(number){
  13.     if (number <= 100 && number >= 1) {
  15. } else if (isNaN(number)) {
  16.     promptForNumber("Please enter a number from 1 to 100", "");
  17. } else {
  18.     promptForNumber("Your number (" + number + ") is not between 1 and 100", "");
  19. }
  21. }
  22. </script>
Apr 16 '15 #5

Expert Mod 5K+
P: 8,639
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.
Apr 16 '15 #6

P: 5
I beleive the numbers should be entered one after another and then button to calculate mean
Apr 16 '15 #7

Expert Mod 5K+
P: 8,639
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.
Apr 16 '15 #8

P: 5
ok thanks a lot. what if the number are to be computed at once ? could help with some part of the code?
Apr 16 '15 #9

Expert Mod 5K+
P: 8,639
first you need to get the numbers.

once you have the numbers together it doesn’t matter any more how you got them.
Apr 16 '15 #10

Post your reply

Sign in to post your reply or Sign up for a free account.