|
| 1 | +/* |
| 2 | +Here are the rules for a valid number: |
| 3 | +
|
| 4 | +- Number must be 16 digits, all of them must be numbers. |
| 5 | +- You must have at least two different digits represented (all of the digits cannot be the same). |
| 6 | +- The final digit must be even. |
| 7 | +- The sum of all the digits must be greater than 16. |
| 8 | +
|
| 9 | +For example, the following credit card numbers are valid: |
| 10 | +
|
| 11 | +```markdown |
| 12 | +9999777788880000 |
| 13 | +6666666666661666 |
| 14 | +``` |
| 15 | +
|
| 16 | +And the following credit card numbers are invalid: |
| 17 | +
|
| 18 | +```markdown |
| 19 | +a92332119c011112 (invalid characters) |
| 20 | +4444444444444444 (only one type of number) |
| 21 | +1111111111111110 (sum less than 16) |
| 22 | +6666666666666661 (odd final number) |
| 23 | +``` |
| 24 | +
|
| 25 | +These are the requirements your project needs to fulfill: |
| 26 | +
|
| 27 | +- Make a JavaScript file with a name that describes its contents. |
| 28 | +- Create a function with a descriptive name which makes it clear what the function does. The function should take one argument, the credit card number to validate. |
| 29 | +- Write at least 2 comments that explain to others what a line of code is meant to do. |
| 30 | +- Return a boolean from the function to indicate whether the credit card number is valid. |
| 31 | +
|
| 32 | +Good luck! |
| 33 | +*/ |
| 34 | + |
| 35 | +const validateCard = (card) => { |
| 36 | + const cardStr = String(card); |
| 37 | + // Must have 16 digits |
| 38 | + const has16Digits = cardStr.length === 16; |
| 39 | + // Must be all digits |
| 40 | + const isAllDigits = /^\d+$/.test(cardStr); |
| 41 | + // Must have at least 2 numbers |
| 42 | + const hasMoreThanOneDigit = cardStr.length > 1; |
| 43 | + // Final digit must be even |
| 44 | + const hasEvenFinalDigit = isAllDigits && (Number(cardStr) % 10) % 2 === 0; |
| 45 | + // Sum of numbers must be at least 16 |
| 46 | + const sumOfNumbers = isAllDigits |
| 47 | + ? cardStr.split("").reduce((acc, num) => acc + Number(num), 0) |
| 48 | + : 0; |
| 49 | + |
| 50 | + const hasSumOver15 = sumOfNumbers > 15; |
| 51 | + |
| 52 | + // Must have more than one type of number |
| 53 | + const hasMultipleDistinctDigits = new Set(cardStr.split("")).size > 1; |
| 54 | + |
| 55 | + return has16Digits && |
| 56 | + hasMoreThanOneDigit && |
| 57 | + hasEvenFinalDigit && |
| 58 | + hasSumOver15 && |
| 59 | + hasMultipleDistinctDigits |
| 60 | + ? "Valid card" |
| 61 | + : "Invalid card"; |
| 62 | +}; |
| 63 | + |
| 64 | +console.log(validateCard(9999777788880000)); // valid |
| 65 | +console.log(validateCard(6666666666661666)); // valid |
| 66 | +console.log(validateCard("a92332119c011112")); // Invalid |
| 67 | +console.log(validateCard(4444444444444444)); // Invalid |
| 68 | +console.log(validateCard(1111111111111110)); // Invalid |
| 69 | +console.log(validateCard(6666666666666661)); // Invalid |
| 70 | + |
| 71 | +module.exports = validateCard; |
0 commit comments