Step 12

Here is the completed Luhn algorithm:

const cnumber = "4003600000000014";

let arr = cnumber
  .split('')
  .reverse()
  .map(element => parseInt(element));

const sum = arr.reduce(reducer, 0)

function reducer(accumulator, currentValue, currentIndex) {
  currentIndex += 1; // account for 0-based indexing
  if (currentIndex % 2 === 0) { // even index
    currentValue *= 2;
    if (currentValue > 9) {
      currentValue -= 9;
    }
  }
  return accumulator + currentValue;
}

if (sum % 10 === 0) {
  console.log("Valid card number");
} else {
  console.log("Invalid card number");
}

You can try the above for another valid input, 6011329933655299.

Now you can complete the starter code by providing an implementation for the isCardNumberValid function:

function isCardNumberValid(cnumber) {
  let arr = cnumber
    .split("")
    .reverse()
    .map((element) => parseInt(element));

  const sum = arr.reduce(reducer, 0);

  function reducer(accumulator, currentValue, currentIndex) {
    currentIndex += 1; // account for 0-based indexing
    if (currentIndex % 2 === 0) {
      // even index
      currentValue *= 2;
      if (currentValue > 9) {
        currentValue -= 9;
      }
    }
    return accumulator + currentValue;
  }

  return sum % 10 === 0;
}

Here is the payment form in action: