Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-09-2012, 05:16 PM   PM User | #1
tim656
New to the CF scene

 
Join Date: Nov 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
tim656 is an unknown quantity at this point
javascript grade calculator

PHP Code:
[LEFT]<html>
<
body>
 <
script type "text/javascript">
         <!--
         var 
gradeCounter,  
             
gradeValue,    
             
total,         
             
average,       
             
grade;         
         
total 0;        
         
gradeCounter 0;  
         
grade prompt("enter grade, -1 to Quit:""0" );
         
gradeValue parseIntgrade );
         while ( 
gradeValue != -) {
            
total total gradeValue;
            
gradeCounter gradeCounter 1;
            
grade prompt"enter grade, -1 to Quit:""0" );
            
gradeValue parseIntgrade );
         }
         if ( 
gradeCounter != ) {
            
average total gradeCounter;  
            
document.write"<br>total grade: " gradeCounter "</bt>" );
            
document.write"<br>average passing grade:" average "</br>");
         }
         else 
            
document.write"total grade:"+);             
      
</script>
      </body>
      </html>[/LEFT] 
i'm writing this code for grade calculating, so far my code does the calculation as i wanted, however i try to get the code to actually show the inputs on the webpage like this

PROMPTS OUTPUT in webpage
“Please enter a grade” 85 85 Pass
“Please enter a grade” 61 61 Fail
“Please enter a grade” 95 95 Pass
“Please enter a grade” -1 Total grades: 3. Average passing grade: 90.
“Please enter a grade” -1 Total grades: 0.
“Please enter a grade” 50 50 Fail
“Please enter a grade” -1 Total grades: 1.

so everytime i type in a grade, it will show on the webpage later.

any help is appreciated. thank you.

Last edited by tim656; 11-09-2012 at 05:19 PM..
tim656 is offline   Reply With Quote
Old 11-09-2012, 06:52 PM   PM User | #2
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,764
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
Making some assumptions here base upon what I think are your requirements.
Code:
<html>
<body>
<div id="results"></div>
<script type="text/javascript">
var gradeCounter,  
    gradeValue,    
    total,         
    average,       
    grade;         
var total = 0;        
var gradeArray = [];

grade = prompt("enter grade, -1 to Quit:", "0" );
gradeValue = parseInt( grade );
while ( gradeValue != -1 ) {
  total = total + gradeValue;
  gradeArray.push(gradeValue);
  grade = prompt( "enter grade, -1 to Quit:", "0" );
  gradeValue = parseInt( grade );
}
if ( gradeCounter != 0 ) {
  average = total / gradeArray.length;  
  str = 'Grades entered:<br>'+gradeArray.join('<br>');
  str += "<p>total grades entered: " + gradeArray.length;
  str += "<br>average passing grade:" + average + "<br>";
} else str = "total grade: 0";             
document.getElementById('results').innerHTML = str;
</script>
</body>
</html>
prompt() is not the best way to collect user information.
For example:
1. What effect does a wrong entry have on your calculations.
2. How would user make a correction without starting ALL OVER again!!!
3. How would handle an entry that the user wanted removed/

Good Luck!
jmrker is offline   Reply With Quote
Old 11-09-2012, 09:00 PM   PM User | #3
tim656
New to the CF scene

 
Join Date: Nov 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
tim656 is an unknown quantity at this point
can i use something else instead of var gradeArray = []; my professor haven't get to array yet.
tim656 is offline   Reply With Quote
Old 11-09-2012, 09:44 PM   PM User | #4
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,764
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
Lightbulb

Quote:
Originally Posted by tim656 View Post
can i use something else instead of var gradeArray = []; my professor haven't get to array yet.
Don't use document.write() as it will reset your page after it is rendered (displayed).
You can use it in your script because you don't render it until all data is collected, then displayed.
You will need to save the inputs of the user somewhere.
If you don't use an array, then you will need to save it to a continuous string, as in:
Code:
var str = '';
...
while ...
...
  x = prompt(...)
  str += x+'<br>';  // if saved to an element with .innerHTM (as in earlier example)
//  str += x+'\n';  // if saved to an <textarea> element with .value (little research needed here)
...
}
// save 'str' above to 'results' element or <textarea> if desired.
...
jmrker is offline   Reply With Quote
Old 11-09-2012, 11:12 PM   PM User | #5
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,453
Thanks: 0
Thanked 498 Times in 490 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
You should be using a form to collect the data - prompt is a debugging tool and in some browsers displays a checkbox for turning off JavaScript.

You should be using innerHTML to display the output into a specified spot in the page.

The best place to put JavaScript is immediately before the </body> tag where all of the HTML has already rendered and where it doesn't slow the loading of the page.

Those are just about the first things that anyone learning JavaScript needs to know so if you are more than five minutes into a JavaScript course and the teacher hasn't mentioned those things yet then your teacher needs to take a JavaScript course themselves.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 11-11-2012, 01:29 AM   PM User | #6
tim656
New to the CF scene

 
Join Date: Nov 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
tim656 is an unknown quantity at this point
well in my assignment, i'm only allow to use prompt() as input and document.write() to print results. Another question, how can i get my code to print a result like this

85 pass
61 fail
95 pass
total grades: 3. Average passing grade: 80.33333333

so far my code only print this

85
61
95
total grades: 3. Average passing grade: 80.333333333333
tim656 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:42 PM.


Advertisement
Log in to turn off these ads.