Credit Card Checker Challenge Project (JavaScript)

This is my solution to this challenge

Really good challenge for sure. Here’s my solution:

// Step #3 function validateCred(array) { let revArray = array.reverse(); let cardCheck = []; let cardSum = 0; for (num in revArray) { if (num % 2 == true) { let revArrayDub = revArray[num] * 2; if (revArrayDub > 9) { cardCheck.push(revArrayDub - 9); cardSum += (revArrayDub - 9); } else { cardCheck.push(revArrayDub); cardSum += revArrayDub; }; } else { cardCheck.push(revArray[num]); cardSum += revArray[num]; }; }; //console.log(`Card Check: ${cardCheck}`); //console.log(`Card sum: ${cardSum}`); if (cardSum % 10 === 0) { return 'Credit card number is valid!'; } else { return 'Credit card number is NOT valid!'; }; }; //console.log(validateCred(mystery5)); // Step #4 function findInvalidCards(array) { let invalidCards = []; let nestedBatch = batch.forEach((card) => { if (validateCred(card) === 'Credit card number is NOT valid!') { invalidCards.push(card.reverse()) }; }); return invalidCards; }; //console.log(findInvalidCards(batch)); // Step #5 function idInvalidCardCompanies(array) { let cardNums = findInvalidCards(array); let invalidCompanies = []; for (nums in cardNums) { //console.log(cardNums[nums][0]); if (cardNums[nums][0] === 3) { invalidCompanies.push('Amex'); } else if (cardNums[nums][0] === 4) { invalidCompanies.push('Visa'); } else if (cardNums[nums][0] === 5) { invalidCompanies.push('Mastercard'); } else if (cardNums[nums][0] === 6) { invalidCompanies.push('Discover'); } } let unique = (array) => { return [...new Set(array)]; } console.log(unique(invalidCompanies)) //console.log(invalidCompanies); } idInvalidCardCompanies(batch);

Here guys my solution!!
Please give me feedbacks
Thanks a lot

Hello everyone!
Here is the link to my solution. Any comment will be appreciated.

Credit Card Checker

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

const test1 = [4, 7, 1, 6, 4, 3, 7, 8, 4, 7, 6, 6, 0, 3, 8, 9];
const test2 = [4, 5, 5, 6, 7, 7, 6, 5, 4, 5, 9, 7, 0, 5, 7, 1];
const test3 = [5, 3, 8, 7, 9, 9, 3, 6, 7, 8, 7, 0, 9, 4, 7, 6];
const test4 = [3, 7, 6, 9, 7, 2, 6, 3, 4, 2, 6, 2, 4, 5, 1];
const test5 = [6, 0, 1, 1, 0, 6, 7, 3, 3, 1, 3, 0, 0, 3, 8, 1, 7, 9, 5];

// An array of all the arrays above
const batch2 = [test1, test2, test3, test4, test5];

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 sum = 0;
  let digit = 0;
  let c = 0;
  for (let i = arr.length - 1; i >= 0; i--) {
    c++;
    if (c % 2 == 0 && i != arr.length - 1) {
      digit = arr[i] * 2;
      if (digit > 9) {
        digit -= 9;
      }

      sum += digit;
    } else {
      sum += arr[i];
    }
  }

  if (sum % 10 == 0) {
    return true;
  } else {
    return false;
  }
}

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

function idInvalidCardCompanies(arr) {
  let com = [];
  for (let i = 0; i < arr.length; i++) {
    if (arr[i][0] == 3 && !com.includes("Amex")) {
      com.push("Amex");
    } else if (arr[i][0] == 4 && !com.includes("Visa")) {
      com.push("Visa");
    } else if (arr[i][0] == 5 && !com.includes("Mastercard")) {
      com.push("Mastercard");
    } else if (arr[i][0] == 6 && !com.includes("Discover")) {
      com.push("Discover");
    } else {
      com.push("Company not found");
    }
  }
  return com;
}

const invalid = findInvalidCards(batch);

console.log(idInvalidCardCompanies(invalid));


const testInvalid = findInvalidCards(batch2);
console.log(idInvalidCardCompanies(testInvalid));

Many many hours (over 12 :sweat_smile:) later and here it is. Any feed back appreciated.
Project was frustrating, but worth it.

