Array.some给出TypeError

时间:2019-10-21 15:25:32

标签: javascript visual-studio-code

这些是VSC写的错误。

if (array.some(typeof item !== 'number') || !(intNumber)) {
          ^

TypeError: true is not a function
    at Array.some (<anonymous>)
    at addNumber (D:\ИРИТ-РТФ\3 курс\Тинькофф\1 ЛАБА 2 попытка\rtf-lab-1-master\index.js:7:15)
    at Object.<anonymous> (D:\ИРИТ-РТФ\3 курс\Тинькофф\1 ЛАБА 2 попытка\rtf-lab-1-master\index.js:14:13)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
    at startup (internal/bootstrap/node.js:283:19)

function toArray() {
    return [...arguments];
}

function addNumber(array, number) {
    let intNumber = (number >= 0 && number <= 9) ? Number.isInteger(number) : false;
    if (array.some(typeof item !== 'number') || !(intNumber)) {
        return null;
    }
    
    const stringArray = array.map(parseInt) + number;
    return toArray(stringArray);
}
console.log(addNumber([1, 0, 9], 2));
console.log(addNumber([2, 5, 1], 5));
console.log(addNumber([1], 4020));
console.log(addNumber([1, '4'], 1));
console.log(addNumber([1, 2, 4], 1));
console.log(addNumber([0], 900));
console.log(addNumber([7, 1, 7, 7, 1], 1248));
console.log(addNumber([0], 0));
console.log(addNumber([0, '4'], 11));
console.log(addNumber([null], 125));

2 个答案:

答案 0 :(得分:3)

some的参数必须是一个函数(spec | MDN)。您正在传递typeof item !== 'number',即true,因为在任何地方都没有定义itemtypeof可以使用未声明的标识符)。

将其传递给函数:

array.some(item => typeof item !== 'number')

这将使some检查每个项目,如果其中任何一个不是数字,则使其返回true(因此使addNumber返回null)。

实时示例(我不能保证代码中没有其他问题,但是问题的问题已解决):

function toArray() {
    return [...arguments];
}

function addNumber(array, number) {
    let intNumber = (number >= 0 && number <= 9) ? Number.isInteger(number) : false;
    if (array.some(item => typeof item !== 'number') || !(intNumber)) {
        return null;
    }
    
    const stringArray = array.map(parseInt) + number;
    return toArray(stringArray);
}
console.log(addNumber([1, 0, 9], 2));
console.log(addNumber([2, 5, 1], 5));
console.log(addNumber([1], 4020));
console.log(addNumber([1, '4'], 1));
console.log(addNumber([1, 2, 4], 1));
console.log(addNumber([0], 900));
console.log(addNumber([7, 1, 7, 7, 1], 1248));
console.log(addNumber([0], 0));
console.log(addNumber([0, '4'], 11));
console.log(addNumber([null], 125));

答案 1 :(得分:0)

再次...有误。

function addNumber(array, number) {
    let intNumber = (number >= 0 && number <= 9) ? Number.isInteger(number) : false;
    if (array.some(item => typeof item !== 'number') || !(intNumber)) {
        return null;
    }
    
    const result = array.reduce(function(sum, current) {
        return sum + current;
    }, 0);
    const stringArray = (parseInt(result) + number).toString();
    return stringArray.Split('').map(item => parseInt(item));
}
console.log(addNumber([1, 0, 9], 2));
console.log(addNumber([2, 5, 1], 5));
console.log(addNumber([1], 4020));
console.log(addNumber([1, '4'], 1));
console.log(addNumber([1, 2, 4], 1));
console.log(addNumber([0], 900));
console.log(addNumber([7, 1, 7, 7, 1], 1248));
console.log(addNumber([0], 0));
console.log(addNumber([0, '4'], 11));
console.log(addNumber([null], 125));