Credit Card Checker Challenge Project (JavaScript)

Anyone know why step 1 won’t console.log any value for me?

Great work! How would you get rid of duplicates right at the end? I used companiesArray.indexOf(‘Visa’) < 0), how else did other people do it?

I used the new E6 Set const variable = […new Set(arr variable)]. Take a look at the find invalid company function in my code to see how I distinctly print the array

1 Like

Here’s my solution
Any major improvements?

I like what you did at the end with Sets. But when I enter it into terminal it doesn’t print anything.

Also:


I don’t think this bit works when the card has an odd number of digits. E.g. if the card has 16 digits, i=15, so the value returned will be the same as the first digit.

However, if the card has 15 digits, i=14 and i % 2 will === 0, so you will end up with the wrong thing. To avoid this I used an external counter, which counts up from 0. I think codeacaemdy decided to use:


Which is the same as using a counter since it will equate to 0 when the loop is starting. E.g. if there are 16 digits in the card, then (numArr.length - 1 - i) = (16 - 1 - 15). Hope that makes sense.

Also right at the end:


This doesn’t make use of the previous functions or call them, so even if you enter a valid card it will tell you the company.

Same problem with codeAcademy’s solution:


If you console.log(idInvalidCardCompanies([valid3])); it prints a company name, even though the card is valid. @mtf @annajiali
Have I done something wrong? In my solution, I called the previous functions so the last function can be called and it only prints companies with invalid cards.

This is my solution:

It’s working perfectly for me possibly you have the wrong function call… meanwhile, if you study the Luhn’s Algorithm very well you will noticed you start calculating from the rightmost end. Which will give the right value regardless of the number of cards… E.g, how would you solve this; 79927398713x find the value of x… My Algorithm places 1 * n(last right),2 * n(Second to last right)… starting from the right going towards the left. You have to understand the luhn theory and try your algorithm with many card as possible… https://www.google.com/amp/s/www.geeksforgeeks.org/luhn-algorithm/amp/

Here is my code attempt, feedback welcome :+1: :+1: :+1:

Just some silliness…

t = [[],[]]
a = [4, 5, 3, 9, 6, 7, 7, 9, 0, 8, 0, 1, 6, 8, 0, 8]
b = 1
while (a.length) {
  b = ! b
  t[+b].push(a.shift())
}
u = t[0].map(x => x * 2).map(x => x.toString(10)).join('').split('').reduce((a, b) => +a + +b)
console.log(u + t[1].reduce((a, b) => a + b))
// 80

Here’s my version —

Continued…

const validateCard = card => {
  t = [[],[]]
  a = card.slice()
  b = 1
  while (a.length) {
    b = ! b
    t[+b].push(a.shift())
  }
  u = t[0].map(x => (x * 2).toString(10)).join('').split('').reduce((a, b) => +a + +b)
  v = u + t[1].reduce((a, b) => a + b)
  w = v % 10 === 0
  return w 
}
 > batch = [ valid1, valid2, valid3, valid4, valid5 ]
<- [...]
 > batch.forEach(x => console.log(x, validateCard(x)))
   > (16) [4, 5, 3, 9, 6, 7, 7, 9, 0, 8, 0, 1, 6, 8, 0, 8] true
   > (16) [5, 5, 3, 5, 7, 6, 6, 7, 6, 8, 7, 5, 1, 4, 3, 9] true
   > (15) [3, 7, 1, 6, 1, 2, 0, 1, 9, 9, 8, 5, 2, 3, 6] false
   > (16) [6, 0, 1, 1, 1, 4, 4, 3, 4, 0, 6, 8, 2, 9, 0, 5] true
   > (16) [4, 5, 3, 9, 4, 0, 4, 9, 6, 7, 8, 6, 9, 6, 6, 6] true
<- undefined

Tested on my own cc and got true.


Disclaimer

This is not the Luhn algorithm in the strictest sense, but it works in this scenario very close along those lines. We could play around with the short array and see if we can get it to pass, too.


Notes

t[0] is all the elements at even indices; and, t[1] is all the elements at odd indices. The mechanics are pretty simple. Toggle between 1 and 0 and address that array element, which as it turns out is an array so we can pick an element to address.

