Javascript类型错误:对象没有方法

时间:2016-06-25 22:13:09

标签: javascript

am尝试提交具有背景检查的实验室工作(可见和不可见的测试)

这是我的代码

var input = [];
    for (i = 1; i <= 100; ++i) {
        input[i - 1] = i;
    }


    var fizzBuzz = function(what, inputList) {
        return inputList.filter(function(item, index) {
            return !(item % what);
        });
    };


    var fizzes = fizzBuzz(3, input);
    var buzzes = fizzBuzz(5, input);
    var fizzbuzzes = fizzBuzz(15, input);


    var transform = function(to, onWhat) {
        return function(item, index) {
            onWhat[item - 1] = to; 
        };
    };

    fizzes.each(transform("Fizz", input));
    buzzes.each(transform("Buzz", input));
    fizzbuzzes.each(transform("FizzBuzz", input));


    input.each(function(item, index) {
        document.write(item + "<br>");
    });

以下是错误

您的代码中存在错误/错误 结果:

/home/ubuntu/Applications/andelabs-server/tmp/andelabs_5765ac1ee83de519007349eb_565c28fd9c306a14009680f2.js:25     fizzes.each(transform(&#34; Fizz&#34;,输入));            ^ TypeError:对象3,6,9,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57,60,63,66,69,72 ,75,78,81,84,87,90,93,96,99没有方法&#39;每个&#39;     在对象。 (/home/ubuntu/Applications/andelabs-server/tmp/andelabs_5765ac1ee83de519007349eb_565c28fd9c306a14009680f2.js:25:9)     在Module._compile(module.js:456:26)     at Object.Module._extensions..js(module.js:474:10)     在Module.load(module.js:356:32)     在Function.Module._load(module.js:312:12)     在Module.require(module.js:364:17)     at require(module.js:380:17)     在对象。 (/home/ubuntu/Applications/andelabs-server/tmp/5765ac1ee83de519007349eb-565c28fd9c306a14009680f2-test.js:1:78)     在Module._compile(module.js:456:26)     在Object.Module._extensions..js(module.js:474:10)

1 个答案:

答案 0 :(得分:0)

数组没有这样的方法each()。您可以使用Array.prototype.forEach

fizzes.forEach(transform("Fizz", input));
buzzes.forEach(transform("Buzz", input));
fizzbuzzes.forEach(transform("FizzBuzz", input));


input.forEach(function(item, index) {
    document.write(item + "<br>");
});