简体   繁体   English

数组不推送预期元素

[英]array doesn't push expected element

I'm learning JavaScript and having this problem.我正在学习 JavaScript 并遇到了这个问题。 Basically I have to check for invalid credit cards number基本上我必须检查无效的信用卡号码

// All valid credit card numbers
const valid1 = [4, 5, 3, 9, 6, 7, 7, 9, 0, 8, 0, 1, 6, 8, 0, 8]
const valid2 = [5, 5, 3, 5, 7, 6, 6, 7, 6, 8, 7, 5, 1, 4, 3, 9]
const valid3 = [3, 7, 1, 6, 1, 2, 0, 1, 9, 9, 8, 5, 2, 3, 6]
const valid4 = [6, 0, 1, 1, 1, 4, 4, 3, 4, 0, 6, 8, 2, 9, 0, 5]
const valid5 = [4, 5, 3, 9, 4, 0, 4, 9, 6, 7, 8, 6, 9, 6, 6, 6]

// All invalid credit card numbers
const invalid1 = [4, 5, 3, 2, 7, 7, 8, 7, 7, 1, 0, 9, 1, 7, 9, 5]
const invalid2 = [5, 7, 9, 5, 5, 9, 3, 3, 9, 2, 1, 3, 4, 6, 4, 3]
const invalid3 = [3, 7, 5, 7, 9, 6, 0, 8, 4, 4, 5, 9, 9, 1, 4]
const invalid4 = [6, 0, 1, 1, 1, 2, 7, 9, 6, 1, 7, 7, 7, 9, 3, 5]
const invalid5 = [5, 3, 8, 2, 0, 1, 9, 7, 7, 2, 8, 8, 3, 8, 5, 4]

// Can be either valid or invalid
const mystery1 = [3, 4, 4, 8, 0, 1, 9, 6, 8, 3, 0, 5, 4, 1, 4]
const mystery2 = [5, 4, 6, 6, 1, 0, 0, 8, 6, 1, 6, 2, 0, 2, 3, 9]
const mystery3 = [6, 0, 1, 1, 3, 7, 7, 0, 2, 0, 9, 6, 2, 6, 5, 6, 2, 0, 3]
const mystery4 = [4, 9, 2, 9, 8, 7, 7, 1, 6, 9, 2, 1, 7, 0, 9, 3]
const mystery5 = [4, 9, 1, 3, 5, 4, 0, 4, 6, 3, 0, 7, 2, 5, 2, 3]

// An array of all the arrays above
const batch = [valid1, valid2, valid3, valid4, valid5, invalid1, invalid2, invalid3, invalid4, invalid5, mystery1, mystery2, mystery3, mystery4, mystery5]

The validity of a credit card is accessed by this algorithm (Luhn algorithm):通过此算法(Luhn 算法)访问信用卡的有效性:

function validateCred(card_number){
let sum = card_number[card_number.length-1];
let flag = 1;
for (i = card_number.length-2; i >= 0; i--){
    if (flag % 2 !== 0){
        card_number[i] *= 2;
        if (card_number[i] > 9){
            card_number[i] -= 9;
        }
    }
    sum += card_number[i];
    flag++;
}

if (sum % 10 === 0){
    return true;
} else {
    return false;
}
}

And I have to create an array with all the invalid cards:我必须创建一个包含所有无效卡片的数组:

function findInvalidCards(numbers){
    let invalid_array = [];
    for (i = 0; i < numbers.length; i++){
        if (!validateCred(numbers[i])){
            invalid_array.push(numbers[i]);
        }
    }
    return invalid_array;
}

When I call the findInValidCards function, it raises the heap out of memory error, I tried to follow the solution from this link and raise the usage memory to 8gb but the problem persists.当我调用findInValidCards函数时,它引发了heap out of memory不足错误,我尝试按照此链接中的解决方案将使用内存提高到 8GB,但问题仍然存在。 After debugging, I found out that this line invalid_array.push(numbers[i]) actually appends a undefined variable to the array, not the element that I wanted.调试后,我发现这一行invalid_array.push(numbers[i])实际上附加了一个undefined变量到数组,而不是我想要的元素。 What could possibly cause this problem?什么可能导致这个问题?

As Nina had said in the comments, you should be defining i with let each time you're writing the for loop or else you're going to have unpredictable behavior正如 Nina 在评论中所说的那样,您应该在每次编写 for 循环时使用let定义i ,否则您将有不可预测的行为

However, you also had an issue in your validateCred function where you were mutating the original Array while checking it - you can simply clone the input Array with let clone = card_number.slice(0);但是,您的validateCred函数中也存在一个问题,您在检查原始数组时对其进行了变异 - 您可以简单地使用let clone = card_number.slice(0);克隆输入数组let clone = card_number.slice(0); , and then reference clone for your checks ,然后为您的检查引用clone

Also, you can simplify findInvalidCards by using .filter() , like:此外,您可以使用.filter()简化findInvalidCards ,例如:

