2

I have made a little test program to show the user what bonus they get from having a certain value. The case statements should function as an OR gate. But the problem shows up when the alert should show up on screen but does not.

I have changing a few values, but that did not work. Also new to using JavaScript so I do not know what I can do as of right now.

var strength = prompt("What is the value of strength?");

switch (strength) {
    case 10 : 
    case 11 : 
        alert("+0");
    break;

    case 12 : 
    case 13 : 
        alert("+1");
    break;

    case 14 : 
    case 15 : 
        alert("+2");
    break;

    case 16 :
    case 17 :
        alert("+3");
    break;

    case 18 :
    case 19 :
        alert("+4");
    break;

    case 20 :
    case 21 :
        alert("+5");
    break;

    default : ("Please enter a value between 10 and 20");
}

When the user enters the value of strength the bonus of that value should be showing up.

Example: strength = 12 alert("+1") The alert does not show up though

Nemus
  • 3,879
  • 12
  • 38
  • 57
D. White
  • 21
  • 1
  • 6
    well `"12" !== 12` – epascarello Jan 04 '19 at 15:22
  • 1
    To add to what @epascarello is saying, check [this answer](https://stackoverflow.com/a/6989959/965834). In short, you'll have to convert your value to an integer first, or test (case) with strings. – Jeto Jan 04 '19 at 15:23
  • The default is not showing an alert: `default : ("Please enter a value between 10 and 20");` should be `default : alert("Please enter a value between 10 and 20");` – scott.korin Jan 04 '19 at 15:27
  • use `parseInt()` to convert prompt value to Integer – Feelsbadman Jan 04 '19 at 15:30

3 Answers3

5

Prompt returns a string and you are looking for numbers.

var a = 10

switch (a) {
  case 10:
    console.log("a - yes");
    break;
  default:
    console.log("a - no");
}

var b = "10"

switch (b) {
  case 10:
    console.log("b - yes");
    break;
  default:
    console.log("b - no");
}

So either you change your switch to be strings, or you change your prompt to be a number. Many ways to do it.

var strength = Number(prompt("What is the value of strength?"));
var strength = parseInt(prompt("What is the value of strength?"));
var strength = +prompt("What is the value of strength?");
epascarello
  • 204,599
  • 20
  • 195
  • 236
0

Just convert strength to an integer:

var strength = parseInt(prompt("What is the value of strength?"));

switch (strength) {
    case 10 : 
    case 11 : 
        alert("+0");
    break;

    case 12 : 
    case 13 : 
        alert("+1");
    break;

    case 14 : 
    case 15 : 
        alert("+2");
    break;

    case 16 :
    case 17 :
        alert("+3");
    break;

    case 18 :
    case 19 :
        alert("+4");
    break;

    case 20 :
    case 21 :
        alert("+5");
    break;

    default : ("Please enter a value between 10 and 20");
}
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
0

You are trying to compare a string strength to an int value e.g:10 and this is because prompt is reading as string.

quote from w3schools:

(Returns) a String.
If the user clicks "OK", the input value is returned.
If the user clicks "cancel", null is returned.
If the user clicks OK without entering any text, an empty string is returned.

What you can do is to use parseInt() function to convert the input to an int like so:

var strength = parseInt(prompt("your message: "));

Here's a short modified version:

var strength = parseInt(prompt("What is the value of strength?"));

switch (strength) {
 case 11:
  console.log("11");
 break;

 case 12:
 case 13:
  console.log("+1");
 break;

 case 14:
 case 15:
  console.log("+2");
 break;
 default : ("Please enter a value between 10 and 20");
}
Feelsbadman
  • 1,163
  • 4
  • 17
  • 37