Credit Card Checker Challenge Project (JavaScript)

That’s what each case is doing, no? The if statement is to stop duplicates being put in the array as per the last point of the task
" idInvalidCardCompanies() should return an array of companies that have mailed out cards with invalid numbers. This array should NOT contain duplicates, i.e. even if there are two invalid Visa cards, "Visa" should only appear once in the array."

Given there is no category for ‘not found’ the default is moot.

Are you getting any error messages?

I ended up looking at the solution. The code actually works as it should, I was calling it to the console incorrectly! I didn’t realise until I copy pasted the console.log from the solution.

And I thought I was getting better at noticing those small details…

2 Likes

Github: Source Code

1 Like

My solution:

I used a for loop and decremented versus incrementing and then I used digit variable to track the position of [i]… I later saw other people using the reverse method, I want to use it in my next project.

 for (let i = arr.length - 1; i >= 0; i--){
    let digit = arr[i]; }

Here’s my solution. I would be grateful if you can help to improve my code.

//Part 1
let validateCred = arr => {
  let sum = 0;
  for (let i = arr.length-1; i >= 0; i--)  {
   if ((arr.length - 1 - i) % 2 === 1) {    
     if (arr[i] * 2 > 9) {
       sum += (arr[i] * 2 - 9);
       }
    else {
      sum += (arr[i] * 2)
      }
   }
   else { 
     sum += (arr[i]);
   }
  }
 if (sum % 10 === 0) {
    return true;
    }
  else {
    return false;
    }
  }

//Part 2
let findInvalidCards = (arr) => {
  let invalidCardsArray = [];
 for (let i = 0; i < arr.length; i++) {
   if (validateCred(arr[i]) === false) {
     invalidCardsArray.push(arr[i])
   }
 }
  return invalidCardsArray
}

//Part 3
let invalidCards = findInvalidCards(batch);
let invalidCardCompanies = (arr) => {
 let companies = [];
 let list = [];
 for(let i = 0; i < arr.length; i++) { 
   companies.push(arr[i][0]);
    }
  if (companies.includes(3)) {
      list.push('Amex')
    }
  if (companies.includes(4)) {
      list.push('Visa')
      }
  if (companies.includes(5)) {
      list.push('Master')
    }
  if (companies.includes(6)) {
      list.push('Discover')
       }
  if (companies.some(x => x < 3 )) {
    console.log('No company found!')
  }
  else if (companies.some(x => x > 6 )) {
    console.log('No company found!')
  }
    
   return list
}

My approach => Codecademy export · GitHub

/*
CREDIT CARD CHECKER BY ~ FRU GODWILL FRU
*/
// 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(arr){
    let multiplied = [];
    let nonMultiplied = [];

    for (i= arr.length-1; i>0; i= i-2){
      
      nonMultiplied.push(arr[i])
    }

    for (i= arr.length-2; i>=0; i= i-2){
     
      let multiplier = arr[i]*2
       if(multiplier>9){
         multiplier = multiplier - 9
       }else{
         multiplier = multiplier
       }
     
      multiplied.push(multiplier)
    };

 let multipliedSum = multiplied.reduce((a,b)=>a+b);
 
 let nonMultipliedSum = nonMultiplied.reduce((a,b)=>a+b);

 let  sumMod =  (multipliedSum + nonMultipliedSum) % 10 ;

/*
  if (sumMod===0){
    console.log(`CC is valid`)
  }else{
     console.log(`Invalid CC`)
  }
*/
 return sumMod
};

//Find invalid Cards and return array
function findInvalidCards(x){
    let invalidArr = []
  for(let i of x){
    if (validateCred(i) != 0){
      invalidArr.push(i)
    }
  }
  return invalidArr;
}

//set batch array called with invalid cards function to var
const invalidArr = findInvalidCards(batch);

//Find invalid cards companies
function idInvalidCardCompanies(x){
let invalidCardCompanies = [];
     for (i=0; i<x.length; i++){
 if( (x[i][0] === 3) && !invalidCardCompanies.includes(`Amex (American Express)`)){  
     invalidCardCompanies.push(`Amex (American Express)`)
      }else if( (x[i][0] === 4) && !invalidCardCompanies.includes(`Visa`)){  
     invalidCardCompanies.push(`Visa`)
     } else if( (x[i][0] === 5) && !invalidCardCompanies.includes(`Mastercard`)){  
     invalidCardCompanies.push(`Mastercard`)
     }else if( (x[i][0] === 6) && !invalidCardCompanies.includes(`Discover`)){  
     invalidCardCompanies.push(`Discover`)
      }else if(x[i][0] != 6 && x[i][0] != 5 && x[i][0] != 4 && x[i][0] != 3   ){
    console.log(`“Company not found”.`)
       }
  }
     return invalidCardCompanies
}


console.log(idInvalidCardCompanies(invalidArr))

Code for my project can be found here:

Similar in a lot of ways to the suggested solutions.

Hey there! I have completed the challenge and I am trying to create a gist to post it on my github account! I DO NOT HAVE A SHARE BUTTON!!! Why is that?

Did you try copying the URL from the location bar?

I did that, just that it requires autenthification when I access it…could I copy the content, paste it into codepen and generate a URL?

I completed this project. Here’s my code:

