use*_*951 3 javascript arrays if-statement
我是编程新手,并尝试编写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.
Run Code Online (Sandbox Code Playgroud)
你不能像这样链接关系比较.你必须写:
if (1000 <= userScorePoints && userScorePoints < 2500) {
...
}
Run Code Online (Sandbox Code Playgroud)
你写的内容被解析,好像你写的:
if ((1000 <= userScorePoints) < 2500) {
...
}
Run Code Online (Sandbox Code Playgroud)
括号中的比较评估为0或1,始终小于2500.
| 归档时间: |
|
| 查看次数: |
138 次 |
| 最近记录: |