// 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 evensDoubled = []; // 0,2,4...indexes let evensDoubledLessNine = [] // let oddTotals = [] // 1,3,5...indexes let oddsSum let total = 0 for ( let i= array.length -2; i >=0; i--) { evensDoubled.push(array[i] * 2); } //return evensDoubled; let j = 0 while (j < evensDoubled.length) { if (evensDoubled[j] < 9 ) { evensDoubledLessNine.push(evensDoubled[j]); } else if (evensDoubled[j] > 9) { evensDoubledLessNine.push(evensDoubled[j] - 9) } j++; } //return evensDoubledLessNine for (i=1; i<array.length ; i+=2) { oddTotals.push(array[i] * 1) } //return oddTotals; let sum2 = evensDoubledLessNine.reduce((accumulator, currentValue) => accumulator + currentValue, 0); //console.log(sum2) let sum3 = oddTotals.reduce((accumulator, currentValue) => accumulator + currentValue, 0); //console.log(sum3) total = sum2+sum3; //return total if (total % 10 === 0) { return true } else { return false } } //console.log(validateCred(invalid3)) function findInvalidCards (nestedArray) { let invalidCards = [] for (i=0; i < nestedArray.length; i++) { if (validateCred(nestedArray[i]) === false) { invalidCards.push(nestedArray[i]) } } return invalidCards } function idInvalidCompanies (nestedArray) { let badCC = []; for ( let i=0; i < nestedArray.length ; i++) switch(nestedArray[i][0]) { case 3: badCC.push(`Amex (American Express) ${nestedArray[i]}`) break; case 4: badCC.push(`Visa ${nestedArray[i]}`) break; case 5: badCC.push(`Mastecard ${nestedArray[i]}`) break; case 6: badCC.push(`Discover ${nestedArray[i]}`) break; case 3: badCC.push(`Amex (American Express) ${nestedArray[i]}`) break; default: badCC.push(`Company not Found`) } return badCC } console.log(idInvalidCompanies(batch)) // GOOD TRY But messed up. Reversed too early and kept the same format. The reversed changes how Luhn algo works // let twoMultiply = [] // let sumlessIndex0 = 0 // function reverseArray (array) { // let rArray = [] // rArray = array.reverse() // return rArray // }; // function twoTimesArray (array) { // for ( i = 1 ; i < array.length; i+=2) { // twoMultiply.push(array[i] * 2) // }return twoMultiply // }; // function lessThanNine(array) { // let copy = [] // for (i=0; i<array.length;i++) { // if (array[i] > 9 ) { // copy.push(array[i] - 9) // } // } return copy // } // function sumCheck (arr1, arr2, arr3) { // let arr1SumOdds = 0 // let arr2TimesTwo = 0 // let arr3LessNine = 0 // for (i=0; i<arr1.length; i+=2) { // arr1SumOdds = arr1SumOdds + arr1[i] // } console.log(arr1SumOdds) // for (i=0; i<arr2.length; i++) { // if (arr2[i] < 9) // arr2TimesTwo = arr2TimesTwo + arr2[i] // } console.log(arr2TimesTwo) // for (i=0; i<arr3.length; i++) { // arr3LessNine = arr3LessNine + arr3[i] // } console.log(arr3LessNine) // return sumlessIndex0 = arr1SumOdds+arr2TimesTwo+ arr3LessNine // } // function validateCred (array) { // let sumIncludesIndexLast = 0 // sumIncludesIndexLast = sumlessIndex0 + array[array.length - 1]; // if (sumIncludesIndexLast % 10 === 0) { // return `valid` // } else { // return `invalid` // } // } // console.log(reverseArray(valid1)) // console.log(twoTimesArray(valid1)) // console.log(lessThanNine(twoMultiply)) // console.log(sumCheck(reverseArray(valid1))) // // ,twoTimesArray(reverseArray),lessThanNine(twoMultiply))) // console.log(validateCred(valid1)) // // console.log(reverseArray(mystery5)[0]) // // function twoTimesArray (array) { // // const copy = []; // // array.forEach(function(arrayItem) { // // copy.push(arrayItem * 2) // // }); return copy // // } // //function lessThanNine (array) { // // array.map (num => { // // //.map doesnt work here as it preserve the lenght of the array. The conditions be return a different size array // // if (num > 9) { // // num - 9 // // } // // }) // // } // // function lessThanNine(array) { // // for (let i = 0; i < array.length; i++) { // // if (array[i] > 9) { // // array[i] -= 9; // Subtracting 9 from elements greater than 9 // // } // // } // // return array;

Here is my solution, please let me know any thoughts to improve. Thanks!

This is my solution, it might be a little bit ugly but it works and I am proud of it :slight_smile:

My solution. I made my own isUnique function to determine if the company was already there. Realize now I could have just used .includes.
main.js