Credit Card Checker Challenge Project (JavaScript)

I’m having some issues while I’m on step 4. I’ve tested the function ‘validateCred’ with the examples given and it works and now I’m suppose to use it with the next function ‘findInvalidCards’ in which an array of arrays is taken and an array with invalid cards is returned using the previous function created. My problem is that every time I try and compile my code it runs infinitely or at least for so long I don’t see any results. Any ideas on how I might fix this?

// 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 total=0;

  //even arrays
  if(array.length % 2===0){
    for(i = array.length-1; i >=0; i--){
          if( i % 2 !== 0)
            total += array[i];
          else{
            let tempNum = array[i] * 2;
            while(tempNum > 9)
              tempNum -= 9;
            total = total+ (tempNum);
              }
            }
  }

  //odd arrays
  else{
    for(i=array.length-1; i>=0; i--){
      if(i%2 === 0)
        total += array[i];
      else{
        let tempNum=array[i]*2;
        while(tempNum > 9)
          tempNum -= 9;
        total = total+ (tempNum);
          }
       }
    }
    if(total%10===0)
      return true;
    else
      return false;
}

function findInvalidCards(array){
  let tempArray=[];
  for(i=0; i < array.length; i++){
    if(validateCred(array[i])===false){
      tempArray.push(array[i]);
    }
  }
  return tempArray;
}

console.log(findInvalidCards(batch));

Hello everyone! This is my solution!

// 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 = function(numbers) {
    const reversedNumbers = numbers.slice().reverse();
    const numbersForTheSum = [];

    for (let i = 0; i < reversedNumbers.length; i += 2) {
        numbersForTheSum.push(reversedNumbers[i]);
    }

    for (let i = 1; i < reversedNumbers.length; i += 2) {
        const doubledNum = reversedNumbers[i] * 2;
        if (doubledNum > 9) {
            numbersForTheSum.push(doubledNum - 9);
        } else {
            numbersForTheSum.push(doubledNum);
        }
    }  
    
    const sumNumber = numbersForTheSum.reduce((a, b) => a + b, 0);
    
    if (sumNumber % 10 === 0) {
        return true;
    } else {
        return false;
    }
}

const findInvalidCards = function (nestedNumbers) {
    const invalidCards = [];
    for (let i = 0; i < nestedNumbers.length; i++) {
        const checkValidity = validateCred(nestedNumbers[i]);
        if (checkValidity === false) {
            invalidCards.push(nestedNumbers[i]);
        }
    }
    return invalidCards;
}

const invalidCardCompanies = function (invalidCardsNestedArray) {
    const companies = [];
    for (let i = 0; i < invalidCardsNestedArray.length; i++) {
        if (invalidCardsNestedArray[i][0] === 3) {
            companies.push("Amex");
        } else if (invalidCardsNestedArray[i][0] === 4) {
            companies.push("Visa");
        } else if (invalidCardsNestedArray[i][0] === 5) {
            companies.push("Mastercard");
        } else if (invalidCardsNestedArray[i][0] === 6) {
            companies.push("Discover");
        } else {
            companies.push("Company not found");
        }
    }
    const uniqueCompanies = [...new Set(companies)];
    return uniqueCompanies;
}

console.log(validateCred(valid1));
console.log(findInvalidCards(batch));
console.log(invalidCardCompanies(findInvalidCards(batch)));















2 Likes

A couple of things to check. What happens when variable i is equal to zero? I’m not sure if modulo 2 of 0 is considered an even number. Also the while loop may work for you but consider using an if statement, it may be more appropriate. I don’t know enough to explain why. Hope that helps.

FYI, zero is an even number.

1 Like

I’ve change the while loops to if statements (it does make more sense, considering the most it would have to loop is one time), but I’m still experiencing the same issue. I’ll hit save, it will compile, take a while, and not display anything on the console, as if I never told it to log anything at all.

1 Like

Hi @kogeta

i has never been declared, that causes trouble.
Write for( let i=0; i < array.length; i++) instead…

Oh that did it! Thank you! Strange that it doesn’t catch this as a compiling error.

2 Likes

Here is my finished code! Thanks for the help on this!

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

const batch2 = [mystery1, mystery2, mystery3, mystery4, mystery5];

// Add your functions below:

function validateCred(array){
  let total=0;

  //even arrays
  if(array.length % 2===0){
    for(let i = array.length-1; i >=0; i--){
          if( i % 2 !== 0)
            total += array[i];
          else{
            let tempNum = array[i] * 2;
            if(tempNum > 9)
              tempNum -= 9;
            total = total+ (tempNum);
              }
            }
  }

  //odd arrays
  else{
    for(let i=array.length-1; i>=0; i--){
      if(i%2 === 0)
        total += array[i];
      else{
        let tempNum=array[i]*2;
        if(tempNum > 9)
          tempNum -= 9;
        total = total+ (tempNum);
          }
       }
    }
    //console.log(total);
    if(total%10===0)
      return true;
    else
      return false;
}

function findInvalidCards(array){
  let tempArray=[];
  for(let i=0; i < array.length; i++){
    if(validateCred(array[i])===false){
      tempArray.push(array[i]);
    }
  }
  return tempArray;
}

function idInvalidCardCompanies(array){
  
  let badCompanyArray=[];
  
  for(let i=0; i < array.length; i++){
    switch (array[i][0]){

      case 3:
        if(!badCompanyArray.includes('Amex')){
          badCompanyArray.push('Amex');
        }
        break;
      
      case 4:
        if(!badCompanyArray.includes('Visa')){
          badCompanyArray.push('Visa');
        }
        break;
      
      case 5:
      if(!badCompanyArray.includes('Mastercard')){
        badCompanyArray.push('Mastercard');
      }
      break;
      
      case 6:
      if(!badCompanyArray.includes('Discover')){
        badCompanyArray.push('Discover');
      }
      break;
      
      default:
        if(!badCompanyArray.includes('Company not found')){
          badCompanyArray.push('Company not found');
        }
        break;

        
    }
  }
  
  return badCompanyArray;
}


/*
console.log(validateCred(valid1));
console.log(validateCred(valid2));
console.log(validateCred(valid3));
console.log(validateCred(valid4));
console.log(validateCred(valid5));
console.log(validateCred(invalid1));
console.log(validateCred(invalid2));
console.log(validateCred(invalid3));
console.log(validateCred(invalid4));
console.log(validateCred(invalid5));
console.log(validateCred(mystery1));
console.log(validateCred(mystery2));
console.log(validateCred(mystery3));
console.log(validateCred(mystery4));
console.log(validateCred(mystery5));
*/

console.log(findInvalidCards(batch2));
console.log(idInvalidCardCompanies(findInvalidCards(batch2)));

2 Likes

So much fun completing this exercise :stuck_out_tongue_closed_eyes:
Here’s my solution:

Hey everyone!

Finished my project and here’s the code - https://github.com/Varun8286/Credit-Card-Checker.git

This required a lot of debugging but managed to finish it and understood every core concept.

I couldn’t get my head around thinking of to code the last challenging function on the last step of the project - Function that will convert invalid numbers to valid numbers. Anyone here tried it and mind explaining it how’s it done?
Would appreciate the help a lot.

Thanks!

Hi @varungokalgandhi0901
I also needed quite a while to understand the concept behind it, although in the end it’s quite simple:

Summary

Whatever the digits are, their sum has to be % 10 === 0. You could modify your first function in a way that it rather returns the rest of modulo 10 than a boolean.
With this value you can alter the check digit in order for the sum of the digits in the array to be devidable by 10.

i have resolved this in this mode