Credit card checker (step 1)

https://www.codecademy.com/paths/full-stack-engineer-career-path/tracks/fscp-javascript-syntax-part-ii/modules/fecp-challenge-project-credit-card-checker/projects/credit-card-checker

Hello all, I´m doing the Credit Card checker project ofr Java Script in the full Stack carreer path, and… I’m not sure what I’m doing wrong.
Upon testing, it seems the issue is with the “sum” variable, which is suposed to return the NDigit-1 (aka, the last number of the reversed array) in order to complete the validation; but then again, i’m not entirely sure.
Here is my code:

const validateCred = (arr) => {
  let nDigits = arr.length;
  let sum = arr.reverse(nDigits[- 1]);
  let parity = nDigits % 2;
  for (let i=0; i <= nDigits.length-2;i -= 2) {
    let digit = nDigits[i];
    if (i % 2 === parity) {
      digit === digit * 2;
    }
    if (digit > 9) {
      digit === digit - 9;
    }
  }
  return (sum % 10) === 0;
}
console.log(validateCred(valid1))

This is returning false, no matter what array is evaluated. Then again, is my guess the issue is in the sum variable declaration

Hi Miguel,

Inside your if blocks you probably want to change the value of digit which you don’t due to the use of the wrong operator.

Also don’t forget to follow the advice from the lesson, step 3:

This function should NOT mutate the values of the original array.

This will be crucial in the next steps.

1 Like

I’m quite new to all this, so I’ll apologize ofr the dumb question:
Does it mean it would be better if I define the conditional truthy as a return?
i.e: return digit - 9

Your conditional is fine. That’s the one inside the parenthesis. There you check whether your digit is larger than nine and should be altered or not. If it is, you’re entering the code block inside the curly braces. There you want to do the action: Subtract nine from the digit. But you aren’t doing that. Instead you’re doing another comparison: digit === digit - 9. Which in words is:
Is digit the same as digit - 9?

1 Like