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
--Hooded Gryphon
-------------------edit----------------------------------------------------------------
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