Credit Card Checker project

Please I’m having an issue with this. I’m trying to find a way to create a function that returns the companies that give invalid card numbers(the last instruction). This is my code from the beginning:

const validateCred = (card) => {
  let newCard = card.reverse();
  //return newCard;
  for (let i = 0; i < newCard.length; i++){
    if (i % 2 != 0) {
      newCard[i] *= 2;
    if(newCard[i] > 9) {
      newCard[i] -= 9;
    }
}
  }
  let total = newCard.reduce((a,b) => a + b, 0)
   if (total % 10 == 0) {
     return true;
   } else {
     return false;
   }
  }
const findInvalidCards = (cards) =>{
  let invalidCard = [];
  for (let j = 0; j < cards.length; j++) {
    if(validateCred(cards[j]) === false) {
      invalidCard.push(cards[j])
      
    }
  }
  return invalidCard;
}
const idInvalidCardCompanies = (cards) => {
  let invalidCardCompanies = [];
 for (let k = 0; k < cards.length; k++){
   
  }
}
console.log(findInvalidCards(batch));

@bened03 Could you please enclose your code like this:

let myCode = true;
console.log(myCode);

Annotation%202019-10-07%20150741

I’ve been trying to. I don’t know how

Annotation%202019-10-07%20150741

Ok I’ve done that…

@bened03
Try logging your function findInvalidCards to see what the output is.
Notice that these credit card numbers are not in the list above…

// 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 = [invalid1, invalid2, invalid3, invalid4, invalid5];
// Add your functions below:
//t num = [1, 2, 3, 4, 5, 6];
const validateCred = (card) => {
  let newCard = card.reverse();
  //return newCard;
  for (let i = 0; i < newCard.length; i++){
    if (i % 2 != 0) {
      newCard[i] *= 2;
    if(newCard[i] > 9) {
      newCard[i] -= 9;
    }
}
  }
  let total = newCard.reduce((a,b) => a + b, 0)
   if (total % 10 == 0) {
     return true;
   } else {
     return false;
   }
  }
const findInvalidCards = (cards) =>{
  let invalidCard = [];
  for (let j = 0; j < cards.length; j++) {
    if(validateCred(cards[j]) === false) {
      invalidCard.push(cards[j])
      
    }
  }
  return invalidCard;
}

My findInvalidCards function is working. This is what I tried to come up with for the idInvalidCardCompanies function. I’m having errors.

const idInvalidCardCompanies = (cards) => {
  let invalidCardCompanies = [];
 for (let k = 0; k < cards.length; k++){
   if(validateCred(cards[k]) === false) {
     switch(cards[k]) {
       case 3:
         return 'Amex (American Express)';
         break;
       case 4:
         return 'Visa';
         break;
       case 5:
         return 'MasterCard';
         break;
       case 6:
         return 'Discover';
         break;
       default:
         return 'Company not found';
         break;
     }
     invalidCardCompanies.push(cards[k]); 
   }
   
  }
  return invalidCardCompanies;
}
console.log(idInvalidCardCompanies(batch));

This is what I get when I log your code:
Annotation%202019-10-07%20155011
This cards are not part of batch.

That is the result of findInvalidCard (batch);

Hello, @bened03

What @micro3227863613 is telling you is true. The card numbers output by your findInvalidCards() function are not found in any of the original data. Look at the card numbers. They should include all of the invalid cards, and whichever mystery cards are also invalid, but the numbers returned by your function do not match any of the original card numbers. Your function is mutating the original card number arrays. When we assign a new variable name to an existing array, we do not have a new stand alone copy of the array. We have 2 names referring to the same exact array. This line is where the problem begins:

newCard and card are 2 names for the same array.

Consider this simplified example:

const nums = [1, 2, 3];
const letters = nums;

for (let i = 0; i < letters.length; i++) {
  switch (letters[i]) {
    case 1:
      letters[i] = 'a';
      break;
    case 2:
      letters[i] = 'b';
      break;
    case 3:
      letters[i] = 'c';
      break;
  }
}

console.log(`nums is: ${nums}`);
console.log(`letters is: ${letters}`);

Output:

nums is: a,b,c
letters is: a,b,c

Even though I only used the variable name letters to make changes, printing the variable nums produces the same output. Both variables refer to the same array. There is only 1 array with 2 names.
You can make a copy of the array using the array.slice() method. Once you have the correct output for your findInvalidCards() function, we can tackle the idInvalidCardCompanies() function. It has a few issues as well.

1 Like

Thank you for sharing your code. I am struggling to understand, I do understand the luhn algorithm. In your code there is no mention of any of the arrays, batch, etc. How does it pull the array from batch and then pull the numbers from each array in batch? Thank you

Hey @simplelife42 which section of code are you referring to?

Could you explain why this is used, as im struggling to work out why?

Sure,

% represents a modulo operation. In this case modulo 2 of the index number. This means it looks at each index and tries to fit 2 as many times possible inside i and then sees what the remainder is.

I use this to determine uneven indexes. Because in case of an even number the remainder is 0.
For example 6, 2 fits 3 times inside 6 with a remainder of 0 ;).

Now I would like to find the uneven indexes for doubling so I do this by calling the modulo 2 which does not (!=) result in 0.

Hi,

I’m racking my head over the following code and unsure why it doesn’t work, can someone please spot the mistake?

It only returns false at the moment


const validateCred = arr => { 
  for (let i = arr.length - 1; i === 0; i --) // Iterating from left to right{
    if (i % 2 != 0) { // only iterating odd numbers
      arr[i] = arr[i] * 2; // multiplying each odd number by 2
      if (arr[i] > 9) { // checking if the multiplied digit is greater than 9
      arr[i] -= 9 // then subtracting it by 9
      } 
    }
     // do i need to push i into back into an array?
       const sum = arr.reduce((a,b) => a + b, 0) // getting the sum of the array
      if (sum % 10 === 0)  { // checking the if sum modulo is 0
        return true
      } else {
        return false
      }
 
    }
 
 console.log(validateCred(valid1))

Hello, @jafkam895798440384!

I could not run your code yet. But, it’s possible to see that the for looping expressions are not good.
Try something like:

for (let i = arr.length - 1; i >= 0; i--) {
  if () {
}
 }
1 Like

2 posts were split to a new topic: Credit card checker

A post was split to a new topic: Credit card checker

If you still have a question, please make a new topic. This one is becoming very cluttered. Thank you in advance.

2 Likes