//CLASS EXAMPLES FROM NOVEMBER 5, 2014 //EXAMPLE 1 list = new Array(19, 7, 25, 60, 40, 12); list[0] //TRY // list[1] // list[2] // list[3] > 20 // list[4] + 1 // list[4+1] // list.length // TRY CHANGING THE LENGTH // //HOW DO YOU GET IT TO TELL (always) YOU THE LAST NUMBER? //EXAMPLE 2 names = new Array("Tom", "Seymour", "Bob", "Alice", "Ted"); names[0] //TRY OTHERS // TRY // names.length // names[0].length // // HOW DO I GET IT TO VISIT EACH NAME AND SAY // Tom is great! Seymour is great! ... Ted is great! // This should work REGARDLESS of how many names I have // // HOW DO I GET IT TO COUNT HOW MANY NAMES HAVE LENGTH 3? i = 0; count = 0; while (i < names.length) { alert(names[i] + " is great!"); //say that for current name if (names[i].length == 3) //count how many have length 3 count++; i++; //move right ... to next name } alert("There were " + count + " names of length 3"); //BACK TO EXAMPLE 1 list = new Array(19, 7, 25, 60, 40, 12); //WE WANT TO WRITE A PROGRAM THAT SUMS THE NUMBERS //AND REPORTS THE SUM AT THE END WITH AN ALERT //HOW DO WE REPORT THE AVERAGE INSTEAD? //This must work regardless of how many numbers we have! i = 0; sum = 0; while (i < list.length) { sum += list[i]; // increase sum by current list item i++; //move right ... to next number } alert("The sum is " + sum); alert("The average is " + sum/list.length);