Credit Card Checker Challenge Project (JavaScript)

Thank you so much for your encouraging feedback. Going back to read the instructions again I think you are correct about logging that the company was not found instead of pushing to the array.

1 Like

Hi!
I’m a beginner but your code looks very clean and concise to me and functions well. Great job! I learned a lot looking through your use of the for loops on the first function. I also had never heard of the new Set method you used on the last function so I learned a lot reading about that method as well. The only thing I noticed is that you pushed ‘Company not found’ to the array of companies. I did the same thing as well but someone pointed out to me that it should have been logged as an error instead of going to the array per the directions.

Hello,

I have just completed the Credit Card Checker Challenge Project and I don’t have the share link at the bottom of my code editor, but I have copied it. I will paste it below: (Super anxious sharing as it is my first project to share!)

// 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];

// Add your functions below:

const validateCred = arr => {
let revArr = arr.reverse();
let everyOther = (arr, n) => revArr.filter((e,i) => i % n === 0);
let every2nd = everyOther(revArr,2);
let oddNum = (arr, n) => revArr.filter((e,i) => i % n === 1);
let notEvery2nd = oddNum(revArr,2);
let dubEven = every2nd.map(x => x * 2);
let check9 = ;
for(let i = 0; i <= dubEven.length - 1; i++) {
if(dubEven[i] > 9) {
check9.push(dubEven[i] - 9)
} else {
check9.push(dubEven[i])
}
}
let modArr = notEvery2nd.concat(check9);

let reduce = (previousValue, currentValue) => previousValue + currentValue;

let sumTotal = modArr.reduce(reduce);
// console.log(sumTotal)
if(sumTotal % 10 === 0) {
return ‘Valid Credit Card’
} else {
return ‘Invalid Credit Card’
}
};

const findInvalidCards = nArr => {
let invalidCards = ;
for(let i = 0; i <= nArr.length-1; i++) {

let checkValid = validateCred(nArr[i])
 if(checkValid === "Invalid Credit Card") {
  invalidCards.push(nArr[i])
}

}
return invalidCards;
};

const idInvalidCardCompanies = invalidNums => {
let companies = ;
for(let i = 0; i <= invalidNums.length - 1; i++) {
let compDigit = invalidNums[i]
if(compDigit[0] === 3) {
companies.push(‘Amex’)
} else if(compDigit[0] === 4) {
companies.push(‘Visa’)
} else if(compDigit[0] === 5) {
companies.push(‘Mastercard’)
} else if(compDigit[0] === 6) {
companies.push(‘Discover’)
} else {
console.log(‘Company not found’)
}
}
let singleCompanies = ;
companies.forEach(element => {
if(!singleCompanies.includes(element)) {
singleCompanies.push(element)
}
});
console.log(singleCompanies);
};

let invalidCCs = findInvalidCards(batch);

//idInvalidCardCompanies(invalidCCs)

//Which companies sent out invalid codes?

let invalidBatch = [invalid1, invalid2, invalid3, invalid4, invalid5];

idInvalidCardCompanies(invalidBatch)

Here’s my solution. I tried to keep the code concise but wonder if, ultimately, this might have affected the readability. Let me know your thoughts!

Here’s my code. I hope it will be helpful for you all.

Here’s my solution. Hope this helps.

// 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];


// Add your functions below:
const validateCred =  arr =>{
  let sum = 0;
  let j = 1;
  for (let i = arr.length-1 ; i >= 0 ; i--)
  {
    if(j%2==0) {
      sum+=(arr[i]*2) <= 9?(arr[i]*2):(arr[i]*2)-9 ;
    }
    else {
      sum+=arr[i];
    }
    j++;
  }
  return (sum%10) == 0;
}
const findInvalidCards = batch =>{
  let res = [];
  batch.forEach(ele=>{
    if(!validateCred(ele)) res.push(ele);
  });
  return res;
}
const idInvalidCardCompanies = batch =>{
 let flags = [false,false,false,false];
 let flagDesc = ["Amex (American Express)","Visa","Mastercard","Discover"];
 batch.forEach(ele=>{
   switch( ele[0] ){
    case 3:
      flags[0]=true;
      break;
    case 4:
      flags[1]=true;
      break;
    case 5:
      flags[2]=true;
      break;
    case 6:
      flags[3]=true;
      break;
    default:
      break;
   }
 });
 for(const index in flags){
     if(!flags[index]) flagDesc.splice(index,1);
   }
return flagDesc;
}

