Files
wehub-resource-sync dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 11:55:53 +08:00

1.7 KiB

id, title, challengeType, dashedName
id title challengeType dashedName
6a0dcd03ee4e68698080ef6c Challenge 308: Credit Card Validator 28 challenge-308

--description--

Given a string of digits for a credit card number, determine if it's a valid card number using the following method:

  • Starting from the second-to-last digit, double every other digit moving left.
  • If doubling a digit results in a number greater than 9, subtract 9.
  • Sum all the digits (doubled and undoubled).
  • If the total is divisible by 10, the number is valid.

--hints--

isValidCard("4532015112830366") should return true.

assert.isTrue(isValidCard("4532015112830366"));

isValidCard("5425233430109903") should return true.

assert.isTrue(isValidCard("5425233430109903"));

isValidCard("371449635398431") should return true.

assert.isTrue(isValidCard("371449635398431"));

isValidCard("6011111111111117") should return true.

assert.isTrue(isValidCard("6011111111111117"));

isValidCard("4532015112830367") should return false.

assert.isFalse(isValidCard("4532015112830367"));

isValidCard("1234567890123456") should return false.

assert.isFalse(isValidCard("1234567890123456"));

isValidCard("4532015112830368") should return false.

assert.isFalse(isValidCard("4532015112830368"));

--seed--

--seed-contents--

function isValidCard(number) {

  return number;
}

--solutions--

function isValidCard(number) {
  let sum = 0;

  for (let i = number.length - 1; i >= 0; i--) {
    let digit = parseInt(number[i]);

    if ((number.length - 1 - i) % 2 === 1) {
      digit *= 2;
      if (digit > 9) digit -= 9;
    }

    sum += digit;
  }

  return sum % 10 === 0;
}