Reputation: 3
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>
Upvotes: 0
Views: 142
Reputation: 61
case 1001 === getData(2, 0):
is not valid. It literally means "the case when 1001 is the same as getData(2,0)". (which it's either true or false).
try,
case getData(2, 0):
this sets the value from your table as one of the switch cases. see this working example
Upvotes: 0
Reputation: 4569
To dynamically solve this, you should loop over the table, compare the input to the first column, and if that matches display the result from the second column, like
var table = document.getElementById('musketeer');
for(i=0; i<table.rows.length; i++) {
if(idnum==table.rows[i].cells[0].innerHtml){
alert("That is Musketeer " + table.rows[i].cells[1].innerHtml);
}
}
Upvotes: 0
Reputation: 3760
That's not how a switch
statement works, you can't compare a case
value to something. You should write it like this:
switch(number) {
case 1:
// do something
break;
case 2:
// do something else
break;
default:
// do default
break;
}
As for your program, your prompt should be inside the while loop so it repeatedly asks for a number until the user wants to quit.
Something like this:
var input = window.prompt("Enter a Uniform ID (0 to quit): ");
var idnum = parseInt(input);
while(number != 0) {
// use the number to perform a lookup
var name;
switch(idnum) {
case 1001:
// return the name NOT THE NUMBER
name = getData(2, 1);
break;
case 2034:
name = getData(3, 1);
break;
default:
name = "";
break;
}
// print out the name, or error (or you can write it to the page)
if(name != "") {
console.log("That is Musketeer " + name);
} else {
console.log("No Musketeer found with Uniform ID " + idnum);
}
// prompt for another number
input = window.prompt("Enter a Uniform ID (0 to quit): ");
idnum = parseInt(input);
}
Upvotes: 1