3

我是编程新手,曾尝试编写 if-else 逻辑来创建数组等。

我想使用一个点变量来确定变量落在哪个点区间内,然后使用该区间的函数创建数组,并从该数组返回一个随机函数。

例如,我有里程碑 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.
4

6 回答 6

5

你不能像这样链接关系比较。你必须写:

if (1000 <= userScorePoints && userScorePoints < 2500) {
    ...
}

您所写的内容被解析为就像您写的一样:

if ((1000 <= userScorePoints) < 2500) {
    ...
}

括号中的比较结果为 0 或 1,始终小于 2500。

于 2013-08-30T08:32:46.537 回答
4

语法对于您真正想要的东西是错误的-

这是正确的语法 -

if(5000 <= userScorePoints &&  userScorePoints < 10000)

使用 '&&' 进行多个逻辑比较。

我还将解释当您编写代码时解释器会理解什么 -

if(5000 <= userScorePoints < 10000)

基本上是先做比较5000 <= userScorePoints。当转换为数字时,结果将是 atruefalse相当于1或分别。0

因此,在下一步中,您将比较0 < 10000先前比较的结​​果是否为假 1 < 10000结果是否为真。在这两种情况下,值都小于 10000,这就是条件始终为真的原因。

希望这可以消除您的疑问。快乐编码!

于 2013-08-30T08:32:38.880 回答
3

编程不是数学。您的条件应采用以下形式:

if (1000 <= userScorePoints && userScorePoints  < 2500)
于 2013-08-30T08:32:52.907 回答
2

Javascript 不会像这样链接比较。 对于大于*if (a < b < c)将始终为真。c1

解决方案是将它们替换为if (a<b && b < c)

  • a<b<c相当于(a<b)<c由于 Javascript 运算符优先规则。a<b将返回trueor falsewhich 当与数字比较时,类型转换为1or 0。结论是您的所有比较实际上都c0or进行比较1
于 2013-08-30T08:33:03.680 回答
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 */
于 2013-08-30T08:33:18.663 回答
0

图沙尔是对的。采用:

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)];
}
于 2013-08-30T08:39:23.183 回答