A Little stuck on this problem, I think I have the switch statement not in working condition. This is the prompt:
1) Use a sentinel while loop that only exits when the number zero (0) is read.
2) Inside the sentinel while loop
2.a) Read a musketeer number. You can assume this is a unique serial number printed on the musketeer's uniform.
2.b) Look up the table below, find a matching name with a JavaScript switch statement, and then save the name to a variable.
2.c) Print out the matching name with either innerHTML or document.write(), with proper prompt.
2.d) Give an error message if no match can be found in the table below for a given musketeer number.
Now here was my code:
var idnum;
var input;
input = window.prompt("Enter a Uniform ID (0 to quit): ");
idnum = parseInt(input);
function getData(rNum, cNum) {
var table = document.getElementById('musketeer');
var rowElem = table.rows[rNum];
var tdValue = rowElem.cells[cNum].innerHTML;
var match = parseInt(tdValue);
return match;
}
while (idnum != 0) {
switch (idnum) {
case 1001 === getData(2, 0):
document.writeln("That is Musketeer D'Artagnan");
break;
case 2034 === getData(3, 0):
document.writeln("That is Musketeer Athos");
break;
case 2178 === getData(4, 0):
document.writeln("That is Musketeer Aramis");
break;
case 1091 === getData(5, 0):
document.writeln("That is Musketeer Porthos");
break;
default:
document.writeln("That ID does not match a musketeer.");
}
}
<html>
<head>
<title>Lab 9</title>
</head>
<body>
<table id='musketeer' border=1 cellpadding=0 cellspacing=0>
<tr>
<td><strong>Musketeer Number</strong></td>
<td><strong>Musketeer Name</strong></td>
</tr>
<tr>
<td>1001</td>
<td>D'Artagnan</td>
</tr>
<tr>
<td>2034</td>
<td>Athos</td>
</tr>
<tr>
<td>2178</td>
<td>Aramis</td>
</tr>
<tr>
<td>1091</td>
<td>Porthos</td>
</tr>
</table>
</body>
</html>
idnuma boolean value? if not, you will end up in thedefaultbranch at all times. Why not store the data in a Set, so you can do a simple look up against the set? btw, what a horrible schoolwork task are they giving you, prompts, while loops, document.write, that teacher should really refresh on what js can do...switch(idnum) { case getData(2,0): ... break; case getData(3,0): ... break; ...}.idnumto Boolean values in the case statements...