//COMPUTER SCIENCE 161.01 //MONDAY NOVEMBER 10TH 2014 //SEQUENTIAL SEARCH (a.k.a. LINEAR SEARCH) //THE PROGRAM BELOW PERFORMS THE SEQUENTIAL SEARCH ALGORITHM //IT IS ALSO KNOWN AS THE 'LINEAR' SEARCH ALGORITHM //BECAUSE IT REQUIRES AT MOST N COMPARISONS WHERE N IS SIZE OF LIST //HERE, N is 15. lastNames = new Array("Morgan", "Westerhoff", "Lilley", "Duling", "Welch", "Adair", "Perkins", "Reed", "Ingersoll", "Deyerle", "James", "Hickman", "Hughes", "O'Brien", "Moon"); firstNames = new Array("Wood", "Ben", "John", "Cody", "Spencer", "James", "Zach", "Whitaker", "Jamie", "Evan", "Christopher", "Jesse", "Preston", "Conor", "Thomas"); i=0; desiredName = prompt("Enter the last name of the person you wish to greet:"); while (i < lastNames.length) { if (lastNames[i] == desiredName) //IF CURRENT lastName MATCHES desiredName alert("Happy Monday to " + firstNames[i]); //THEN send a friendly greeting using firstName i++; } alert("ALL DONE"); //WE NEED TO IMPROVE THIS, BECAUSE THE ABOVE PROGRAM ALWAYS MAKES 15 COMPARISONS //EVEN IF WE WERE LOOKING FOR Starr. //1) I WANT TO NOT MAKE ANY UNNECESSARY COMPARISONS ONCE I FIND A MATCH //2) AS A SECOND IMPROVEMENT, LET'S REPORT WHETHER OR NOT // THE desired NAME WAS FOUND lastNames = new Array("Morgan", "Westerhoff", "Lilley", "Duling", "Welch", "Adair", "Perkins", "Reed", "Ingersoll", "Deyerle", "James", "Hickman", "Hughes", "O'Brien", "Moon"); firstNames = new Array("Wood", "Ben", "John", "Cody", "Spencer", "James", "Zach", "Whitaker", "Jamie", "Evan", "Christopher", "Jesse", "Preston", "Conor", "Thomas"); i=0; desiredName = prompt("Enter the last name of the person you wish to greet:"); found = false; while ( !found && i < lastNames.length) { if (lastNames[i] == desiredName) { //IF CURRENT lastName MATCHES desiredName alert("Happy Monday to " + firstNames[i]); //THEN send a friendly greeting using firstName found = true; } i++; } if (!found) alert("Could not locate " + desiredName); alert("ALL DONE");