Skip to content

Commit ebd07d2

Browse files
committed
is-valid-triangle.js updated
1 parent a733352 commit ebd07d2

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed
Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,31 @@
11
function cardValidator(num) {
2-
const numStr = num.toString(); // Convert to string for iteration with this we can check the length and individual checks
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")
5-
return false; // Check length is 16 numbers long, if it only contains digits and if the last digits is even
2+
const numStr = num.toString(); // Convert to string for iteration and validation
3+
4+
// Initial validation conditions:
5+
if (
6+
numStr.length !== 16 || // Must be exactly 16 digits long
7+
!/^[0-9]+$/.test(numStr) || // Must contain only numbers
8+
numStr[numStr.length - 1] % 2 === 1 || // Last digit must be even
9+
numStr.split("").map(Number).reduce((acc, val) => acc + val, 0) <= 16 // Sum of digits must be greater than 16
10+
) {
11+
console.log("I stopped because one of my conditions is true");
12+
return false;
613
}
714

8-
let firstDigit = numStr[0]; // Track the first digit to compare with the rest digits
15+
// Check if all digits are the same:
16+
const firstDigit = numStr[0];
917
for (let i = 1; i < numStr.length; i++) {
10-
// console.log(`compare ${numStr[i]} con ${firstDigit} in position ${i}`);
11-
1218
if (numStr[i] !== firstDigit) {
13-
// console.log(`different digit found: ${numStr[i]}`);
1419
return true; // Found at least two different digits
1520
}
1621
}
17-
// console.log("all digits are distint to same.");
22+
1823
console.log(`The last digit (${numStr[numStr.length - 1]}) is odd, so the validation fails.`);
1924
return false; // All digits are the same
2025
}
2126

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
26-
27+
// Test cases:
28+
console.log(cardValidator(1111111111111111)); // Output: false (all digits are the same)
29+
console.log(cardValidator(1234567890123456)); // Output: true (valid card)
30+
console.log(cardValidator(2222222222222222)); // Output: false (all digits are the same)
31+
console.log(cardValidator(3333333333333334)); // Output: true (valid card)

Sprint-3/revise/implement/count.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919

2020
function countChar(str, char) {
21-
21+
2222
let findSameLetter = 0;
2323
for(let i = 0; i < str.length; i++){
2424
if(str[i] === char){

0 commit comments

Comments
 (0)