Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html>
- <title>
- Asssignment 2: GPA calculator_Madeleine Gold
- </title>
- <head> <meta charset="utf-8"/>
- <script type="text/JavaScript">
- //Declare arrays to hold data
- var courseCodeArray = new Array();
- var courseGradeArray = new Array();
- //HTML output functions
- function writeTableStart() {
- //Print table
- document.writeln("<table border = '1' >");
- //Table row 1, headings
- document.writeln("<tr>");
- document.writeln("<th>Course Code</th>");
- document.writeln("<th>Grade</th>");
- document.writeln("</tr>");
- }
- function writeTableRow(code, grade) {
- //Table rows
- document.writeln("<tr>");
- document.writeln("<td>"+code+"</td>");
- document.writeln("<td>"+grade+"</td>");
- document.writeln("</tr>");
- }
- function writeTableEnd() {
- //End table
- document.writeln("</table>");
- }
- function writeSummary(numCourses, highestGrade, lowestGrade, GPA) {
- //Output unordered list
- document.writeln("<ul>");
- document.writeln("<li> You have entered "+numCourses+" courses</li>");
- document.writeln("<li> The course with the highest grade is: "+highestGrade+"</li>");
- document.writeln("<li> The course with the lowest grade is: "+lowestGrade+"</li>");
- document.writeln("<li> Your Grade Point Average (GPA) is: "+GPA+"</li>");
- document.writeln("</ul>");
- }
- //User input functions
- function getCourseCode() {
- return prompt('Please enter your course code with three letters followed by four integers, with no spaces or symbols','ABC1234');
- }
- function validateCourseCode(codetoValidate) {
- //Validation plan for course codes
- //Course code length
- var stringLength = codetoValidate.length;
- if (stringLength < 7){
- alert('The course code you have entered is too short. Please try again');
- return false;
- }
- if (stringLength > 7){
- alert('The course code you have entered is too long. Please try again');
- return false;
- }
- //Course code letters
- var character = '';
- var i = 0;
- while (i < 3){
- character = codetoValidate.charAt(i);
- if( (/[A-Z]/).test(character)==false ){
- alert('The course code, '+codetoValidate+' you have entered, contains invalid letters. Please try again');
- return false;
- }
- i++;
- }
- //Course code numbers
- //Constants
- const LOWRANGE = 1000;
- const HIGHRANGE = 9999;
- //Extract the course number from the string and convert it to an integer
- var courseNum = parseInt(codetoValidate.slice(-4));
- if (LOWRANGE > courseNum) {
- alert('The course number, '+codetoValidate.slice(-4)+' you have entered, is not above '+LOWRANGE+'. Please try again');
- return false;
- }
- if (HIGHRANGE < courseNum) {
- alert('The course number, '+codetoValidate.slice(-4)+' you have entered, is not below '+HIGHRANGE+'. Please try again');
- return false;
- }
- //If we got this far, it's valid
- return true;
- }
- function getCourseGrade() {
- //Grade value input
- return parseFloat(prompt('Please enter your course grade as a number between 3 and 7, or equal to 1.5','4'));
- }
- function validateCourseGrade(gradetoValidate) {
- //Grade value validation plans
- if (gradetoValidate != 1.5 && 3 > gradetoValidate || gradetoValidate > 7 || isNaN(gradetoValidate)){
- alert('The grade you have entered is not valid. Please try again');
- return false;
- }
- //If we got this far, it's valid
- return true;
- }
- //Data Parsing functions
- function getHighestGrade() {
- //Find course with highest grade
- var max = Number.NEGATIVE_INFINITY;
- for (thisCourseIndex = 0; thisCourseIndex < courseGradeArray.length; thisCourseIndex++) {
- if (courseGradeArray[thisCourseIndex] > max)
- max = courseGradeArray[thisCourseIndex];
- }
- return max;
- }
- function getLowestGrade() {
- //Find course with lowest grade
- var min = Number.POSITIVE_INFINITY;
- for (thisCourseIndex = 0; thisCourseIndex < courseGradeArray.length; thisCourseIndex++) {
- if (courseGradeArray[thisCourseIndex] < min)
- min = courseGradeArray[thisCourseIndex];
- }
- return min;
- }
- function getGPA() {
- //Find the average grade
- var sum = 0;
- var average = 0;
- if (courseGradeArray.length != 0) {
- for (thisCourseIndex = 0; thisCourseIndex < courseGradeArray.length; thisCourseIndex++) {
- sum += (courseGradeArray[thisCourseIndex]);
- }
- average = (sum / courseGradeArray.length);
- } else {
- alert('No courses entered');
- return false;
- }
- return average;
- }
- //Run the code to generate the page
- //Iteration for information gathering phase
- var thisCourseIndex = 0;
- while (confirm('Would you like to add a new course?')){
- //Initialise this entry
- var thisCourseCode = getCourseCode();
- //Validate user input
- while (validateCourseCode(thisCourseCode) == false) {
- thisCourseCode = getCourseCode();
- }
- //Initialise this entry
- var thisCourseGrade = getCourseGrade();
- //Validate user input
- while (validateCourseGrade(thisCourseGrade) == false) {
- thisCourseGrade = getCourseGrade();
- }
- //Fill arrays with validated user input
- courseCodeArray[thisCourseIndex] = thisCourseCode;
- courseGradeArray[thisCourseIndex] = thisCourseGrade;
- thisCourseIndex++;
- }
- writeTableStart();
- for (thisCourseIndex = 0; thisCourseIndex < courseGradeArray.length; thisCourseIndex++) {
- writeTableRow(courseCodeArray[thisCourseIndex],courseGradeArray[thisCourseIndex]);
- }
- writeTableEnd();
- writeSummary(courseCodeArray.length, getHighestGrade(), getLowestGrade(), getGPA());
- </script>
- </head>
- <body>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement