Credit Card Checked JavaScript Challenge: Code Review

Hi,

I am working through the JavaScript ‘Full Stack Path’ and have just created the following algorithm to solve the credit card checker problem. 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

Would someone be kind enough to review my code and let me know how I could improve? What other solutions could I have used to ensure every other element from end to start was manipulated as per the instructions? As you can see I used a boolean implement this as I couldn’t work out how I could use the modular operator on an array that was iterated over in reverse and could be of varying length.

Thank you! - Kit

Question:

Create a function, validateCred() that has a parameter of an array. The purpose of validateCred() is to return true when an array contains digits of a valid credit card number and false when it is invalid. This function should NOT mutate the values of the original array.

To find out if a credit card number is valid or not, use the Luhn algorithm. Generally speaking, an algorithm is a series of steps that solve a problem — the Luhn algorithm is a series of mathematical calculations used to validate certain identification numbers, e.g. credit card numbers. The calculations in the Luhn algorithm can be broken down as the following steps:

  1. Starting from the farthest digit to the right, AKA the check digit, iterate to the left.
  2. As you iterate to the left, every other digit is doubled (the check digit is not doubled). If the number is greater than 9 after doubling, subtract 9 from its value.
  3. Sum up all the digits in the credit card number.
  4. If the sum modulo 10 is 0 (if the sum divided by 10 has a remainder of 0 ) then the number is valid, otherwise, it’s invalid.

Here’s a visual that outlines the steps. Check your function using both the provided valid and invalid numbers.

My Solution:

function validateCred(array) {
  const output = [];
  let double = false;
  let sum;

  // Loop over each element in array from last to first.
  for (let i = array.length - 1; i >= 0; i--) {
    // Process every other element.
    if (double) {
      let doubled = array[i] * 2; // Double value in array.
      if (doubled > 9) {
        // If 'doubled' is greater than 9, minus 9.
        doubled = doubled - 9;
      }
      output.unshift(doubled); // Add processed value to 'output' array.
      double = false;
    } else {
      output.unshift(array[i]); // Add unprocessed value to 'output' array.
      double = true;
    }
  }

  // Add up all elements in 'output' array.
  sum = output.reduce((total, currentValue) => total += currentValue, 0);

  // Check whether total mod 10 is equal to 0. 
  return sum % 10 === 0;
}

Hello, @howiek12.

Your code looks good. That is an efficient way to implement the Luhn Algorithm. You are only iterating through the card number array once which is good. One thing you could consider, though there is nothing wrong with your current implementation, would be toggling your boolean variable outside your if...else structure as the last task of your for loop.

Hint:
let myBool = false;

for (let i = 0;  i < 5; i++) {
  console.log(myBool);
  myBool = !myBool; // Toggle the value
}

Output:

false
true
false
true
false

1 Like

Thank you, really appreciate the advice. Will give this a go :grinning:

1 Like