简体   繁体   English

与函数一起使用时数组的问题

[英]Problem with Arrays when being used with a function

I am trying to use my validateCred() function which can check an array of credit cards and returns valid or invalid and works fine.我正在尝试使用我的 validateCred() 函数,它可以检查信用卡数组并返回有效或无效并且工作正常。 There is also a batch array containing all of the arrays together.还有一个包含所有数组的批处理数组。 The problem is that inside of my findInvlaidCards() function when I want to use the validateCred() method inside the if statement after i loop through the batch array in the for loop of the findInvalidCards() function it transforms the arrays into different ones.问题是,在我的 findInvlaidCards() 函数内部,当我在 findInvalidCards() 函数的 for 循环中循环批处理数组后,我想在 if 语句中使用 validateCred() 方法时,它将数组转换为不同的数组。 Check the if statement inside of the findInvalidCred() I added a comment with the output I am getting as well.检查 findInvalidCred() 中的 if 语句,我还添加了一条注释以及我得到的输出。 Please help and explain it the best you can.请帮助并尽可能地解释它。

// 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];
const invalid6 = [3, 7, 1, 6, 1, 2, 0, 1, 9, 9, 8, 5, 2, 3, 6];

// 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, invalid1, invalid2, invalid3, invalid4, 
invalid5, invalid6, mystery1, mystery2, mystery3, mystery4, mystery5];


// Add your functions below:

function validateCred(arr){
let sum = 0;
for (let i = arr.length - 1; i >= 0; i--) {
if(i % 2 == 0){
arr[i] *= 2;
if(arr[i] > 9){
  arr[i] -= 9;
}
sum += arr[i];
} else {
sum += arr[i];
}
}
if (sum % 10 == 0){
return "Invalid";
} else {
return "Valid";
}
}
function findInvalidCards(array){
const invalidArrays = [];
for(let i = 0; i < array.length; i++){
//console.log(array[i]);
if(validateCred(array[i]) == "Invalid"){
  console.log(array[i]);
/* this if statment returns these arrays which are not what i have
[ 8, 5, 6, 9, 3, 7, 5, 9, 0, 8, 0, 1, 3, 8, 0, 8 ]
[ 1, 5, 6, 5, 5, 6, 3, 7, 3, 8, 5, 5, 2, 4, 6, 9 ]
[ 3, 0, 2, 1, 2, 4, 8, 3, 8, 0, 3, 8, 4, 9, 0, 5 ]
[ 8, 5, 6, 9, 8, 0, 8, 9, 3, 7, 7, 6, 9, 6, 3, 6 ]
[ 1, 4, 3, 6, 2, 0, 0, 8, 3, 1, 3, 2, 0, 2, 6, 9 ]
[ 3, 0, 2, 1, 6, 7, 5, 0, 4, 0, 9, 6, 4, 6, 1, 6, 4, 0, 6 ]
[ 8, 9, 2, 3, 1, 4, 0, 4, 3, 3, 0, 7, 4, 5, 4, 3 ]*/
 invalidArrays.push(array[i]);
}
}
//console.log(invalidArrays)
return invalidArrays;
}

findInvalidCards(batch);

You are changing the original arrays inside your validateCred function.您正在更改 validateCred 函数中的原始数组。 You can instead store the array element value in a variable and then do any operations you want on it.您可以改为将数组元素值存储在一个变量中,然后对其进行任何您想要的操作。

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

  if (sum % 10 == 0) {
    return 'Invalid'
  } else {
    return 'Valid'
  }
}

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

相关问题 在间隔内使用 state 时出现问题 - Problem updating state when it is being used in an interval 当函数用作函数参数时“不是函数” - "not a function" when function being used as function parameter 正在使用的函数,当它不是函数时? - Function being used, when it isn't a function? 未定义的引用错误未定义-使用Angular时添加javascript函数 - Uncaught referenceerror is not defined - adding javascript function when Angular is being used 为什么在构造函数之外对函数进行原型开发时会正确返回函数,而在构造函数中将其用作方法时却不能正确返回? - Why is my function being returned correctly when prototyped outside of my constructor, but not when it's being used as a method within my constructor? 在无限循环中使用$ rootscope.broadcast时,如何在异步模式下编写函数? - How to write function in async mode when $rootscope.broadcast is being used in an infinite loop? 当它被用作函数中的参数时如何更改全局变量的值? (JavaScript) - How to change value of global var when it is being used as a parameter in a function? (Javascript) 什么时候使用匿名函数? - When will an anonymous function be used? RGB填充样式中未使用Javascript函数 - Javascript Function Not Being Used in RGB fill Style 为什么new与函数表达式一起使用? - Why is new being used with a function expression?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM