# Credit Card Checker - Step 4

Hi, I’m trying to do step 4 but when I log the second function to the console, it’s giving me 12 invalids arrays, when it should be only 8. Also, the arrays that are logged have random numbers. I just don’t understand why ``````const validateCred = arr => {
let lastD = arr.pop();

let double;

let reverseArr = arr.reverse();

let newArr = [];

let specificArr = [];

for (let i = 0; i < reverseArr.length; i += 2) {

newArr.push(reverseArr[i] * 2)

};

for (let i = 0; i < newArr.length; i++){

if (newArr[i] > 9) {

double = newArr[i] - 9;

specificArr.push(double)

} else {

double = newArr[i]

specificArr.push(double)

}

};

for (let i = 1; i < reverseArr.length; i += 2) {

specificArr.push(reverseArr[i])

};

let finalArr = specificArr.push(lastD);

const suma = specificArr.reduce((a, b) => a + b, 0);

if (suma % 10 === 0) {

return 'Valid card number';

} else {

return 'Invalid card number';

}

};

const findInvalidCards = nesArr => {

let invalidCards = [];

let validCards = [];

nesArr.forEach(value => {

validateCred(value)

validateCred(value) === 'Invalid card number' ? invalidCards.push(value) : validCards.push(value)

})

return invalidCards;

};

console.log(validateCred(mystery1))

console.log(findInvalidCards(batch))
``````

Hey,
run this shortened version of your code:

``````const originalArray = [1,2,3,4,5,6];

const validateCred = arr => {
let lastD = arr.pop();
let reverseArr = arr.reverse();
return reverseArr;
}

console.log(validateCred(originalArray));
console.log(originalArray)
``````

I think you’ll see what’s wrong then.

1 Like

In addition to the issue @mirja_t is referring to, you have an extra function call above. That in combination with the previously mentioned problem is the source of your, “random numbers”.

1 Like

Oh, I see I tried to make some changes in the code but still doesn’t working as it should, could you check it please??

``````const validateCred = arr => {
let newArr = [];
let finalArr = [];
let double;
for (let i = arr.length - 1; i >= 0; i -= 2) {
newArr.push(arr[i] * 2)
}
for (let i = 0; i < newArr.lenth; i++) {
if (newArr[i] > 9) {
double = newArr[i] - 9
finalArr.push(double)
} else {
double = newArr[i]
finalArr.push(double)
}
}
for (let i = arr.length; i >= 0; i -= 2) {
finalArr.push(arr[i])
}
const suma = finalArr.reduce((a, b) => a + b, 0)
if (suma % 10 === 0) {
return 'Valid card number.'
} else {
return 'Invalid card number.'
}
}
``````

Now when I call validateCred(), all the cards log as invalid The problem wasn’t reversing the array. The problem was mutating the original array. Your previous code worked fine otherwise. Your current code will probably work for most card numbers, (haven’t actually tested it) but not for those with an odd number of digits once you fix a typo:

1 Like

Hiii it’s me again, I did this “new version”, but now the problem is when I try to check valid3, I can see that it has 15 characters but I’m trying to solve that but definitely I can’t…

``````const validateCred = arr => {
//let lastD = arr.pop();
let double;
//let reverseArr = arr.reverse();
let newArr = [];
let specificArr = [];
let normalNums = [];
if (arr.length < 16) {
for (let i = 0; i < arr.length - 2; i += 2) {
newArr.push(arr[i] * 2)
}
} else if (arr.length >= 16) {
for (let i = 0; i < arr.length - 1; i += 2) {
newArr.push(arr[i] * 2)
}
}
console.log(newArr)
for (let i = 0; i < newArr.length; i++){
if (newArr[i] > 9) {
double = newArr[i] - 9;
specificArr.push(double)
} else {
double = newArr[i]
specificArr.push(double)
}
};
console.log(specificArr)
for (let i = 1; i < arr.length; i += 2) {
normalNums.push(arr[i])
};
console.log(normalNums)
let finalArr = specificArr.concat(normalNums)
console.log(finalArr)
//let lastNumber = 0;
//lastNumber = arr[arr.length - 1]
const suma = finalArr.reduce((a, b) => a + b, 0);
console.log(suma)
if (suma % 10 === 0) {
return 'Valid card number';
} else {
return 'Invalid card number';
}
};

const findInvalidCards = nesArr => {
let invalidCards = [];
let validCards = [];
nesArr.forEach(value => {
validateCred(value)
validateCred(value) === 'Invalid card number' ? invalidCards.push(value) : validCards.push(value)
})
return invalidCards;
};

``````

In the first two for loops, should the `<` be `<=` ?
here:

``````for (let i = 0; i < arr.length - 2; i += 2) {
``````

and here:

``````for (let i = 0; i < arr.length - 1; i += 2) {
``````

Reversing the array was not a bad strategy. The only problem was that your code changed the original array. You could make a copy of the array first, and then reverse the copy leaving the original array intact.

Hint

One way to make a copy of an array:
Array.prototype.slice() - JavaScript | MDN

1 Like