function findInvalidCards(numbers) {
  return numbers.filter(x => !validateCred(x));
}

 // All valid credit card numbers const valid1 = [4, 5, 3, 9, 6, 7, 7, 9, 0, 8, 0, 1, 6, 8, 0, 8]; const valid2 = [5, 5, 3, 5, 7, 6, 6, 7, 6, 8, 7, 5, 1, 4, 3, 9]; const valid3 = [3, 7, 1, 6, 1, 2, 0, 1, 9, 9, 8, 5, 2, 3, 6]; const valid4 = [6, 0, 1, 1, 1, 4, 4, 3, 4, 0, 6, 8, 2, 9, 0, 5]; const valid5 = [4, 5, 3, 9, 4, 0, 4, 9, 6, 7, 8, 6, 9, 6, 6, 6]; // All invalid credit card numbers const invalid1 = [4, 5, 3, 2, 7, 7, 8, 7, 7, 1, 0, 9, 1, 7, 9, 5]; const invalid2 = [5, 7, 9, 5, 5, 9, 3, 3, 9, 2, 1, 3, 4, 6, 4, 3]; const invalid3 = [3, 7, 5, 7, 9, 6, 0, 8, 4, 4, 5, 9, 9, 1, 4]; const invalid4 = [6, 0, 1, 1, 1, 2, 7, 9, 6, 1, 7, 7, 7, 9, 3, 5]; const invalid5 = [5, 3, 8, 2, 0, 1, 9, 7, 7, 2, 8, 8, 3, 8, 5, 4]; // Can be either valid or invalid const mystery1 = [3, 4, 4, 8, 0, 1, 9, 6, 8, 3, 0, 5, 4, 1, 4]; const mystery2 = [5, 4, 6, 6, 1, 0, 0, 8, 6, 1, 6, 2, 0, 2, 3, 9]; const mystery3 = [6, 0, 1, 1, 3, 7, 7, 0, 2, 0, 9, 6, 2, 6, 5, 6, 2, 0, 3]; const mystery4 = [4, 9, 2, 9, 8, 7, 7, 1, 6, 9, 2, 1, 7, 0, 9, 3]; const mystery5 = [4, 9, 1, 3, 5, 4, 0, 4, 6, 3, 0, 7, 2, 5, 2, 3]; // An array of all the arrays above const batch = [valid1, valid2, valid3, valid4, valid5, invalid1, invalid2, invalid3, invalid4, invalid5, mystery1, mystery2, mystery3, mystery4, mystery5]; function validateCred(card_number) { let clone = card_number.slice(0); let sum = clone[clone.length - 1]; let flag = 1; for (i = clone.length - 2; i >= 0; i--) { if (flag % 2 !== 0) { clone[i] *= 2; if (clone[i] > 9) { clone[i] -= 9; } } sum += clone[i]; flag++; } if (sum % 10 === 0) { return true; } else { return false; } }; // And I have to create an array with all the invalid cards: function findInvalidCards(numbers) { let invalid_array = []; for (let i = 0; i < numbers.length; i++) { if (!validateCred(numbers[i])) { invalid_array.push(numbers[i]); } } return invalid_array; } let invalidCardArray = findInvalidCards(batch); // Prettier console.log() invalidCardArray.forEach(x => console.log(validateCred(x) + ':', ...x));

The i variable is defined as global if you don't define it with var or let .如果不使用varlet定义i变量,则将其定义为全局变量。 So when it starts from findInvalidCards function the i value is zero but when it access the validateCred function, the i value changes.So when it will finish from validateCred function, i value will be -1 and it will try to increment it in findInvalidCards for loop and i value will be zero.That will happen over and over again,it will go in infinite loop causing the memory exception and everything crashes.因此,当它从findInvalidCards函数开始时, i值为零,但是当它访问validateCred函数时,i 值会发生变化。因此,当它从validateCred函数中完成时, i值将是-1 ,它会尝试在findInvalidCards增加它循环和i值将为零。这将一遍又一遍地发生,它将进入无限循环,导致内存异常,一切都崩溃了。

I have debug your code, so i know how it goes.我已经调试了你的代码,所以我知道它是怎么回事。 :) :)

  function findInvalidCards(numbers){
        let invalid_array = [];
        //make i local variable by defining it let or var, otherwise it will be global
        for (var i = 0; i < numbers.length; i++){
            if (!validateCred(numbers[i])){
                invalid_array.push(numbers[i]);
            }
        }
        return invalid_array;
    }

function validateCred(card_number){
    let sum = card_number[card_number.length-1];
    let flag = 1;
    //make i local variable by defining it let or var, otherwise it will be global
    for (var i = card_number.length-2; i >= 0; i--){
        if (flag % 2 !== 0){
            card_number[i] *= 2;
            if (card_number[i] > 9){
                card_number[i] -= 9;
            }
        }
        sum += card_number[i];
        flag++;
    }

    if (sum % 10 === 0){
        return true;
    } else {
        return false;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM