|
1 | 1 | function cardValidator(num) { |
2 | | - const numStr = num.toString(); // Convert to string for iteration |
| 2 | + const numStr = num.toString(); // Convert to string for iteration with this we can check the length and individual checks |
3 | 3 | if (numStr.length !== 16 || !/^[0-9]+$/.test(numStr) || numStr[numStr.length -1] % 2 === 1) { |
| 4 | + console.log("I stopped because one of my condition is true") |
4 | 5 | return false; // Check length is 16 numbers long, if it only contains digits and if the last digits is even |
5 | 6 | } |
6 | 7 |
|
7 | | - let firstDigit = numStr[0]; // Track the first digit |
| 8 | + let firstDigit = numStr[0]; // Track the first digit to compare with the rest digits |
8 | 9 | for (let i = 1; i < numStr.length; i++) { |
| 10 | + // console.log(`compare ${numStr[i]} con ${firstDigit} in position ${i}`); |
| 11 | + |
9 | 12 | if (numStr[i] !== firstDigit) { |
| 13 | + // console.log(`diferent digit found: ${numStr[i]}`); |
10 | 14 | return true; // Found at least two different digits |
11 | 15 | } |
12 | 16 | } |
13 | | - |
| 17 | + // console.log("all digits are distint to same."); |
| 18 | + console.log(`The last digit (${numStr[numStr.length - 1]}) is odd, so the validation fails.`); |
14 | 19 | return false; // All digits are the same |
15 | 20 | } |
16 | 21 |
|
17 | | -console.log(cardValidator(1111111111111111)); // Output: false |
18 | | -console.log(cardValidator(1234567890123456)); // Output: true |
19 | | -console.log(cardValidator(2222222222222222)); // Output: false |
20 | | -console.log(cardValidator(3333333333333334)); // Output: true |
| 22 | +console.log(cardValidator(1111111111111111)); // Output: false and stop in the first return. |
| 23 | +// console.log(cardValidator(1234567890123456)); // Output: true |
| 24 | +// console.log(cardValidator(2222222222222222)); // Output: false |
| 25 | +// console.log(cardValidator(3333333333333334)); // Output: true |
21 | 26 |
|
0 commit comments