Challenge Project: Credit Card Checker
// Add your functions below:
const validateCred = arr => { // created function
// Iterating through for loop from right to left from check digit and skiping every other digit
for (let i = arr.length - 2; i >= 0; i = i - 2){
// Every other digit is multiplied by 2
arr[i] = arr[i] * 2
// if other digit’s answer is greater than 9 the subtract 9 from the value
if (arr[i] > 9) {
arr[i] = arr[i] - 9 // new value to arr[i] is stored
}
}
// Declare a total variable to get the sum of all values in an array.
let total = 0
// Summing up all elements of array one by one using for loop
for (let i = 0; i < arr.length; i++){
total = total + arr[i]
}
// Calculating if sum modulo 10 is 0 then the number is valid otherwise invalid
return total % 10 === 0
};
// Logging validateCred function to console
// console.log(validateCred(valid1))
// In order to log the batch of nested arrays we need to iterate the batch using for loop
for (let i = 0; i < batch.length; i++){
console.log(validateCred(batch[i]))
};
// 4- Creating a function findInvalidCards()
const findInvalidCards = arr => {
// Declaring a new empty to store invalid cards latter
let invalidCards =
//Role of this function is to check through the nested array to find out for invalid cards using for loop
for (let i = 0; i < arr.length; i++){
// let callValidateCred = validateCred(arr[i])
if (validateCred(arr[i]) === false) {
invalidCards.push(arr[i])
}
}
return invalidCards
}
console.log(findInvalidCards(batch))
// 5- Create idInvalidCardCompanies
const idInvalidCardCompanies = arr => {
if (findInvalidCards(arr[0]) === 3) {
console.log(Amex (American Express)
)
} else if (findInvalidCards(arr[0]) === 4) {
console.log(Visa
)
} else if (findInvalidCards(arr[0]) === 5) {
console.log(Mastercard
)
} else if (findInvalidCards(arr[0]) === 6) {
console.log(Discover
)
} else {
console.log(Company not found
)
}
};
console.log(idInvalidCardCompanies(batch))