www.webdeveloper.com
Results 1 to 3 of 3

Thread: Noobie

  1. #1
    Join Date
    Nov 2009
    Posts
    4

    Noobie

    Hi, so I'm completely new to Javascript and have a horrible teacher who wants us to make a program using "if" statements to enter in three grades and have the program remove the highest number and then average the remaining two numbers. Any help would be appreciated on how to do this.

  2. #2
    Join Date
    Apr 2006
    Location
    Silicon Valley
    Posts
    20
    That does sound like a horrible teacher because that assignment does not require if statements in order to write sufficient Javascript to satisfy the requirements.

    Here is a solution that delivers the requirements without any if statements:

    Code:
    var grades = new Array();
    grades[0] = parseInt(prompt('Enter grade of first student:'));
    grades[1] = parseInt(prompt('Enter grade of second student:'));
    grades[2] = parseInt(prompt('Enter grade of third student:'));
    grades.sort(); // grades[2] is now the highest grade
    alert('Highest grade is: '+grades[2]);
    var average = (grades[0] + grades[1]) / 2;
    alert('Average of the other two grades is: '+average);
    But whatever, here is a version with some superfluous if statements thrown in for fun.

    Code:
    var grades = new Array();
    grades[0] = parseInt(prompt('Enter grade of first student:'));
    grades[1] = parseInt(prompt('Enter grade of second student:'));
    grades[2] = parseInt(prompt('Enter grade of third student:'));
    grades.sort(); // grades[2] is now the highest grade
    alert('Highest grade is: '+grades[2]);
    var average = (grades[0] + grades[1]) / 2;
    alert('Average of the other two grades is: '+average);
    
    if (grades[0] >= 65 && grades[1] >= 65 && grades[2] >= 65) {
    	alert('All students received passing grades. :)');
    }
    else if (grades[0] < 65 && grades[1] < 65 && grades[2] < 65) {
    	alert('All students received failing grades. :(');
    }
    else {
    	alert('Some students failed and some students passed. :|');
    }

  3. #3
    Join Date
    Nov 2009
    Posts
    4
    This is perfect, thats exactly what I needed. Thank you.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles