Trying to get this switch code to work with true false

let userName = ‘’;
userName ? console.log(Hello, ${userName}! ) : console.log(‘Hello!’)

let userQuestion = ‘’

switch (userName, userQuestion) {
case userName === true && userQuestion === true :
console.log(${userName} wants too know...${userQuestion});
break;
case userName === false && userQuestion === true :
console.log(The User has wants to know...${userQuestion});
break;
case userName === true && userQuestion === false :
console.log(${userName} you need to ask a question!);
break;
deafult:
console.log(‘You have not asked a question.’);
break;
}

let randomNumber = Math.floor(Math.random() * 8)
let eightBall = ‘’
switch (randomNumber) {
case 0 :
console.log(‘It is certain’);
break;
case 1 :
console.log(‘It is decidedly so’);
break;
case 2 :
console.log(‘Reply hazy try again’);
break;
case 3 :
console.log(‘Cannot predict now’);
break;
case 4 :
console.log(‘Do not count on it’);
break;
case 5 :
console.log(‘My sources say no’);
break;
case 6 :
console.log(‘Outlook not so good’);
break;
default:
console.log(‘Signs point to yes’);
break;
}

Its the 8 Ball project, and i just wanted to add the extra practice in but i dont understand how to make the switch statement work in this case, ive tried multiple placements and operations, etc.

let userName = ‘’;
userName ? console.log(Hello, ${userName}! ) : console.log(‘Hello!’)

let userQuestion = ‘’

switch (userName, userQuestion) {
case true && true :
console.log(${userName} wants too know...${userQuestion});
break;
case false && true :
console.log(The User has wants to know...${userQuestion});
break;
case true && false :
console.log(${userName} you need to ask a question!);
break;
deafult:
console.log(‘You have not asked a question.’);
break;
}

let randomNumber = Math.floor(Math.random() * 8)
let eightBall = ‘’
switch (randomNumber) {
case 0 :
console.log(‘It is certain’);
break;
case 1 :
console.log(‘It is decidedly so’);
break;
case 2 :
console.log(‘Reply hazy try again’);
break;
case 3 :
console.log(‘Cannot predict now’);
break;
case 4 :
console.log(‘Do not count on it’);
break;
case 5 :
console.log(‘My sources say no’);
break;
case 6 :
console.log(‘Outlook not so good’);
break;
default:
console.log(‘Signs point to yes’);
break;
}

Hello @script7492232300, welcome to the forums! To do conditions within a switch, you can write:

switch (true){
   case 4 === 4:
     console.log("Stuff")
}

If you want a false condition, you can write:

switch (true){
  case !(false)://evaluates to true
    console.log("Stuff");
}

Hopefully this has given you some ideas

ohhhhh, i get it, how did i not realize this. Thank you that helps alot!

1 Like