In: Computer Science
3.32 Prog4-Branches-Listing names A university has a web page that displays the instructors for a course, using the following algorithm: If only one instructor exists, the instructor's first initial and last name are listed. If two instructors exist, only their last names are listed, separated by /. If three exist, only the first two are listed, with "/ …" following the second. If none exist, print "TBD". Given six words representing three first and last names (each name a single word; latter names may be "none none"), output one line of text listing the instructors' names using the algorithm. If the input is "Ann Jones none none none none", the output is "A. Jones". If the input is "Ann Jones Mike Smith Lee Nguyen" then the output is "Jones / Smith / …". Hints: Use an if-else statement with four branches. The first detects the situation of no instructors. The second one instructor. Etc. Detect whether an instructor exists by checking if the first name is "none".
<!DOCTYPE html>
<html>
<body>
<h2>Instructors for Course IE9685</h2>
<p id="displayInstructors"></p>
<script>
var input = ""; //Output: TBD
var input = "Ann Jones none none none none"; //Output: A. Jones
var input = "Ann Jones"; //Output: A. Jones
var input = "Ann Jones Mike Smith none none"; //Output: Jones / Smith
var input = "Ann Jones Mike Smith"; //Output: Jones / Smith
var input = "Ann Jones Mike Smith Lee Nguyen"; //Output: Jones / Smith /...
// Checks whether an instructor exists by "none"
var Instructors = input.split(" ");
while(Instructors.indexOf("none") != -1){
Instructors.splice(Instructors.indexOf("none"), 1);
}
var text = "";
// If-else tatement to check the number of instructors
if (Instructors.length == 1){
text = "TBD";
} else if (Instructors.length == 2) {
text += Instructors[0][0] + ". " + Instructors[1];
} else if (Instructors.length == 4) {
text += Instructors[1] + " / " + Instructors[3];
} else if (Instructors.length == 6){
text += Instructors[1] + " / " + Instructors[3] + " /...";
}
// Used to display the instructors for a course in the webpage
var element = document.getElementById("displayInstructors");
element.innerHTML = text;
</script>
</body>
</html>