Skip to content

Commit 1834231

Browse files
committed
strech tasks are done
1 parent 2a7471c commit 1834231

File tree

6 files changed

+160
-43
lines changed

6 files changed

+160
-43
lines changed

Sprint-3/2-practice-tdd/repeat.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1-
function repeat() {
2-
return "hellohellohello";
1+
function repeat(str, count) {
2+
if (count < 0) {
3+
throw new Error("Count must be a non-negative integer");
4+
}
5+
6+
let result = "";
7+
for (let i = 0; i < count; i++) {
8+
result += str;
9+
}
10+
return result;
311
}
412

513
module.exports = repeat;

Sprint-3/2-practice-tdd/repeat.test.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// Implement a function repeat
21
const repeat = require("./repeat");
32
// Given a target string str and a positive integer count,
43
// When the repeat function is called with these inputs,
@@ -21,12 +20,34 @@ test("should repeat the string count times", () => {
2120
// When the repeat function is called with these inputs,
2221
// Then it should return the original str without repetition, ensuring that a count of 1 results in no repetition.
2322

23+
test("should return the original string when count is 1", () => {
24+
const str = "hi";
25+
const count = 1;
26+
const repeatedStr = repeat(str, count);
27+
expect(repeatedStr).toEqual("hi");
28+
});
29+
2430
// case: Handle Count of 0:
2531
// Given a target string str and a count equal to 0,
2632
// When the repeat function is called with these inputs,
2733
// Then it should return an empty string, ensuring that a count of 0 results in an empty output.
2834

35+
test("should return an empty string when count is 0", () => {
36+
const str = "hi";
37+
const count = 0;
38+
const repeatedStr = repeat(str, count);
39+
expect(repeatedStr).toEqual("");
40+
});
41+
2942
// case: Negative Count:
3043
// Given a target string str and a negative integer count,
3144
// When the repeat function is called with these inputs,
3245
// Then it should throw an error or return an appropriate error message, as negative counts are not valid.
46+
47+
test("should throw an error when count is negative", () => {
48+
const str = "hi";
49+
const count = -2;
50+
expect(() => repeat(str, count)).toThrow(
51+
"Count must be a non-negative integer"
52+
);
53+
});
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Function to validate a credit card number
2+
function isValidCreditCard(cardNumber) {
3+
// Convert the input to a string to handle numbers safely
4+
const cardStr = String(cardNumber);
5+
6+
// Rule 1: Must be 16 digits, all numbers
7+
if (!/^\d{16}$/.test(cardStr)) {
8+
return false;
9+
}
10+
11+
// Rule 2: Must contain at least two different digits
12+
const uniqueDigits = new Set(cardStr);
13+
if (uniqueDigits.size < 2) {
14+
return false;
15+
}
16+
17+
// Rule 3: Final digit must be even
18+
const lastDigit = Number(cardStr[cardStr.length - 1]);
19+
if (lastDigit % 2 !== 0) {
20+
return false;
21+
}
22+
23+
// Rule 4: Sum of all digits must be greater than 16
24+
const sum = cardStr.split("").reduce((acc, digit) => acc + Number(digit), 0);
25+
if (sum <= 16) {
26+
return false;
27+
}
28+
29+
// If all checks pass, the card number is valid
30+
return true;
31+
}
32+
33+
// Example test cases
34+
console.log(isValidCreditCard("9999777788880000")); // true
35+
console.log(isValidCreditCard("6666666666661666")); // true
36+
console.log(isValidCreditCard("a92332119c011112")); // false
37+
console.log(isValidCreditCard("4444444444444444")); // false
38+
console.log(isValidCreditCard("1111111111111110")); // false
39+
console.log(isValidCreditCard("6666666666666661")); // false
40+
41+
// Export function for testing in other files
42+
module.exports = isValidCreditCard;

Sprint-3/3-stretch/find.js

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,30 @@
1+
// Function to find the first occurrence of a character in a string
12
function find(str, char) {
3+
// Start searching from index 0
24
let index = 0;
35

6+
// Loop through the string until the end
47
while (index < str.length) {
8+
// Check if the current character matches the target
59
if (str[index] === char) {
6-
return index;
10+
return index; // Return the index if found
711
}
8-
index++;
12+
index++; // Move to the next character
913
}
14+
15+
// Return -1 if character is not found
1016
return -1;
1117
}
1218

13-
console.log(find("code your future", "u"));
14-
console.log(find("code your future", "z"));
15-
16-
// The while loop statement allows us to do iteration - the repetition of a certain number of tasks according to some condition
17-
// See the docs https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/while
19+
// Example calls
20+
console.log(find("code your future", "u")); // 5 (first 'u')
21+
console.log(find("code your future", "z")); // -1 (not found)
1822

19-
// Use the Python Visualiser to help you play computer with this example and observe how this code is executed
20-
// Pay particular attention to the following:
23+
/*
24+
Explanations:
2125
22-
// a) How the index variable updates during the call to find
23-
// b) What is the if statement used to check
24-
// c) Why is index++ being used?
25-
// d) What is the condition index < str.length used for?
26+
a) How the index variable updates during the call to find? index variable: Tracks the current position in the string during the search.
27+
b) What is the if statement used to check? if statement: Checks if the current character matches the one we're looking for.
28+
c) Why is index++ being used? index++: Increments index by 1 to move to the next character in each iteration.
29+
d) What is the condition index? condition index < str.length: Ensures the loop stops when we reach the end of the string to prevent errors.
30+
*/
Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,29 @@
1-
function passwordValidator(password) {
2-
return password.length < 5 ? false : true
3-
}
1+
// Array of previous passwords (example)
2+
const previousPasswords = ["Password1!", "Hello123#", "Test*2024"];
3+
4+
// Function to check if a password is valid
5+
function isValidPassword(password) {
6+
// Rule 1: At least 5 characters
7+
if (password.length < 5) return false;
8+
9+
// Rule 2: At least one uppercase letter
10+
if (!/[A-Z]/.test(password)) return false;
11+
12+
// Rule 3: At least one lowercase letter
13+
if (!/[a-z]/.test(password)) return false;
414

15+
// Rule 4: At least one number
16+
if (!/[0-9]/.test(password)) return false;
17+
18+
// Rule 5: At least one non-alphanumeric symbol
19+
if (!/[!#$%.*&]/.test(password)) return false;
20+
21+
// Rule 6: Must not be in previous passwords array
22+
if (previousPasswords.includes(password)) return false;
23+
24+
// All checks passed
25+
return true;
26+
}
527

6-
module.exports = passwordValidator;
28+
// Export function for Jest testing
29+
module.exports = isValidPassword;
Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,44 @@
1-
/*
2-
Password Validation
1+
const isValidPassword = require("./password-validator");
32

4-
Write a program that should check if a password is valid
5-
and returns a boolean
3+
// Test 1: Password has at least 5 characters and is valid
4+
test("password has at least 5 characters", () => {
5+
const password = "Abc1!";
6+
const result = isValidPassword(password);
7+
expect(result).toEqual(true);
8+
});
69

7-
To be valid, a password must:
8-
- Have at least 5 characters.
9-
- Have at least one English uppercase letter (A-Z)
10-
- Have at least one English lowercase letter (a-z)
11-
- Have at least one number (0-9)
12-
- Have at least one of the following non-alphanumeric symbols: ("!", "#", "$", "%", ".", "*", "&")
13-
- Must not be any previous password in the passwords array.
10+
// Test 2: Password too short
11+
test("password shorter than 5 characters is invalid", () => {
12+
const password = "A1!a";
13+
expect(isValidPassword(password)).toEqual(false);
14+
});
1415

15-
You must breakdown this problem in order to solve it. Find one test case first and get that working
16-
*/
17-
const isValidPassword = require("./password-validator");
18-
test("password has at least 5 characters", () => {
19-
// Arrange
20-
const password = "12345";
21-
// Act
22-
const result = isValidPassword(password);
23-
// Assert
24-
expect(result).toEqual(true);
25-
}
26-
);
16+
// Test 3: Missing uppercase
17+
test("password without uppercase letter is invalid", () => {
18+
const password = "abc1!";
19+
expect(isValidPassword(password)).toEqual(false);
20+
});
21+
22+
// Test 4: Missing lowercase
23+
test("password without lowercase letter is invalid", () => {
24+
const password = "ABC1!";
25+
expect(isValidPassword(password)).toEqual(false);
26+
});
27+
28+
// Test 5: Missing number
29+
test("password without number is invalid", () => {
30+
const password = "Abcde!";
31+
expect(isValidPassword(password)).toEqual(false);
32+
});
33+
34+
// Test 6: Missing special character
35+
test("password without special character is invalid", () => {
36+
const password = "Abcde1";
37+
expect(isValidPassword(password)).toEqual(false);
38+
});
39+
40+
// Test 7: Password already used
41+
test("password that is already used is invalid", () => {
42+
const password = "Password1!";
43+
expect(isValidPassword(password)).toEqual(false);
44+
});

0 commit comments

Comments
 (0)