First of all, I don’t know why it keeps showing augmented form of credit card numbers, even if I used const inside the functions, this made me unable to identify the first number of the original array such as invalid1 or invalid3 .
My codes can only make the computer show the index of the batch array. Any chance this can still work or do I have to start from scratch all over again?
function validateCred(arr) {
const reversedArray = arr.reverse()
const doubledArray = [];
for (let i = 0; i < reversedArray.length; i++) {
if (i === 0) {
arr[i] *= 1;
} else if (i === 1 || i % 2 === 1) {
arr[i] *= 2;
}
doubledArray.push(arr[i]);
}
//console.log(doubledArray)
const finalArray = [];
for (let j = 0; j < doubledArray.length; j++) {
if (doubledArray[j] > 9) {
doubledArray[j] -= 9;
}
finalArray.push(doubledArray[j]);
}
//console.log(finalArray)
const sum = finalArray.reduce((a, b) => a + b, 0);
//console.log(sum)
if (sum % 10 === 0) {
return "valid";
} else return "invalid";
}
function findInvalidCards(arr3) {
const results = [];
for (let k = 0; k < arr3.length; k++) {
if (validateCred(arr3[k]) === "invalid") {
results.push(k);
}
}
return results
}
const index = findInvalidCards(batch);
console.log(index);
console.log(batch[index[0]])