简体   繁体   中英

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. 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. Check the if statement inside of the findInvalidCred() I added a comment with the output I am getting as well. 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. 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'
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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