Bingo! All five ‘trues’ as would be expected…

provisional
validateCard = card => {
  t = [[],[]]
  a = card.slice()
  b = ! (a.length % 2)
  while (a.length) {
    b = ! b
    t[+b].push(a.shift())
  }
  u = t[0].map(x => (x * 2).toString(10)).join('').split('').reduce((a, b) => +a + +b)
  v = u + t[1].reduce((a, b) => a + b)
  w = v % 10 === 0
  return w 
}

Again ‘true’ on my cc.

still
const validateCard = card => {
  const add = (a, b) => +a + +b
  t = [[],[]]
  a = card.slice()
  b = ! (a.length % 2)
  while (a.length) {
    b = ! b
    t[+b].push(a.shift())
  }
  u = t[0].map(x => (x * 2).toString(10)).join('').split('').reduce(add)
  v = u + t[1].reduce(add)
  w = v % 10 === 0
  return w 
}

Hey y’all; first time poster.
Here’s a link to my gitHub repository. If someone could check and give me feedback, I’d appreciate it.
Thanks in advance!

The logic might have a chink in its armor…

[1,2,3,'a'].some(isNaN)
true
[1,2,3,true].some(isNaN)
false
 > let n
<- undefined
 > 2 + n
<- NaN
 > [1, 2, 3, 2 + n].some(isNaN)
<- true
 >

Ok thanks. I went and updated the code to add this at the top:
function validateCred(arr) {

if (arr.length < 13 || !arr.every(Number.isInteger)) {
    console.log('Entered if block', arr)
    return false;
}

I know it wasn’t included in the instructions, but min length of a CC is 13, at least according to a very quick google search. Thanks.

1 Like

Took a lot more than 40 mins, but it was worth it. Extremely satisfied with my results.
if (You’re facing issues in your program) {
Feel free to take a look at my project;
}


Here is my code…
Please inform about any mistakes.
Thanks in advance

Still messing around, but creeping forward…

const stringify = x => x.map(x => x.toString()).join('')
const validateCard = card => {
  const add = (a, b) => +a + +b
  t = [[],[]]
  a = card.slice()
  b = ! (a.length % 2)
  while (a.length) {
    b = ! b
    t[+b].push(a.shift())
  }
  u = t[0].map(x => (x * 2).toString(10)).join('').split('').reduce(add)
  v = u + t[1].reduce(add)
  w = v % 10 === 0
  return w 
}
const findValidCards = sample => {
  return sample.filter(validateCard)
}
const findInvalidCards = sample => {
  return sample.filter(x => ! validateCard(x))
}
const valid = [
  [4, 5, 3, 9, 6, 7, 7, 9, 0, 8, 0, 1, 6, 8, 0, 8],
  [5, 5, 3, 5, 7, 6, 6, 7, 6, 8, 7, 5, 1, 4, 3, 9],
  [3, 7, 1, 6, 1, 2, 0, 1, 9, 9, 8, 5, 2, 3, 6],
  [6, 0, 1, 1, 1, 4, 4, 3, 4, 0, 6, 8, 2, 9, 0, 5],
  [4, 5, 3, 9, 4, 0, 4, 9, 6, 7, 8, 6, 9, 6, 6, 6]
]
const invalid = [
  [4, 5, 3, 2, 7, 7, 8, 7, 7, 1, 0, 9, 1, 7, 9, 5],
  [5, 7, 9, 5, 5, 9, 3, 3, 9, 2, 1, 3, 4, 6, 4, 3],
  [3, 7, 5, 7, 9, 6, 0, 8, 4, 4, 5, 9, 9, 1, 4],
  [6, 0, 1, 1, 1, 2, 7, 9, 6, 1, 7, 7, 7, 9, 3, 5],
  [5, 3, 8, 2, 0, 1, 9, 7, 7, 2, 8, 8, 3, 8, 5, 4]
]
const mystery = [
  [3, 4, 4, 8, 0, 1, 9, 6, 8, 3, 0, 5, 4, 1, 4],
  [5, 4, 6, 6, 1, 0, 0, 8, 6, 1, 6, 2, 0, 2, 3, 9],
  [6, 0, 1, 1, 3, 7, 7, 0, 2, 0, 9, 6, 2, 6, 5, 6, 2, 0, 3],
  [4, 9, 2, 9, 8, 7, 7, 1, 6, 9, 2, 1, 7, 0, 9, 3],
  [4, 9, 1, 3, 5, 4, 0, 4, 6, 3, 0, 7, 2, 5, 2, 3]
]
const test = valid.concat(invalid).concat(mystery)
//test.forEach(x => console.log(validateCard(x)))
console.log('Valid')
allValid = findValidCards(test)
allValid.forEach(x => console.log(stringify(x)))
console.log('Invalid')
allInvalid = findInvalidCards(test)
allInvalid.forEach(x => console.log(stringify(x)))

Output

Valid
4539677908016808
5535766768751439
371612019985236
6011144340682905
4539404967869666
5466100861620239
4913540463072523
Invalid
4532778771091795
5795593392134643
375796084459914
6011127961777935
5382019772883854
344801968305414
6011377020962656203
4929877169217093
brands = {3: 'Amex', 4: 'Visa', 5: 'Mastercard', 6: 'Discovery'}
warns = Array.from(new Set(allInvalid.map(x => brands[x[0]])))
console.log(warns)
[ 'Visa', 'Mastercard', 'Amex', 'Discovery' ]

Here’s my solution!!

Hi guys! This was pretty challenging, but it was worth it.
Here is my code.
It’s working ok. If anyone has any suggestions on how to improve it, I would appreciate very much. 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];

