我是编程新手,并尝试编写if-else逻辑来创建数组等。
我想使用一个点变量来决定变量落在哪个点间隔内,然后用该间隔的funfacts创建数组,并从该数组中返回一个随机的funfact。
例如,我有里程碑1000,2500等。如果userScorePoints超过2500,我希望该方法从包含有关该数字的funfact的数组中返回一个随机funfact,直到userScorePoints已达到下一个里程碑,即5000。
我写的代码的问题是它只返回第一个if中的随机funfact,所以我只从数字1000得到funfacts,即使我应该从数字2500获得funfacts,因为我现在的分数是超过2603 ..
有人可以帮我解决这个问题吗?
这是我的代码:
function getFunfact(userScorePoints) {
var array = new Array();
if (1000 <= userScorePoints < 2500) {
var funfact1000 = new Array();
funfact1000[0] = "funfacts about the number 1000";
funfact1000[1] = "...";
funfact1000[2] = "...";
funfact1000[3] = "...";
funfact1000[4] = "...";
funfact1000[5] = "...";
array = funfact1000;
} else if (2500 <= userScorePoints < 5000) {
var funfact2500 = new Array();
funfact2500[0] = "funfacts about the number 2500";
funfact2500[1] = "...";
funfact2500[2] = "...";
funfact2500[3] = "...";
funfact2500[4] = "...";
funfact2500[5] = "...";
array = funfact2500;
} else if (5000 <= userScorePoints < 10000) {
var funfact5000 = new Array();
funfact5000[0] = "funfacts about the number 5000";
funfact5000[1] = "...";
funfact5000[2] = "...";
funfact5000[3] = "...";
funfact5000[4] = "..."
funfact5000[5] = "...";
array = funfact5000;
} else if (10000 <= userScorePoints < 20000) {
var funfact10000 = new Array();
funfact10000[0] = "funfacts about the number 10.000";
funfact10000[1] = "...";
funfact10000[2] = "...";
funfact10000[3] = "...";
funfact10000[4] = "...";
funfact10000[5] = "...";
array = funfact10000;
} else if (20000 <= userScorePoints < 30000) {
var funfact20000 = new Array();
funfact20000[0] = "funfacts about the number 20.000";
funfact20000[1] = "...";
funfact20000[2] = "...";
funfact20000[3] = "...";
funfact20000[4] = "...";
funfact20000[5] = "...";
array = funfact20000;
} else if (30000 <= userScorePoints < 50000) {
//etc.
} else {}
return array[getRandom(6)]; //this method returns a random element, this one works.
答案 0 :(得分:5)
你不能像这样链接关系比较。你必须写:
if (1000 <= userScorePoints && userScorePoints < 2500) {
...
}
你写的内容被解析为好像你写的:
if ((1000 <= userScorePoints) < 2500) {
...
}
括号中的比较评估为0或1,总是小于2500。
答案 1 :(得分:4)
语法错误的实际需要 -
这是正确的语法 -
if(5000 <= userScorePoints && userScorePoints < 10000)
使用'&amp;&amp;'用于多个逻辑比较。
我还将解释在编写代码时解释器能够理解的内容 -
if(5000 <= userScorePoints < 10000)
基本上是第一次比较5000 <= userScorePoints
。结果将是true
或false
,在转换为数字时分别相当于1
或0
。
因此,如果结果为true,则在下一步中,如果上一次比较的结果为false 或 0 < 10000
,则比较1 < 10000
。在这两种情况下,值都小于10000,这就是条件始终为真的原因。
希望这可以解除你的怀疑。快乐的编码!
答案 2 :(得分:3)
编程不是数学。你的问题应该采取以下形式:
if (1000 <= userScorePoints && userScorePoints < 2500)
答案 3 :(得分:2)
Javascript不会像这样进行比较。
对于大于if (a < b < c)
*的<{1}},c
将始终为真。
解决方案是用1
if (a<b && b < c)
相当于a<b<c
。 (a<b)<c
将返回a<b
或true
,与数字进行比较时会返回false
或1
。结论是,您的所有比较实际上都会将0
与c
或0
进行比较。答案 4 :(得分:1)
你需要使用这样的东西:
if (1000 <= userScorePoints && userScorePoints < 2500)
if (2500 <= userScorePoints && userScorePoints < 5000)
etc...
您使用的语法不会为您提供所需的结果,因为Javascript会像这样解析它:
if((1000 <= userScorePoints)/*either true(1) or false(0)*/ < 2500)/* Always true,
since both 0 and
1 are < 2500 */
答案 5 :(得分:0)
Tushar是对的。 使用:
function getFunfact(userScorePoints) {
var array = new Array();
if ((1000 <= userScorePoints) && (userScorePoints < 2500)) {
var funfact1000 = new Array();
funfact1000[0] = "funfacts about the number 1000";
funfact1000[1] = "...";
funfact1000[2] = "...";
funfact1000[3] = "...";
funfact1000[4] = "...";
funfact1000[5] = "...";
array = funfact1000;
} else if ((2500 <= userScorePoints) && (userScorePoints < 5000)) {
var funfact2500 = new Array();
funfact2500[0] = "funfacts about the number 2500";
funfact2500[1] = "...";
funfact2500[2] = "...";
funfact2500[3] = "...";
funfact2500[4] = "...";
funfact2500[5] = "...";
array = funfact2500;
} else if ((5000 <= userScorePoints) && (userScorePoints < 10000)) {
var funfact5000 = new Array();
funfact5000[0] = "funfacts about the number 5000";
funfact5000[1] = "...";
funfact5000[2] = "...";
funfact5000[3] = "...";
funfact5000[4] = "..."
funfact5000[5] = "...";
array = funfact5000;
} else if ((10000 <= userScorePoints) && (userScorePoints < 20000)) {
var funfact10000 = new Array();
funfact10000[0] = "funfacts about the number 10.000";
funfact10000[1] = "...";
funfact10000[2] = "...";
funfact10000[3] = "...";
funfact10000[4] = "...";
funfact10000[5] = "...";
array = funfact10000;
} else if ((20000 <= userScorePoints) && (userScorePoints < 30000)) {
var funfact20000 = new Array();
funfact20000[0] = "funfacts about the number 20.000";
funfact20000[1] = "...";
funfact20000[2] = "...";
funfact20000[3] = "...";
funfact20000[4] = "...";
funfact20000[5] = "...";
array = funfact20000;
} else if ((30000 <= userScorePoints) && (userScorePoints < 50000)) {
//etc.
} else {}
return array[getRandom(6)];
}