As I understand it, a GIST is a one-off and should be public. Been wrong before, especially since I never use it. You may have to ring up the latest user in this thread and ask them for advice/assistance. I’m sure they won’t mind.

Hi! Sharing my code here, and i would be thrilled if somebody reviewed or had remarks.Thanks!

// 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 = [7, 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, invalid6, mystery1, mystery2, mystery3, mystery4, mystery5];


const validateCred = (arr) => {
  // invert arr
  let temp = [];
  for (let i = arr.length - 1; i >= 0; i--) {
    temp.push(arr[i]);
  }
  //console.log(temp);
  //Luhn algorithm
  for (let j = 0; j < temp.length; j++) {
    if (j % 2 !== 0) {
      if (temp[j] * 2 > 9) {
        temp[j] = temp[j] * 2 - 9;
      } 
      else {
        temp[j] = temp[j] * 2;
      }
    }
    else {
      continue;
    }
  }
  //console.log(temp);
  //sum of array values
  let sum = temp.reduce(function (previousValue, currentValue) {
    return previousValue + currentValue
  }, 0)
  //result
  if (sum % 10 === 0) {
    return true
  }
  else {
    return false
  }
}

const findInvalidCards = (arr) => {
  let invalidArr = arr.filter(card => {
    let result = validateCred(card);
    return result === false
  });
  return invalidArr;
}

const idInvalidCardCompanies = (arr) =>  {
  let result = [];
  let companies = { 3: 'Amex' , 4: 'Visa' , 5: 'Mastercard', 6: 'Discover'};
  for (card of arr) {
    let company = companies[card[0]];
    if (company === undefined){
      result.push('Company not found')
    }
    else if (!result.includes(company)){
      result.push(company)
    } 
    else {
      continue;
    }
  }
  return result
}


console.log(batch.forEach(card => console.log(validateCred(card))))
console.log(findInvalidCards(batch))
console.log(idInvalidCardCompanies(findInvalidCards(batch)))

Hi there, my code is posted to my GitHub - codecademyCreditCardChecker/main.js at main · NinPasker/codecademyCreditCardChecker · GitHub

I had to break down a lot of sections of this problem, took about ~3 days to get the extra challenges done.

If there’s any way I can improve my code I’m open to suggestions, it’s a little hard to read but I left comments to help someone understand what I was trying to achieve.

Happy coding fellow code warriors!!

Hello everyone,
Here is my code. Would appreciate any feedback!

GitHub

Greetings,
Julia

Hi there,
I just looked at your solution. Thank you for sharing! I’m very new to this so I don’t think I can give you much feedback. I was just curious to see other solutions. I like your comments. I should start doing that too! I didn’t do the challenges.
It was interesting comparing our solutions.
The first function I started out with something similar to yours, but then couldn’t figure out how to add the new arrays together and how to get their sums. I admit I ended up looking at the solution to find out how to write an if statement for ever other element.
The third function I did exactly like you but then got the array with some companies named twice. I found another solution which seems to work. But I’m curious to know what ‘new Set’ in your code line 100 does? I assume it takes the double companies out? How did you find it?
Sorry, I don’t have suggestions for you.
I posted my code below if you are interested.
Greetings,
Julia

Hi Julia,

Thanks for taking a look at my code! :blush:

Happy to hear you’ll take on adding comments - comments make a world of difference! I’ve noticed it helps me understand what I’m trying to achieve from the code I’m writing.

Yeah the first function is a tough one, I tried several different methods until I chose to break up the array and isolate the numbers I didn’t want to double and the ones that I did, then all I had to do was save the sum from each array to a variable & add them together.

I was tempted to take a look at the solution but I was adamant on being a google search warrior :stuck_out_tongue_winking_eye:, a couple of google searches led me to the .reduce() method, which was in one of the prior lessons on built-in array methods, for adding array elements together.

For the new Set method I googled how to remove duplicate items in an array, which led me to some documentation on the method, I’ve linked them here if you’d like to give a read - basically it removes duplicate elements from an array by simply calling new Set on your array.

new Set() documentation links

Stackoverflow Documentation on new Set()

MDN Documentation for new Set()

No need to apologize, I’m glad you were able to take things away from reviewing my code :grin:.

Also I reviewed your code, there are a few suggestions I can give you:

Try giving your code some breathing space, it allows you to debug easier since thing’s are spaced out and it looks cleaner to read.

When using an if statement, if possible make use of the if else statement instead of creating separate if statements as you did in your final function to check invalid card companies, it gives less room for bugs to appear in your code.

On line 78 of your code the return call to companies stops the code right there. Which means your if statement on line 80 will never run because the code returns a value at line 78 and believes it doesn’t need to continue to execute the code below it.

If you have discord you can add me on there my username is nintachi#1624, you can DM me and if you’d like we can share ideas on the projects coming up.

Hopefully my little essay above is easy to understand, happy coding, looking forward to hearing from you soon!

1 Like

Thank you so much for your respond, the links and for taking the time to review my code! I really appreciate it!
I will try working with your suggestions!
I’m in the front-end-developer path although I’m thinking about switching over to back-end. If I just would know… :wink:

But I think either way, the next project is called Mysterious Organism. Maybe I see you there again.
I’m not on discord (yet). However, would be great to stay connected.
Have a good day,
Julia

1 Like