// 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:


// ---------------------------------------
// return card's condition (valid/invalid)
function validateCred(array) {
  let momentSum = 0;
  let condition;
  if (array.length % 2 === 0) {
    for (i = array.length-1; i >= 0; i -= 2) {
      let localSum = 0;
      let doubled = 2 * array[i-1];
      if (doubled < 9) {
        localSum = array[i] + doubled;
        momentSum += localSum;
      } else {
        let reduced = doubled - 9;
        localSum = array[i] + reduced;
        momentSum += localSum;
      }
    }
    if (momentSum % 10 === 0) {
      condition = 'valid number';
      return condition;
    } else {
      condition = 'invalid number';
      return condition;
    }
  } else if (array.length % 2 !== 0) {
    let number = array.pop();
    for (let i = array.length-1; i >=0; i-=2) {
      let localSum = 0;
      let doubled = 2 * array[i];
      if (doubled < 9) {
        localSum = doubled + array[i-1];
        momentSum += localSum;
      } else {
        let reduced = doubled - 9;
        localSum = reduced + array[i-1];
        momentSum += localSum;
      }
    }
    momentSum += number;
    if (momentSum % 10 === 0) {
      condition = 'valid number';
      return condition;
    } else {
      condition = 'invalid number';
      return condition;
    }
  }
};
// console.log(validateCred(mystery5));
// console.log('\n');


// ---------------------------------------
// return array of invalid cards
function findInvalidCards(arrayGroup) {
  let validCardArray = [];
  let invalidCardArray = [];
  for (let j = 0; j < arrayGroup.length; j++) {
    if (validateCred(arrayGroup[j]) === 'valid number') {
      validCardArray.push((arrayGroup[j]));
    } else {
      invalidCardArray.push(arrayGroup[j]);
    }
  }
  return invalidCardArray;
};
let invalidCards = findInvalidCards(batch);
// console.log(invalidCards);
// console.log('\n');


// ---------------------------------------
//return array of identified companies
function idInvalidCardCompanies(array) {
  let companiesArray = [];
  array.forEach(numArray => {
    let company = '';
    if (numArray[0] === 3) { 
      company = 'American Express';
    } else if (numArray[0] === 4) {
      company = 'Visa';
    } else if (numArray[0] === 5) {
      company = 'MasterCard';
    } else if (numArray[0] === 6) {
      company = 'Discover';
    } else {
      console.log('Company not Found')
    }
    if (company) {
      companiesArray.push(company);
    }
  })
  companiesArray = companiesArray.filter((value, i, array) => array.indexOf(value) === i);
  return companiesArray;
};
let invalidCompanies = idInvalidCardCompanies(invalidCards);
// console.log(invalidCompanies);

Here is my code <3