# Credit Card checker Step 4

Hey

I am struggling to understand the output of my code.
step 3 was to verify whether a card number was valid or not using a logic close to Luhn algorithm.
I have completed this and received the desired output i.e. true or false (valid or invalid).
step 4 i then need to build a function which will accept the variable ‘`batch`’ containing all the variables.
The purpose here is to create 2 new variables which containing the valid and invalid.

2 questions are:

1. the output of step four is pushing the arrays to the correct buckets, but it does not return the card numbers as their original state, instead it in includes some of the factoring from step 3 (`function validateCred(array)`). Why does this happen?

2. is there a way that i can push the names of the array to the variables in step 4, rather than the whole credit card number? This would probably simplify the whole process anyway.

``````// 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(array) {
let cardNumber = array;
for (let i = array.length -2; i >= 0; i -= 2){
let numCheck = array[i]*2;

if (numCheck > 9){
cardNumber[i] = numCheck -9;
} else { cardNumber[i] = numCheck }
}

let sumCard =  cardNumber.reduce((a,b) => a + b, 0);

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

console.log(validateCred(valid2));  //testing output results to true
console.log(validateCred(invalid2)); // testing output results to false

function findInvalidCards(nestedArray){
let validCards = [];
let invalidCards = [];

for (let i = 0; i < nestedArray.length; i++){
let array = nestedArray[i];
let x = validateCred(array);

if (  x === true ){
validCards.push(array)
} else { invalidCards.push(array)}
}

console.log(validCards);
console.log(invalidCards)

}

findInvalidCards(batch)
``````

1. Because here:
``````function validateCred(array) {
let cardNumber = array;
``````

you don’t make a couple of the array. You simple have yet another variable which points to the same array in memory. Sort of like handing out your address to two people (variables). They now both know your address, but there is only house (array)

1. You could use a dictionary?

thanks for explaining that makes sense, i’ll check out the dictionary feasibility.

Hey after researching a little more i opted to use the slice function…

``````function validateCred(array) {
let cardNumber = array.slice(0);
``````

This has resolved the issue, but happy for feedback whether this is deemed a correct approach.
you can review the full code on GitHub.

many thanks Martin

Seems odd to me that you would want to specify the start location but omit the end location when you’re using the default value for both.

You could use that method. You could also do the copying yourself.

You’ve got this in your code:

``````        if (sumCard % 10 === 0){
return true
} else {return false}
}
``````

Your condition will either evaluate to true, or false.
When true, you return true.
When false, you return false.
In both cases, the result of the condition is returned, so wouldn’t it be better to return THAT?

``````return sumCard % 10 === 0
``````

The reason you’re looking to copy the array is because you’re modifying it. But what do you actually need from that array? Do you need to create a modified array? You’re looking for a sum. All you really need then, is to read from it. There’s no need to make a copy or modify it.