I figured out the code checker. please review my code

// 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];
const valid6 = [4, 5, 3, 9, 6, 8, 9, 8, 8, 7, 7, 0, 5, 7, 9, 8];

// 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 = card => {
  
//reverse the varibale array

let newReverseCard = [...card].reverse(); 

//console.log(newReverseCard);
 
//create variable to go through a newReverseCard and pickup even numbers to a new variable called newReverseCardCalculated.

newReverseCardUncalculated = [
  newReverseCard[0],
  newReverseCard[2],
  newReverseCard[4],
  newReverseCard[6],
  newReverseCard[8],
  newReverseCard[10],
  newReverseCard[12],
  newReverseCard[14],
                 
   ];
  
//create variable to go through a newReverseCard and pickup odd numbers to a new variable called newReverseCardCalculated.
  
  newReverseCardCalculated = [
  newReverseCard[1],
  newReverseCard[3],
  newReverseCard[5],
  newReverseCard[7],
  newReverseCard[9],
  newReverseCard[11],
  newReverseCard[13],
  newReverseCard[15],
]
// preform the Luhn Algorythin on the newReverseCardCalculated = i * 2 then if i > 9 then i-9

let g = [];
let k = [];
let t = [];
  
for(let i = 0;i < newReverseCardCalculated.length; i++) {   

//step 1 -mulitplies the numbers by 2
let p= t.push(newReverseCardUncalculated[i]);
let jj=newReverseCardCalculated[i];
let j =newReverseCardCalculated[i]*2;


// step 2 check to see if they are larger then 9 if so subtract 9
//let h = []; // this is the elements of the array that is doubled -9
  
let h = [j].filter(x => x > 9);
    
////need to manipulate the J string to push out non doubled calculated numbers

if([j] < 9){
  g.push(j);  /////g//////
};
  
//these elements of the array have been doubled this makes the ones larger than 9 after doubling - subtract by 9 
let l = [];

if(h>9) {
 k.push([h]-9); 
  if(h<9){
   l.push([h]); 

 };
   

};



}; 

const reducer = (a, b) => a + b;
let q = g.reduce(reducer);
let r = k.reduce(reducer);
let s = t.reduce(reducer);
  

let z = r+q+s;

let modulo = z%10;
  
if(modulo === 0){
  return true;{
  if(modulo !== 0) {
      return false;
    }
  }
};
  
 
  
};

link https://www.codecademy.com/practice/projects/credit-card-checker

TLDR - help me make this code smaller.

So I’ve figured out the credit card checker and here is the validatecredt() portion of my code. AND IT WORKS! I’m a total newbie and this when I read the challenge I never through I would be able to make this work. So completing it (without cheating)is a huge success for me. I learned so much about passing functions parameters. One of my major dislikes of learning javascript is that they teach you a method and then teach you ten ways to write it before you ever have a chance on understanding how the method works. So this is really using basic rudimentary syntax from the lessons in code academy.

That being said, Since finishing it I’ve seen other people write the same code in 10 lines.

When you see my code here you can see how I made my way logically through the problem.

What do I not understand? I feel like I’m relying heavily on creating new variables…

Thanks.

I see you have some hard-coded variables. Have you tried running this with a longer credit card like mystery3? What about some of the test ones from this website from the last step?
And yes you are very heavily relying on new variables.

yes, it runs the whole batch including mystery3. I didn’t post that section of code.

I see what you mean there. I was having trouble interating through the batch but as I worked the process I have a better understanding now I’ll give it another go.

Okay! Now there are two ways you can solve this, one way is to write a hard-coded way of doing the luhn algorithm for each individual case, but this will lead to repetition which we definitely do not want. The other way is to figure out how to traversing an array backwards, no matter what the size is, and use the luhn algorithm.

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.