I need to check for equality with a switch statement

175 views Asked by At

I'm writing a hot or cold app. and I need a switch statement to decide whether the user is hot or cold. Here's my code so far:

switch(true) {
         case (userGuess / secretNumber === 1):
          setFeedback("You win");
             finish = true;
             break;
            case ((userGuess - secretNumber) > 50):
             setFeedback("You're ice cold!");
             break;
            case ((userGuess - secretNumber) > 30):
             setFeedback("You're cold!");
             break;
            case ((userGuess - secretNumber) > 20):
             setFeedback("You're warm!");
             break;
            case ((userGuess - secretNumber) > 10):
             setFeedback("You're hot!");
             break;
            case ((userGuess - secretNumber) > 0):
             setFeedback("You're blazing hot!");
             break;
        }

Edit: I'm trying to replace the following if/else statement with a switch. Please tell me if it's possible and if so: how. Here's the old code:

if (userGuess / secretNumber === 1){
            setFeedback("You win");
            finish = true;
          } else if ((secretNumber - userGuess) > 60.5){
            setFeedback("Wow! You are freezing!");
            } else if ((secretNumber - userGuess) > 55.5){
            setFeedback("Wow! You better put on a jacket cause its super cold!");
            } else if ((secretNumber - userGuess) > 50.5){
            setFeedback("Its is super cold man!");
            } else if ((secretNumber - userGuess) > 40.5) {
            setFeedback("Now you are cold!");
            } else if ((secretNumber - userGuess) > 30.5) {
            setFeedback("It's getting warm around here");
            } else if((secretNumber - userGuess) > 20.5) {
            setFeedback("It's getting very warm in here!");
            } else if((secretNumber - userGuess) > 15.5) {
            setFeedback("It's getting very very warm in here!");
            } else if ((secretNumber - userGuess) > 7.5){
            setFeedback("It's hot!");
            } else if ((secretNumber - userGuess) > 5.5){
            setFeedback("It is very hot here!");
           } else if ((secretNumber - userGuess) > 1.5){
            setFeedback("I am burning here!!");
            } else if ((secretNumber - userGuess) > 0.5){
            setFeedback("Its is hotter than the sun!!!!!");
        } else {
        }

1

There are 1 answers

0
FirebladeDan On
       int value = secretNumber-userGuess;

       if ((userGuess / secretNumber) == 1)
        {
          setFeedback("You win");
          finish = true;
        } 
        else if (value > 60.5)
          setFeedback("Wow! You are freezing!");
        else if (value > 55.5)
          setFeedback("Wow! You better put on a jacket cause its super cold!");
        else if (value > 50.5)
          setFeedback("Its is super cold man!");
        else if (value > 40.5) 
          setFeedback("Now you are cold!");
        else if (value > 30.5) 
          setFeedback("It's getting warm around here");
        else if (value > 20.5) 
          setFeedback("It's getting very warm in here!");
        else if(value > 15.5) 
          setFeedback("It's getting very very warm in here!");
        else if (value > 7.5)
          setFeedback("It's hot!");
        else if (value > 5.5)
          setFeedback("It is very hot here!");
        else if (value > 1.5)
          setFeedback("I am burning here!!");
        else if (value > 0.5)
          setFeedback("Its is hotter than the sun!!!!!");
     else {
    }