开发者问题收集

函数内部 >>> TypeError: 无法读取未定义的属性“length”

2020-02-06
64

这是一段根据给定要求验证信用卡号的代码。我编写的代码适合主函数中的所有检查函数,并且以这种方式运行良好。但是我想整理一下我的代码并使其更好地实践,所以代码现在就是这样。我想我仍然无法完全理解部分函数。你能告诉我我在这里犯了什么错误吗? 欢迎提供任何意见。

'use strict';
let cardNumLength = getLength();
let isNumOnly = /^\d+$/.test(cardNum);
let isLastDigitEven = isEndEven();
let isSumValid = isSumGreaterThan16();
let allDigitsNotSame = allEqualCheck(cardNum);
let errorArray = [];

function cardNumValidator(cardNum) {
    if (cardNumLength, isNumOnly, isLastDigitEven, isSumValid, allDigitsNotSame) {
        console.log(`"${cardNum}" is a valid credit card number.`);
        return
    }
    return errorArray;
}

// getLength function to check if the number has 16 digits
function getLength(cardNum) {
    //console.log(cardNum.length); //debugging
    if (cardNum.length == 16) {
        return true;
    }
    return false;
}

// to check the final digit if its even
function isEndEven(cardNum) {

    if (cardNum[cardNum.length - 1] % 2 == 0) {
        return true;
    }
    return false;
}

// to check if the sum of the digits are greater than 16
function isSumGreaterThan16(cardNum) {
    let intCardNum = parseInt(cardNum);
    let sum = 0;
    for (let i = 0; i < cardNum.length; i++) {
        sum = parseInt(sum) + parseInt(cardNum[i]); //parseInt() converts string into number
    }
    if (sum > 16) {
        return true;
    }
    return false;
}

function allEqualCheck(cardNum) {
    if (cardNum.split('').every(char => char === cardNum[0])) {
        return false;
    }
    return true;
}

/* using switch statement to final validation regarding to the requirements those checked seperately by previous inner functions*/
function isValidError() {
    if (cardNumLength === false) {
        errorArray.push('Number must be 16 digits!');
    } else if (isNumOnly === false) {
        errorArray.push('Invalid characters!');
    } else if (isLastDigitEven === false) {
        errorArray.push('Odd final number!');
    } else if (isSumValid === false) {
        errorArray.push('Sum less than 16!');
    } else if (allDigitsNotSame === false) {
        errorArray.push('All numbers can not be the same!');
    }
    return errorArray;
}
cardNumValidator('9999777788880000'); //valid number example
cardNumValidator('6666666666661666'); //valid number example

cardNumValidator('a92332119c011112'); //Invalid number example
cardNumValidator('4444444444444444'); //Invalid number example
cardNumValidator('1111111111111110'); //Invalid number example
cardNumValidator('6666666666666661'); //Invalid number example
2个回答

在第一行中,您没有向 getLength 函数传递任何参数。

Łukasz Karczewski
2020-02-06

我像您一样使用全局变量,但根据 cardNum 设置它们。 cardNumValidator 函数现在将始终返回一个错误数组,当没有错误时,该数组的长度为零。当有多个错误时,errorArray 将包含所有错误,而不仅仅是一个错误。

'use strict';
let cardNumLength = false;
let isNumOnly = false;
let isLastDigitEven = false;
let isSumValid = false;
let allDigitsNotSame = false;
let errorArray = [];

function cardNumValidator(cardNum) {
	cardNumLength = getLength(cardNum);
	isNumOnly = /^\d+$/.test(cardNum);
	isLastDigitEven = isEndEven(cardNum);
	isSumValid = isSumGreaterThan16(cardNum);
	allDigitsNotSame = allEqualCheck(cardNum);

	errorArray = [];
	isValidError();

    if (errorArray.length == 0) {
        console.log(`"${cardNum}" is a valid credit card number.`);
    } else {
        console.log(`"${cardNum}" is an invalid credit card number.`);
        console.dir(errorArray);
    }
    return errorArray;
}

// getLength function to check if the number has 16 digits
function getLength(cardNum) {
    //console.log(cardNum.length); //debugging
    if (cardNum.length == 16) {
        return true;
    }
    return false;
}

// to check the final digit if its even
function isEndEven(cardNum) {

    if (cardNum[cardNum.length - 1] % 2 == 0) {
        return true;
    }
    return false;
}

// to check if the sum of the digits are greater than 16
function isSumGreaterThan16(cardNum) {
    let intCardNum = parseInt(cardNum);
    let sum = 0;
    for (let i = 0; i < cardNum.length; i++) {
        sum = parseInt(sum) + parseInt(cardNum[i]); //parseInt() converts string into number
    }
    if (sum > 16) {
        return true;
    }
    return false;
}

function allEqualCheck(cardNum) {
    if (cardNum.split('').every(char => char === cardNum[0])) {
        return false;
    }
    return true;
}

/* using switch statement to final validation regarding to the requirements those checked seperately by previous inner functions*/
function isValidError() {
    if (cardNumLength === false) {
        errorArray.push('Number must be 16 digits!');
    }
    if (isNumOnly === false) {
        errorArray.push('Invalid characters!');
    }
    if (isLastDigitEven === false) {
        errorArray.push('Odd final number!');
    }
    if (isSumValid === false) {
        errorArray.push('Sum less than 16!');
    }
    if (allDigitsNotSame === false) {
        errorArray.push('All numbers can not be the same!');
    }
    return errorArray;
}
cardNumValidator('9999777788880000'); //valid number example
cardNumValidator('6666666666661666'); //valid number example

cardNumValidator('a92332119c011112'); //Invalid number example
cardNumValidator('4444444444444444'); //Invalid number example
cardNumValidator('1111111111111110'); //Invalid number example
cardNumValidator('6666666666666661'); //Invalid number example
Adder
2020-02-06