Project Pains - Credit Card Checker | Intermediate Javascript

Challenge Project: Credit Card Checker | Codecademy

I’m struggling with Javascript in general. These last few lessons have had me working at a snail’s pace. And now I’m stuck on this project. Any help would be super appreciated!

const validateCred = arr => {
  for (let i = arr.length - 1; i >= 0; i--) {

    let val = arr[i];

    if (i % 2 === 0) {
      continue;
    }
    val *= 2;
    console.log('ARRI*2: ' + val);

    if (val > 9) {
      val -= 9;
      console.log('ARRI-9: ' + val);
    }

    let total = 0;

    for (let j = 0; j < arr.length; j++) {
      total += val;
      console.log('TOTAL: ' + total);
      return total % 10 === 0;
    }
  }
};

console.log(validateCred(valid1));
// const valid1 = [4, 5, 3, 9, 6, 7, 7, 9, 0, 8, 0, 1, 6, 8, 0, 8];
console.log(validateCred(invalid1));
// const invalid1 = [4, 5, 3, 2, 7, 7, 8, 7, 7, 1, 0, 9, 1, 7, 9, 5];
console.log(validateCred(mystery1));
// const mystery1 = [3, 4, 4, 8, 0, 1, 9, 6, 8, 3, 0, 5, 4, 1, 4];

This is my code. If y’all could pick it apart and tell me all the reasons why it’s not working, that would be great. I feel like I have a small knowledge gap when it comes to Javascript functionality that is having a huge impact on my current progress.

Code returns this to console:

##
Output-only Terminal

Output:

ARRI*2: 16
ARRI-9: 7
TOTAL: 7
false

ARRI*2: 10
ARRI-9: 1
TOTAL: 1
false

ARRI*2: 2
TOTAL: 2
false

// KNOWN ISSUES:
// it should be iterating through the rest of the numbers in each array
// the first one should be true. the last one is a mystery of whether it is true or false

Thank you!!

Put the initialization out of the for loop. Inside the for loop, it is reset with every new digit.
Inside the for loop, add val to total.

Remove the nested loop. That does not make sense here. Below the outer (and only) loop, just check if total % 10 is 0.

2 Likes

Thank you for taking the time and energy to look over my code and give me some feedback. I had been unable to revise until now. Luckily, with your help and an hour or so of messing with other parts and logic, my code is finally working as intended. Here’s the final result:
(You’ll notice I worked on the visual aspect a bit, which was just some fun I decided to have with it. It did also help with the troubleshooting quite a bit though.)

const validateCred = arr => {
  let total = 0;
  for (let i = arr.length - 1; i >= 0; i--) {
    let val = arr[i];
    if (i % 2 === 0 && i !== arr.length - 1) {
      console.log('New Value: ' + val);
      val *= 2;
      console.log('New Val*2 = ' + val + ' - Adding to Subtotal...');
    } else {
      console.log('');
      console.log('New Value: ' + val + ' - Adding to Subtotal...');
    }

    if (val > 9) {
      val -= 9;
      console.log('New Val-9: ' + val);
    }

    total += val;
    console.log('Subtotal: ' + total);
  }
    console.log('END TOTAL: ' + total + ' - Prepare for RESULT...');
    
    if (total % 10 === 0) {
      return 'VALID';
    } else {
      return 'INVALID';
    }
    console.log('');
};
1 Like