0

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>

3
  • 2
    I really fail to see why you need a switch statement in your case. A tip might be is idnum a boolean value? if not, you will end up in the default branch 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... Commented Feb 21, 2018 at 1:10
  • If getData works you could try something like switch(idnum) { case getData(2,0): ... break; case getData(3,0): ... break; ...}. Commented Feb 21, 2018 at 1:18
  • Not sure what are you doing, but you are comparing idnum to Boolean values in the case statements... Commented Feb 21, 2018 at 1:23

3 Answers 3

1

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);
}
Sign up to request clarification or add additional context in comments.

Comments

0

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);
  } 
 }

Comments

0
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

1 Comment

Thanks for the feedback!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.