Uikithemes
Uikithemes

Reputation: 27

About Swtich Statments in JavaScript

I write this code to try my idea and is working fine :

var answer = prompt("Please type your gender? male / female");
var gender = answer;
switch (gender) {
  case "female":
    console.log("Hello, madame!");
    break;
  case "male":
    console.log("Hello, sir!");
    break;
  default:
    console.log("Hello!");
}

But the problem for example when someone add answer with uppercase like "MALE" or Capital Letter like "Male" so this code is not working. any idea about that to get working in any statments of words ( Lowercase / Uppercase / Capital Letter )

Upvotes: 0

Views: 72

Answers (2)

Retr0id
Retr0id

Reputation: 350

Use

var gender = answer.toLowerCase();

Also, please indent your code in future.

Upvotes: 1

Felix Kling
Felix Kling

Reputation: 816780

Convert the input to lower case:

switch (gender.toLowerCase()) {

Upvotes: 4

Related Questions