赋值(kindof)后返回null的值?

时间:2016-05-22 16:57:33

标签: javascript math return equations

这是我的代码,有一些错误(我稍后会解释)

<html><head><title>RandMath</title></head><body>
<p>RandomMath</p>
<p>Click the button below to generate a random equation.</p>

<button onclick="exampleSite();">Generate</button>
<button id="isHard" onclick="harder();">Change Hardness</button>
<button onclick="setTimeout(function() {window.location = 'http://xtremeplayzcode.github.io/randmath/source.js';}, 100);">See the code</button>
<p id="generate"></p>

<script>
function exampleSite() {
    change('generate', true);
}
var equations = [];
var isHarder;
function addEquation(equation) {
    this.fromArray = function(array) {
        equations.concat(equations, array);
    }
    equations.push(equation);
}
function getRandomizer(bottom, top) {
    return Math.floor(Math.random() * (1 + top - bottom)) + bottom;
}
function getRandomNumber(isHard) {
    var results = "";
    if (isHard === true) {
        for (var i = 0; i < 1; i++) {
            results += getRandomizer(1, 9);
        }
    } else {
        for (var i_ = 0; i_ < 2; i_++) {
            results += getRandomizer(1, 9)
        }
    }
    return results;
}
function useExampleEquations() {
    var num_7 = getRandomNumber(isHarder),
        num_6 = getRandomNumber(isHarder),
        num_5 = getRandomNumber(isHarder),
        num_4 = getRandomNumber(isHarder),
        num_3 = getRandomNumber(isHarder),
        num_2 = getRandomNumber(isHarder),
        num_1 = getRandomNumber(isHarder);

    var equation1 = num_1+" x "+num_2+" + {"+num_3+" x [("+num_4+" x "+num_5+") - "+num_6+"] + "+num_7+"} = x",
        equation2 = num_1+" x "+num_2+" = y",
        equation3 = num_1+"s x "+num_2+" = z, s = "+num_3,
        equation4 = num_1+" + {" +num_2+ " x [" +num_3+" + ("+num_4+" x "+num_5+") + "+num_6+"] + "+num_7+"} = x",
        equation5 = num_1+"e + "+num_2+"l x "+num_3+" + "+num_4+"a, e = "+num_5+", l = "+num_6+", a = "+ num_7,
        equation6 = "["+num_1+" x "+num_2+ "z] + {"+num_3+" - "+num_4+"} + ("+num_5+" + "+num_6+") = e, z = "+ num_7,
        equation7 = "p x "+num_1+" x "+num_2+" - "+num_3+" + "+num_4+" = e, p = "+num_5;

    var exampleEquations = [
        equation1,
        equation2,
        equation3,
        equation4,
        equation5,
        equation6,
        equation7
    ]

    addEquation.fromArray(exampleEquations);
 }
function getRandomEquation() {
    var change = 0;
    change = getRandomizer(0, equations.length - 1);
    return equations[change];
}
function change(element, useExampleEquations_) {
    if (useExampleEquations_) {
        useExampleEquations();
        document.getElementById(element).innerHTML = getRandomEquation();
    } else {
         document.getElementById(element).innerHTML = getRandomEquation();
    }
}
function harder() {
    if (isHarder) {
            isHarder = false;
    } else if (!isHarder) {
            isHarder = true;
    } else {
        isHarder = true;
    }
 }
 </script>



 </body></html>

当我点击生成时,它什么都不返回。有帮助吗? (P.S.工作演示在这里:here如果你想在我毁了它之前看到它或者看看我想要它怎么样:p)

1 个答案:

答案 0 :(得分:0)

问题在于addEquation以及你(不)调用它的方式。

function addEquation(equation) {
    this.fromArray = function(array) {
        equations.concat(equations, array);
    }
    equations.push(equation);
}

如果曾调用addEquation(它不是),则调用它的对象将获得fromArray属性。但是函数addEquation本身没有任何名为fromArray的属性,所以当执行useExampleEquations()中的代码时:

addEquation.fromArray(exampleEquations);

您将在控制台上收到错误消息。打开Javascript控制台并单击generate按钮,您可能会看到类似于以下错误:

TypeError: addEquation.fromArray is not a function

(这是来自nodejs的错误;您的浏览器的错误消息可能不同。)