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))
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”.
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:
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;
};
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.