Reputation: 323
So I was coding a basic program to play rock paper scissors with the user, and one of my switches wasn't cooperating. The code is below:
console.log("start rokpaperscissors")
var userString = prompt("Do you choose rock, paper, or scissors")
var computerRandom = Math.random()
//convert computerRandom into value and string
switch(computerRandom) {
case computerRandom < 0.33: computerString = "rock"; computerValue = 2
break;
default: computerString = "paper"; computerValue = 4
break;
case computerRandom > 0.66: computerString = "scissors"; computerValue = 6
break;
}
//convert userString into value
switch(userString) {
case "rock": userValue = 2
break;
case "paper": userValue = 4
break;
case "scissors": userValue = 6
break;
default: console.log("debug @ line 12")
}
switch(userValue) {
//if user wins, this code should run
case userValue > computerValue || computerValue === 6 && userValue === 2:
document.write("computer's choice: ")
document.write(computerString)
document.write("<br/>")
document.write("your choice: ")
document.write(userString)
document.write("<br/>")
document.write("you win")
break;
//if user loses, this code should run
case userValue < computerValue || computerValue === 2 && userValue === 6:
document.write("computer's choice:")
document.write(computerString)
document.write("<br/>")
document.write("your choice: ")
document.write(userString)
document.write("<br/>")
document.write("sorry, you lose")
break;
//if userValue === computerValue, they tie and this code runs
case computerValue:
document.write("computer's choice: ")
document.write(computerString)
document.write("<br/>")
document.write("your choice: ")
document.write(userString)
document.write("<br/>")
document.write("tie game")
break;
default: console.log("debug @ line 22")
}
On the final switch, which is meant to display the outputs, the code always jumps to the default code and prints the debug @ line 22 message. I suspect this is due to the complex operations in the "case" line. Are these operations not possible in the switch, and if so, what are my other options? If they are allowed, are they formatted wrong?
Thanks for all your help
I solved the problem by combining an "if-else if" statement to define win, lose, and tie, with a switch statement to output results: here is my final code from line 23 down (the top 22 lines are the same)
//define win, lose, and tie cases as such
if(userValue === computerValue) {var resultString = "tie"}
else if(userValue > computerValue || computerValue === 6 && userValue === 2) {var resultString = "win"}
else if(userValue < computerValue || computerValue === 2 && userValue === 6) {var resultString = "lose"}
else {console.log("debug @ line 56")}
//output result
switch(resultString) {
case "win":
document.write("computer's choice: ")
document.write(computerString)
document.write("<br/>")
document.write("your choice: ")
document.write(userString)
document.write("<br/>")
document.write("you win")
break;
case "lose":
document.write("computer's choice: ")
document.write(computerString)
document.write("<br/>")
document.write("your choice: ")
document.write(userString)
document.write("<br/>")
document.write("sorry, you lose")
break;
case "tie":
document.write("computer's choice: ")
document.write(computerString)
document.write("<br/>")
document.write("your choice: ")
document.write(userString)
document.write("<br/>")
document.write("tie game")
break;
}
Thanks to jdigital, Matthew Booth, and Matthew Lock for helping on this issue. If you have any questions about this, I think you can still comment and I will do my best to respond.
Thanks--Hooded Gryphon
Upvotes: 1
Views: 6104
Reputation: 31
You could basically swap out your switch value to true so the result you get from your conditions match the value and the specific case can apply e.g.:
switch(true){
case userValue < computerValue || computerValue === 2 && userValue === 6:
console.log("case x is true");
break;
}
==> if this case is true the console.log will apply
Upvotes: 3
Reputation: 1
The switch
statement cannot take on ranges. The switch
case label must be a constant of an integer (also including "char" or string). Your case labels are taking on what a switch
statement is not designed for, floating point values and the comparison of two different variables. The case label must be a constant.
Upvotes: 0
Reputation: 71
edit: userValue is type int. the cases in your switch are type Boolean. Refactor that switch statement so that the cases use the same var type as the switch parameter.
edit: unrelated to initial problem but still useful
Try breaking out the comparison operators to new cases. Here's a reference: multiple cases
Here's the code from that post for reference:
switch (varName)
{
case "afshin":
case "saeed":
case "larry":
alert('Hey');
break;
default:
alert('Default case');
break;
}
Additionally, and this is a guess, does wrapping the multiple comparisons in parenthesis help?
(userValue > computerValue || computerValue === 6 && userValue === 2)
Upvotes: 1