admin管理员组文章数量:1026087
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 puterRandom = Math.random()
//convert puterRandom into value and string
switch(puterRandom) {
case puterRandom < 0.33: puterString = "rock"; puterValue = 2
break;
default: puterString = "paper"; puterValue = 4
break;
case puterRandom > 0.66: puterString = "scissors"; puterValue = 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 > puterValue || puterValue === 6 && userValue === 2:
document.write("puter's choice: ")
document.write(puterString)
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 < puterValue || puterValue === 2 && userValue === 6:
document.write("puter's choice:")
document.write(puterString)
document.write("<br/>")
document.write("your choice: ")
document.write(userString)
document.write("<br/>")
document.write("sorry, you lose")
break;
//if userValue === puterValue, they tie and this code runs
case puterValue:
document.write("puter's choice: ")
document.write(puterString)
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 plex 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 bining 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 === puterValue) {var resultString = "tie"}
else if(userValue > puterValue || puterValue === 6 && userValue === 2) {var resultString = "win"}
else if(userValue < puterValue || puterValue === 2 && userValue === 6) {var resultString = "lose"}
else {console.log("debug @ line 56")}
//output result
switch(resultString) {
case "win":
document.write("puter's choice: ")
document.write(puterString)
document.write("<br/>")
document.write("your choice: ")
document.write(userString)
document.write("<br/>")
document.write("you win")
break;
case "lose":
document.write("puter's choice: ")
document.write(puterString)
document.write("<br/>")
document.write("your choice: ")
document.write(userString)
document.write("<br/>")
document.write("sorry, you lose")
break;
case "tie":
document.write("puter's choice: ")
document.write(puterString)
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 ment and I will do my best to respond.
Thanks--Hooded Gryphon
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 puterRandom = Math.random()
//convert puterRandom into value and string
switch(puterRandom) {
case puterRandom < 0.33: puterString = "rock"; puterValue = 2
break;
default: puterString = "paper"; puterValue = 4
break;
case puterRandom > 0.66: puterString = "scissors"; puterValue = 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 > puterValue || puterValue === 6 && userValue === 2:
document.write("puter's choice: ")
document.write(puterString)
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 < puterValue || puterValue === 2 && userValue === 6:
document.write("puter's choice:")
document.write(puterString)
document.write("<br/>")
document.write("your choice: ")
document.write(userString)
document.write("<br/>")
document.write("sorry, you lose")
break;
//if userValue === puterValue, they tie and this code runs
case puterValue:
document.write("puter's choice: ")
document.write(puterString)
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 plex 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 bining 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 === puterValue) {var resultString = "tie"}
else if(userValue > puterValue || puterValue === 6 && userValue === 2) {var resultString = "win"}
else if(userValue < puterValue || puterValue === 2 && userValue === 6) {var resultString = "lose"}
else {console.log("debug @ line 56")}
//output result
switch(resultString) {
case "win":
document.write("puter's choice: ")
document.write(puterString)
document.write("<br/>")
document.write("your choice: ")
document.write(userString)
document.write("<br/>")
document.write("you win")
break;
case "lose":
document.write("puter's choice: ")
document.write(puterString)
document.write("<br/>")
document.write("your choice: ")
document.write(userString)
document.write("<br/>")
document.write("sorry, you lose")
break;
case "tie":
document.write("puter's choice: ")
document.write(puterString)
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 ment and I will do my best to respond.
Thanks--Hooded Gryphon
Share Improve this question edited Jan 28, 2014 at 2:03 Samcfuchs asked Jan 28, 2014 at 0:48 SamcfuchsSamcfuchs 3231 gold badge3 silver badges8 bronze badges 2-
1
You'd be better off with an
if
statement here rather than aswitch
. Something likea > b
will return a boolean, which is never going to matchuserValue
. – jdigital Commented Jan 28, 2014 at 0:56 - especially since your final switch only has a single case – Matthew Lock Commented Jan 28, 2014 at 0:58
3 Answers
Reset to default 3You 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 < puterValue || puterValue === 2 && userValue === 6:
console.log("case x is true");
break;
}
==> if this case is true the console.log will apply
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 parison 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 parisons in parenthesis help?
(userValue > puterValue || puterValue === 6 && userValue === 2)
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 parison of two different variables. The case label must be a constant.
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 puterRandom = Math.random()
//convert puterRandom into value and string
switch(puterRandom) {
case puterRandom < 0.33: puterString = "rock"; puterValue = 2
break;
default: puterString = "paper"; puterValue = 4
break;
case puterRandom > 0.66: puterString = "scissors"; puterValue = 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 > puterValue || puterValue === 6 && userValue === 2:
document.write("puter's choice: ")
document.write(puterString)
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 < puterValue || puterValue === 2 && userValue === 6:
document.write("puter's choice:")
document.write(puterString)
document.write("<br/>")
document.write("your choice: ")
document.write(userString)
document.write("<br/>")
document.write("sorry, you lose")
break;
//if userValue === puterValue, they tie and this code runs
case puterValue:
document.write("puter's choice: ")
document.write(puterString)
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 plex 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 bining 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 === puterValue) {var resultString = "tie"}
else if(userValue > puterValue || puterValue === 6 && userValue === 2) {var resultString = "win"}
else if(userValue < puterValue || puterValue === 2 && userValue === 6) {var resultString = "lose"}
else {console.log("debug @ line 56")}
//output result
switch(resultString) {
case "win":
document.write("puter's choice: ")
document.write(puterString)
document.write("<br/>")
document.write("your choice: ")
document.write(userString)
document.write("<br/>")
document.write("you win")
break;
case "lose":
document.write("puter's choice: ")
document.write(puterString)
document.write("<br/>")
document.write("your choice: ")
document.write(userString)
document.write("<br/>")
document.write("sorry, you lose")
break;
case "tie":
document.write("puter's choice: ")
document.write(puterString)
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 ment and I will do my best to respond.
Thanks--Hooded Gryphon
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 puterRandom = Math.random()
//convert puterRandom into value and string
switch(puterRandom) {
case puterRandom < 0.33: puterString = "rock"; puterValue = 2
break;
default: puterString = "paper"; puterValue = 4
break;
case puterRandom > 0.66: puterString = "scissors"; puterValue = 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 > puterValue || puterValue === 6 && userValue === 2:
document.write("puter's choice: ")
document.write(puterString)
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 < puterValue || puterValue === 2 && userValue === 6:
document.write("puter's choice:")
document.write(puterString)
document.write("<br/>")
document.write("your choice: ")
document.write(userString)
document.write("<br/>")
document.write("sorry, you lose")
break;
//if userValue === puterValue, they tie and this code runs
case puterValue:
document.write("puter's choice: ")
document.write(puterString)
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 plex 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 bining 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 === puterValue) {var resultString = "tie"}
else if(userValue > puterValue || puterValue === 6 && userValue === 2) {var resultString = "win"}
else if(userValue < puterValue || puterValue === 2 && userValue === 6) {var resultString = "lose"}
else {console.log("debug @ line 56")}
//output result
switch(resultString) {
case "win":
document.write("puter's choice: ")
document.write(puterString)
document.write("<br/>")
document.write("your choice: ")
document.write(userString)
document.write("<br/>")
document.write("you win")
break;
case "lose":
document.write("puter's choice: ")
document.write(puterString)
document.write("<br/>")
document.write("your choice: ")
document.write(userString)
document.write("<br/>")
document.write("sorry, you lose")
break;
case "tie":
document.write("puter's choice: ")
document.write(puterString)
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 ment and I will do my best to respond.
Thanks--Hooded Gryphon
Share Improve this question edited Jan 28, 2014 at 2:03 Samcfuchs asked Jan 28, 2014 at 0:48 SamcfuchsSamcfuchs 3231 gold badge3 silver badges8 bronze badges 2-
1
You'd be better off with an
if
statement here rather than aswitch
. Something likea > b
will return a boolean, which is never going to matchuserValue
. – jdigital Commented Jan 28, 2014 at 0:56 - especially since your final switch only has a single case – Matthew Lock Commented Jan 28, 2014 at 0:58
3 Answers
Reset to default 3You 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 < puterValue || puterValue === 2 && userValue === 6:
console.log("case x is true");
break;
}
==> if this case is true the console.log will apply
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 parison 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 parisons in parenthesis help?
(userValue > puterValue || puterValue === 6 && userValue === 2)
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 parison of two different variables. The case label must be a constant.
本文标签: Running complex operations in Javascript switchstatementStack Overflow
版权声明:本文标题:Running complex operations in Javascript switch-statement - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745632982a2160274.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论