This is my solution, I tried to use simple approaches in hope of having a fast and efficient code. pls improve my code and allow me to learn more from you !

Hello, I am struggeling with the first function I am supposed to create in order to accomplish this project.

I commented my code to make it easier for you to understand.

The variable “sum” should be 100 and the modulo should be 0 if the credit card is valid. I tested my function with the first given valid credit card array, but my sum is not 100, can someone help me?

My sum is = 73

(I have added some console.log invokes to debugg if for myself, but I couldn’t figure out where I stuck)

// 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];


// Add your functions below:

function validateCred(array) {

  let sum = 0;
  const checkDigit = array[array.length-1];
  let digit = 0;

  // First Step: Add Checkdigit to sum
  sum += checkDigit;
  console.log(`Sum is: ${sum}`);

  // Second Step: Multiply every other digit from behind, expect Checkdigit
  
  for(let i = array.length-2; i >= 0; i--) {
    let digit = array[i];
    console.log(`Digit is: ${digit}`);
    digit *= 2;
    console.log(`Digit multiplied by 2: ${digit}`);

    // Thrid Step:
    //Check if Digit is > 9 after multiplying, if so, subtrac 9 of digit
    // And add the digit to the sum.

    if(digit > 9) {
      digit -= 9;
      console.log(`digit > 9: ${digit}`);
      sum += digit;

    } else {
      console.log(`digit else: ${digit}`);
      sum += digit;
    }
  }

  console.log(sum);
}

validateCred(valid1);

Hi,
If your code runs correctly, the sum of the first array (based on the instructions) should be 80.

Because you’re decreasing the value of i in your code below by 1 (using 1- -), every number except the last number will be doubled. This is not what we want; it should be every other number. To achieve this, you should decrement by 2 (using i -= 2)

for(let i = array.length-2; i >= 0; i--) {
    let digit = array[i];
    console.log(`Digit is: ${digit}`);
    digit *= 2;
    console.log(`Digit multiplied by 2: ${digit}`);

Now that you’ve doubled every other number, you need to add the other digits that weren’t doubled, to sum. The code you’ve written so far only adds the doubled digits to sum. You can do this using another for loop similar to the one you already have, keeping the above correction in mind.

1 Like

Hi there, I am also stuck!
Since I am trying a new career path!
// 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];

const invalid6 = [0, 1, 1, 1, 1, 1, 1, 1, 2];

// 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];

const batch2 = [invalid1, invalid2, invalid3, invalid4, invalid5, invalid6];

// Add your functions below:

function validateCred(n) {

let sum = 0;

let doubleBol = true;

//luhm algor

const checkDigit = batch[batch.length -1];

sum += checkDigit

//variables 2x

for (let i=checkDigit.lengtth -2; i >=0; i–){

if(doubleBol){

 let doubleVal = batch[i]*2;

 if(doubleVal >9){

   doubleVal -=9;

 }

 sum +=doubleVal;

 doubleBol = false;

} else if(!doubleBol){

 sum += batch[i];

 doubleBol = true;

}

} return sum % 10 === 0;

}

// testing

console.log(validateCred(valid3));

console.log(validateCred(invalid6));
// my results are always false!

based on the way that your function is written,
I think that
function validateCred(n) {
should actually be
function validateCred(batch) {
because batch is the name that you use for that array later in the function

also, there seem to be typos in the code posted