admin管理员组文章数量:1386829
I am trying to make a function that involves switch() and gives me answers on specific, randomly generated numbers. Somehow my function does not run the case it is supposed to run. It only gives me the default case, no matter what the number is.
This is my code:
var i;
var girl;
function response() {
var girl = prompt("What girl do you like?");
var r = (Math.random() * (3 - 1 + 1) + 1).toFixed(0);
var i = r;
switch(i) {
case (i == 1):
alert(girl + " likes you as a friend.");
break;
case (i == 2):
alert(girl + " does not really like you.");
break;
case (i == 3):
alert(girl + " has a crush on you.");
break;
case (i == 4):
alert(girl + " wants you to ask her out.");
break;
default:
console.log(i);
}
}
I am trying to make a function that involves switch() and gives me answers on specific, randomly generated numbers. Somehow my function does not run the case it is supposed to run. It only gives me the default case, no matter what the number is.
This is my code:
var i;
var girl;
function response() {
var girl = prompt("What girl do you like?");
var r = (Math.random() * (3 - 1 + 1) + 1).toFixed(0);
var i = r;
switch(i) {
case (i == 1):
alert(girl + " likes you as a friend.");
break;
case (i == 2):
alert(girl + " does not really like you.");
break;
case (i == 3):
alert(girl + " has a crush on you.");
break;
case (i == 4):
alert(girl + " wants you to ask her out.");
break;
default:
console.log(i);
}
}
Share
Improve this question
edited Sep 30, 2016 at 9:13
alex
491k204 gold badges889 silver badges991 bronze badges
asked Sep 30, 2016 at 9:02
VincentVincent
1673 silver badges16 bronze badges
1
-
toFixed
converts to a string, so you might want to usecase '1':
,case '2':
, etc. Also, your range won't cover all cases, you might want to usevar i = (Math.random() * 4 + 1).toFixed(0);
. – Yimin Rong Commented Sep 30, 2016 at 9:07
4 Answers
Reset to default 5That's not how a switch works. It pares the value for each case
to the switch
.
Essentially now, it is paring the value of i
multiple times to boolean values (the result of, for example i == 1
).
Also, your randomness doesn't bee more random by adding arithmetic with static values into the value like you did. You should replace it with 4
. You also should use something like Math.ceil()
(since you're ignoring the 0
value, which is also probably not a good idea), not toFixed()
which returns a string.
You also don't need the parenthesis around the values to pare. If you know the range of the random number, you also probably don't need a default case (since you cover every possibility already).
You can also use r
directly, no need to reassign it.
Your variables are also local to your function, so probably don't need them at the top.
Here is how I would re-write this:
function response() {
var girl = prompt("What girl do you like?");
var r = Math.floor(Math.random() * 4);
switch(r) {
case 0:
alert(girl + " likes you as a friend.");
break;
case 1:
alert(girl + " does not really like you.");
break;
case 2:
alert(girl + " has a crush on you.");
break;
case 3:
alert(girl + " wants you to ask her out.");
break;
}
}
...or even...
function response() {
var answerSuffixes = [
" likes you as a friend.",
" does not really like you.",
" has a crush on you.",
" wants you to ask her out."
];
var girl = prompt("What girl do you like?");
var r = Math.floor(Math.random() * answerSuffixes.length);
alert(girl + answerSuffixes[r]);
}
The case statement in Javascript already does a match on the target i
:
switch(i) {
case 1:
alert(girl + " likes you as a friend.");
break;
case 2:
alert(girl + " does not really like you.");
break;
case 3:
alert(girl + " has a crush on you.");
break;
case 4:
alert(girl + " wants you to ask her out.");
break;
default:
console.log(i);
}
So, there is no need to have pare expressions in each case
.
When you declare a switch, the variable used is pared to each case, so evaluations are not necessary.
Also it may help to parse your answer from 'r' into an integer for the sake of keeping the variable types in check.
function response() {
var girl = prompt("What girl do you like?");
var r = parseInt((Math.random() * (3 - 1 + 1) + 1).toFixed(0));
var i = r;
switch(i) {
case 1:
alert(girl + " likes you as a friend.");
break;
case 2:
alert(girl + " does not really like you.");
break;
case 3:
alert(girl + " has a crush on you.");
break;
case 4:
alert(girl + " wants you to ask her out.");
break;
default:
console.log(i);
}
}
switch
is paring strict.
You need either
switch (i) {
case 1:
alert(girl + " likes you as a friend.");
break;
Or
switch (true) {
case i === 1:
alert(girl + " likes you as a friend.");
break;
本文标签: javascriptSwitch() does not work with Mathrandom() numbersStack Overflow
版权声明:本文标题:javascript - Switch() does not work with Math.random() numbers - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744556398a2612500.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论