Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <html>
  2.     <title>
  3.     Asssignment 2: GPA calculator_Madeleine Gold
  4.     </title>
  5.     <head> <meta charset="utf-8"/>
  6.    
  7.         <script type="text/JavaScript">
  8.        
  9.             //Declare arrays to hold data
  10.             var courseCodeArray = new Array();
  11.             var courseGradeArray = new Array();
  12.            
  13.             //HTML output functions
  14.             function writeTableStart() {
  15.                 //Print table
  16.                 document.writeln("<table border = '1' >");
  17.            
  18.                 //Table row 1, headings
  19.                 document.writeln("<tr>");
  20.                 document.writeln("<th>Course Code</th>");
  21.                 document.writeln("<th>Grade</th>");
  22.                 document.writeln("</tr>");
  23.             }
  24.            
  25.             function writeTableRow(code, grade) {
  26.                 //Table rows
  27.                 document.writeln("<tr>");                      
  28.                 document.writeln("<td>"+code+"</td>");                     
  29.                 document.writeln("<td>"+grade+"</td>");                    
  30.                 document.writeln("</tr>");         
  31.             }
  32.            
  33.             function writeTableEnd() {
  34.                 //End table
  35.                 document.writeln("</table>");
  36.             }
  37.    
  38.            
  39.             function writeSummary(numCourses, highestGrade, lowestGrade, GPA) {
  40.                 //Output unordered list
  41.                 document.writeln("<ul>");
  42.                 document.writeln("<li> You have entered "+numCourses+" courses</li>");
  43.                 document.writeln("<li> The course with the highest grade is: "+highestGrade+"</li>");
  44.                 document.writeln("<li> The course with the lowest grade is: "+lowestGrade+"</li>");
  45.                 document.writeln("<li> Your Grade Point Average (GPA) is: "+GPA+"</li>");
  46.                 document.writeln("</ul>");
  47.             }  
  48.            
  49.            
  50.             //User input functions
  51.             function getCourseCode() {
  52.                 return prompt('Please enter your course code with three letters followed by four integers, with no spaces or symbols','ABC1234');
  53.             }
  54.            
  55.             function validateCourseCode(codetoValidate) {
  56.                 //Validation plan for course codes
  57.                    
  58.                 //Course code length
  59.                 var stringLength = codetoValidate.length;
  60.                
  61.                 if (stringLength < 7){
  62.                     alert('The course code you have entered is too short. Please try again');
  63.                     return false;
  64.                 }
  65.                
  66.                 if (stringLength > 7){
  67.                     alert('The course code you have entered is too long. Please try again');
  68.                     return false;
  69.                 }
  70.            
  71.                 //Course code letters
  72.                 var character = '';
  73.                 var i = 0;
  74.                
  75.                 while (i < 3){
  76.                     character = codetoValidate.charAt(i);
  77.                     if( (/[A-Z]/).test(character)==false ){
  78.                         alert('The course code, '+codetoValidate+' you have entered, contains invalid letters. Please try again');
  79.                         return false;
  80.                     }
  81.                     i++;
  82.                 }
  83.                
  84.                 //Course code numbers
  85.                
  86.                 //Constants
  87.                 const LOWRANGE = 1000;
  88.                 const HIGHRANGE = 9999;
  89.                
  90.                 //Extract the course number from the string and convert it to an integer
  91.                 var courseNum = parseInt(codetoValidate.slice(-4));
  92.                
  93.                 if (LOWRANGE > courseNum) {
  94.                     alert('The course number, '+codetoValidate.slice(-4)+' you have entered, is not above '+LOWRANGE+'. Please try again');
  95.                     return false;
  96.                 }
  97.                
  98.                 if (HIGHRANGE < courseNum) {
  99.                     alert('The course number, '+codetoValidate.slice(-4)+' you have entered, is not below '+HIGHRANGE+'. Please try again');
  100.                     return false;
  101.                 }
  102.                
  103.                 //If we got this far, it's valid
  104.                 return true;
  105.             }
  106.            
  107.             function getCourseGrade() {
  108.                 //Grade value input
  109.                 return parseFloat(prompt('Please enter your course grade as a number between 3 and 7, or equal to 1.5','4'));
  110.             }
  111.            
  112.             function validateCourseGrade(gradetoValidate) {
  113.                 //Grade value validation plans
  114.                 if (gradetoValidate != 1.5 && 3 > gradetoValidate || gradetoValidate > 7 || isNaN(gradetoValidate)){
  115.                     alert('The grade you have entered is not valid. Please try again');
  116.                     return false;
  117.                 }
  118.                 //If we got this far, it's valid
  119.                 return true;
  120.             }
  121.            
  122.            
  123.             //Data Parsing functions
  124.             function getHighestGrade() {
  125.                 //Find course with highest grade
  126.                 var max = Number.NEGATIVE_INFINITY;
  127.            
  128.                 for (thisCourseIndex = 0; thisCourseIndex < courseGradeArray.length; thisCourseIndex++) {
  129.                     if (courseGradeArray[thisCourseIndex] > max)
  130.                     max = courseGradeArray[thisCourseIndex];
  131.                 }
  132.            
  133.                 return max;
  134.             }
  135.            
  136.             function getLowestGrade() {
  137.                 //Find course with lowest grade
  138.                 var min = Number.POSITIVE_INFINITY;
  139.            
  140.                 for (thisCourseIndex = 0; thisCourseIndex < courseGradeArray.length; thisCourseIndex++) {
  141.                     if (courseGradeArray[thisCourseIndex] < min)
  142.                     min = courseGradeArray[thisCourseIndex];
  143.                 }
  144.                
  145.                 return min;
  146.             }
  147.            
  148.             function getGPA() {
  149.                 //Find the average grade
  150.                 var sum = 0;
  151.                 var average = 0;
  152.                 if (courseGradeArray.length != 0) {
  153.                     for (thisCourseIndex = 0; thisCourseIndex < courseGradeArray.length; thisCourseIndex++) {
  154.                         sum += (courseGradeArray[thisCourseIndex]);
  155.                     }
  156.                     average = (sum / courseGradeArray.length);
  157.                 } else {
  158.                     alert('No courses entered');
  159.                     return false;
  160.                 }
  161.                
  162.                 return average;
  163.             }
  164.  
  165.            
  166.            
  167.            
  168.            
  169.            
  170.            
  171.            
  172.             //Run the code to generate the page
  173.            
  174.            
  175.             //Iteration for information gathering phase
  176.             var thisCourseIndex = 0;
  177.             while (confirm('Would you like to add a new course?')){
  178.                 //Initialise this entry
  179.                 var thisCourseCode = getCourseCode();
  180.                
  181.                 //Validate user input
  182.                 while (validateCourseCode(thisCourseCode) == false) {
  183.                     thisCourseCode = getCourseCode();
  184.                 }
  185.                
  186.                 //Initialise this entry
  187.                 var thisCourseGrade = getCourseGrade();
  188.                
  189.                 //Validate user input
  190.                 while (validateCourseGrade(thisCourseGrade) == false) {
  191.                     thisCourseGrade = getCourseGrade();
  192.                 }
  193.                
  194.                 //Fill arrays with validated user input
  195.                 courseCodeArray[thisCourseIndex] = thisCourseCode;
  196.                 courseGradeArray[thisCourseIndex] = thisCourseGrade;
  197.                
  198.                
  199.                 thisCourseIndex++;
  200.             }
  201.            
  202.             writeTableStart();
  203.            
  204.             for (thisCourseIndex = 0; thisCourseIndex < courseGradeArray.length; thisCourseIndex++) {
  205.                 writeTableRow(courseCodeArray[thisCourseIndex],courseGradeArray[thisCourseIndex]);
  206.             }
  207.            
  208.             writeTableEnd();
  209.            
  210.             writeSummary(courseCodeArray.length, getHighestGrade(), getLowestGrade(), getGPA());
  211.            
  212.         </script>
  213.     </head>
  214.     <body>
  215.     </body>
  216. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement