The dice game on Lesson 8 create your own SWITCH


#1

Dear friends, Jerry's here, and I am a new learner,

I try to create a game by using switch statement to deliver different outcomes according to the dice is thrown. However, the results all jumped back to default. Can you help to show me where I was wrong? Even with or without the '' on every case, the result are still the same. I just can't get it. Thanks in advance.

var diceNum = Math.ceil(Math.random());
var diceResult = Math.ceil(diceNum*6+1);
switch (diceResult) {
case diceResult<= 3:
console.log("You win a puny fog!");
break;
case diceResult<= 5:
console.log("You win a fat girl!");
break;
case diceResult===6:
console.log("You win a princess and become King!");
break;
default:
console.log("Do you even DICE brah?!");
break;
}


#2

@devwhiz88669,
You got 2 problems

1 the evaluation of the diceResult using the Math.Random() strategy
2 the handling of your switch()

1

the evaluation of the diceResult using the Math.Random() strategy

 Math.Random()                     ==> 0 ... 0.9999
 (Math.Random() * 6)               ==> 0 ... 5.9994
 (Math.Random() * 6 + 1)           ==> 1 ... 6.9994
 Math.floor(Math.Random() * 6 + 1) ==> 1 ... 6

= http://stackoverflow.com/questions/15830658/random-number-math-floor-vs-math-ceil

2

the handling of your switch()

In
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch
[quote]

switch (parameter-expression) {
  case value1:
    //Statements executed 
    //      when the VALUE of expression matches value1
    [break;]
  case value2:
     //Statements executed 
    //      when the VALUE of expression matches value2
    [break;]
 ...
  case valueN:
    //Statements executed 
    //      when the VALUE of expression matches valueN
    [break;]
  default:
    //Statements executed 
    //      when none of the VALUE's match the VALUE of the expression
    [break;]
}

= = = = = = = = = = = = = = = = = = = = = = = = = = = =

The switch statement evaluates an parameter-expression,
matching the parameter-expression's VALUE to a case clause,
and executes statements associated with that case.
( and with the break or return-statement will exit the switch statement )
[end-quote]

= = = = = = = = = = = = = = = = = = = = = = = = = = = =

In your case clause you are using an expression
An expression is any valid unit of code that resolves to a VALUE.

Conceptually, there are two types of expressions:
-1 those that assign a value to a variable
and
2- those that simply resolve to a VALUE.

So in your case clause you are using the 2nd kind of expression
and you do that by
using the relational operator "<=" and the comparison operator "==="
on the variable diceResult
which will result in a Boolean VALUE

Thus your case clause VALUE
is matched against the VALUE of the parameter-expression

So if use a Boolean VALUE of true as
being the parameter-expression's VALUE
would get the -result- we want:

switch ( true ) {
 case diceResult<= 3:
      console.log("You win a puny fog!");
      break;
 case diceResult<= 5:
      console.log("You win a fat girl!");
      break;
 case diceResult===6:
      console.log("You win a princess and become King!");
      break;
 default:
     console.log("Do you even DICE brah?!");
     //break; NOT needed !!!
}

#3

Thank you a lot for explain my case in such detail. I manage to understand the problem after a few read through. Thank a lot for your time and effort ! Wish you have an awesome day !