Skip to content

Commit 89fcaf5

Browse files
committed
Added comment to explain how the code runs
1 parent 777d332 commit 89fcaf5

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed
Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
11
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
33
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")
45
return false; // Check length is 16 numbers long, if it only contains digits and if the last digits is even
56
}
67

7-
let firstDigit = numStr[0]; // Track the first digit
8+
let firstDigit = numStr[0]; // Track the first digit to compare with the rest digits
89
for (let i = 1; i < numStr.length; i++) {
10+
// console.log(`compare ${numStr[i]} con ${firstDigit} in position ${i}`);
11+
912
if (numStr[i] !== firstDigit) {
13+
// console.log(`diferent digit found: ${numStr[i]}`);
1014
return true; // Found at least two different digits
1115
}
1216
}
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.`);
1419
return false; // All digits are the same
1520
}
1621

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
2126

0 commit comments

Comments
 (0)