-5

This is my javascript code which is supposed to show: Okay! Great choice We are working hard for your cake if I enter cake or Cake (notice c with capital) but working nice with cake but not with Cake and same goes with pancake and capital p Pancake:

var make=prompt("Hey! What you are up to cake or pancake?");
switch(make){
    case ("cake" || "Cake"):
    console.log("Okay! Great choice  We are working hard for your cake");
    break;
    case ("pancake" || "Pancake"):
        console.log("Okay! Great choice   We are working hard for your pancake");
    break;
    default:
    console.log("Sorry we Only have cake or pancake to choose from.");
}
  • The logical OR returns its first operand which can be evaluated as truthy, hence you're cases are always the same, non-capitalized versions only. – Teemu Sep 07 '18 at 12:19
  • Could you please explain what your issues are and what steps you've taken in trying to solve them. – bork Sep 07 '18 at 12:19
  • Note that you can use `make.toLowerCase()` and only check for "cake" as well if you don't want to use fallthrough – Stefan Blamberg Sep 07 '18 at 12:22
  • Yap it as i type Cake(c with capital one) it display me Sorry we Only have cake or pancake to choose from.but it suppose to display Okay! Great choice We are working hard for your cake – charanPreet Sep 07 '18 at 12:22

3 Answers3

0

The || operator evaluates as the left hand side if the left hand side is a true value, otherwise it evaluates as the right hand side.

So "cake" || "Cake", because "cake" is a true value, means "cake".

Thus case ("cake" || "Cake"): means case ("cake"):.


If you want to have multiple matches in a switch, have multiple cases.

case ("cake"):
case ("Cake"):
    // etc
    break;
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

The expression "cake" || "Cake" evaluates to true, because both those strings are truthy. So when user's input is compared with that value, for example "cake" == true, it evaluates to true because the user's input ("cake") is also truthy.

To ignore the case of user's input, you can simply convert it to lowercase (by make.toLowerCase()) before comparing:

var make = prompt("Hey! What you are up to cake or pancake?");
switch (make.toLowerCase()) {
  case "cake":
    console.log("Okay! Great choice  We are working hard for your cake");
    break;
  case "pancake":
    console.log("Okay! Great choice   We are working hard for your pancake");
    break;
  default:
    console.log("Sorry we Only have cake or pancake to choose from.");
}

If you are not familiar with the concept of truthy or falsy values, you can read MDN's documentation here: https://developer.mozilla.org/en-US/docs/Glossary/Truthy & https://developer.mozilla.org/en-US/docs/Glossary/falsy

Nisarg Shah
  • 14,151
  • 6
  • 34
  • 55
  • 2
    `ToUppercase` is always prefered, please see: https://learn.microsoft.com/en-gb/visualstudio/code-quality/ca1308-normalize-strings-to-uppercase?view=vs-2015 – Rafael Herscovici Sep 07 '18 at 12:25
0

Invariant comparison:

var make = prompt("Hey! What you are up to cake or pancake?");

if (make.localeCompare("cake".toUpperCase()) == -1) {
  console.log("Okay! Great choice  We are working hard for your cake");
} else if (make.localeCompare("pancake".toUpperCase()) == -1) {
  console.log("Okay! Great choice   We are working hard for your pancake");
} else {
  console.log("Sorry we Only have cake or pancake to choose from.");
}
Rafael Herscovici
  • 16,558
  • 19
  • 65
  • 93