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
92 lines
1.7 KiB
Markdown
92 lines
1.7 KiB
Markdown
---
|
|
id: 6a0dcd03ee4e68698080ef6c
|
|
title: "Challenge 308: Credit Card Validator"
|
|
challengeType: 28
|
|
dashedName: 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`.
|
|
|
|
```js
|
|
assert.isTrue(isValidCard("4532015112830366"));
|
|
```
|
|
|
|
`isValidCard("5425233430109903")` should return `true`.
|
|
|
|
```js
|
|
assert.isTrue(isValidCard("5425233430109903"));
|
|
```
|
|
|
|
`isValidCard("371449635398431")` should return `true`.
|
|
|
|
```js
|
|
assert.isTrue(isValidCard("371449635398431"));
|
|
```
|
|
|
|
`isValidCard("6011111111111117")` should return `true`.
|
|
|
|
```js
|
|
assert.isTrue(isValidCard("6011111111111117"));
|
|
```
|
|
|
|
`isValidCard("4532015112830367")` should return `false`.
|
|
|
|
```js
|
|
assert.isFalse(isValidCard("4532015112830367"));
|
|
```
|
|
|
|
`isValidCard("1234567890123456")` should return `false`.
|
|
|
|
```js
|
|
assert.isFalse(isValidCard("1234567890123456"));
|
|
```
|
|
|
|
`isValidCard("4532015112830368")` should return `false`.
|
|
|
|
```js
|
|
assert.isFalse(isValidCard("4532015112830368"));
|
|
```
|
|
|
|
# --seed--
|
|
|
|
## --seed-contents--
|
|
|
|
```js
|
|
function isValidCard(number) {
|
|
|
|
return number;
|
|
}
|
|
```
|
|
|
|
# --solutions--
|
|
|
|
```js
|
|
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;
|
|
}
|
|
```
|