Hello again.
I have an assignment that involves inputting the firstName, lastName and score of a student with the values being added to a textbox for display.
The problem is the last part of the assignment involves getting the last names sorted alphabetically which involves the use of an array.
My classmate has a solution but I feel it is way too complicated compared to the supposed "proper" way of doing it
Below is the Javascript code with a very lazy attempt at sorting the array. Any pointers would be appreciated.
var $ = function ()
{
return document.getElementById(arguments[0]);
}
var avg = 0;
var count = 1;
var array = [];
window.onload = function()
{
$("addScore").onclick = addScore_click;
$("clear").onclick = clear_click;
$("sort").onclick = sort_click;
}
var addScore_click = function()
{
var lastName = $("lastname").value;
var firstName = $("firstname").value;
var score = parseInt ($("score").value);
avg += score;
if(score < 100)
{
var res = lastName+ "," +firstName+ "," +score+"\n";
$("t1").value += res;
$("average").value = avg / count;
count ++;
array[array.length] += $("lastname").value,$("firstname").value,($("score").value);
}
else
{
alert("Please enter a score less than 100");
}
}
var clear_click = function()
{
$("t1").value = "";
$("average").value = "";
}
var sort_click = function()
{
array.sort();
}