Skip to content
20 changes: 19 additions & 1 deletion Sprint-3/2-practice-tdd/count.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
function countChar(stringOfCharacters, findCharacter) {
return 5
if (typeof stringOfCharacters!= "string") {
throw new Error("Input should be a string");
}
if (typeof findCharacter !== "string" || findCharacter.length != 1){
throw new Error ("Input must be a single character");
}

let count = 0;
for(let char of stringOfCharacters) {
if (char === findCharacter) {
count++;
}
}
return count;

}

module.exports = countChar;

console.log(countChar("amazon", "a"));

// Added lines to check cases and return count of characters.
52 changes: 48 additions & 4 deletions Sprint-3/2-practice-tdd/count.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,51 @@ test("should count multiple occurrences of a character", () => {
});

// Scenario: No Occurrences
// Given the input string str,
// And a character char that does not exist within the case-sensitive str,
// When the function is called with these inputs,
// Then it should return 0, indicating that no occurrences of the char were found in the case-sensitive str.
test("should return 0 when the character is not in the string", () => {
const str = "hello";
const char = "x";
const count = countChar(str, char);
expect(count).toEqual(0);
});

// Scenario: Case Sensitivity

test("should be case-sensitive when counting characters", () => {
const str = "Banana";
expect(countChar(str, "a")).toEqual(3);
expect(countChar(str, "A")).toEqual(0);
});

// Scenario: Empty String

test("should return 0 when the input string is empty", () => {
const str = "";
const char = "a";
const count = countChar(str, char);
expect(count).toEqual(0);
});

// Scenario : Special characters

test("should count special characters like spaces or punctuation", () => {
const str1 = "a b a b";
const char1 = " ";
expect(countChar(str1, char1)).toEqual(2);

const str2 = "NO !!!";
const char2 = "!";
expect(countChar(str2, char2)).toEqual(3);
});

// Scenario: Invalid inputs

test("should throw error for invalid string input", () => {
expect(() => countChar(123, "a")).toThrow("Input should be a string");
});

test("should throw error for invalid character input", () => {
expect(() => countChar("hello", "ab")).toThrow("Input must be a single character");
});


// Added different cases and test using npx jest
22 changes: 21 additions & 1 deletion Sprint-3/2-practice-tdd/get-ordinal-number.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
function getOrdinalNumber(num) {
return "1st";
if (!Number.isInteger(num) || num <= 0) {
throw new Error("Only positive integers are allowed");
}
// Handle Exception of 11, 12, 13
if (num % 100 >= 11 && num % 100 <= 13) {
return num + "th";
}
// Handle general last-digit cases
switch (num % 10) {
case 1:
return num + "st";
case 2:
return num + "nd";
case 3:
return num + "rd";
default:
return num + "th";
}
}


module.exports = getOrdinalNumber;

// Function implemented
35 changes: 34 additions & 1 deletion Sprint-3/2-practice-tdd/get-ordinal-number.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,39 @@ const getOrdinalNumber = require("./get-ordinal-number");
// When the number is 1,
// Then the function should return "1st"

test("should return '1st' for 1", () => {
// Case 1: 1 → Standard ordinals

test("should return correct ordinals for 1, 2, 3, 4, 21, 22, 23, 24", () => {
expect(getOrdinalNumber(1)).toEqual("1st");
expect(getOrdinalNumber(2)).toEqual("2nd");
expect(getOrdinalNumber(3)).toEqual("3rd");
expect(getOrdinalNumber(4)).toEqual("4th");
expect(getOrdinalNumber(21)).toEqual("21st");
expect(getOrdinalNumber(22)).toEqual("22nd");
expect(getOrdinalNumber(23)).toEqual("23rd");
expect(getOrdinalNumber(24)).toEqual("24th");
});

// Case 2: Exceptions 11, 12, 13
test("should handle exceptions 11, 12, 13 correctly", () => {
expect(getOrdinalNumber(11)).toEqual("11th");
expect(getOrdinalNumber(12)).toEqual("12th");
expect(getOrdinalNumber(13)).toEqual("13th");
expect(getOrdinalNumber(111)).toEqual("111th");
});

// Case 3: Large numbers
test("should handle larger numbers correctly", () => {
expect(getOrdinalNumber(101)).toEqual("101st");
expect(getOrdinalNumber(112)).toEqual("112th");
expect(getOrdinalNumber(123)).toEqual("123rd");
});

// Case 4: Invalid cases / inputs
test("should throw error for zero, negative or non-integer inputs", () => {
expect(() => getOrdinalNumber(0)).toThrow("Only positive integers are allowed");
expect(() => getOrdinalNumber(-5)).toThrow("Only positive integers are allowed");
expect(() => getOrdinalNumber(2.5)).toThrow("Only positive integers are allowed");
});

// Added possible and invalid cases and tested using npx jest
14 changes: 12 additions & 2 deletions Sprint-3/2-practice-tdd/repeat.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
function repeat() {
return "hellohellohello";
function repeat(str, count) {
if (typeof str !== "string") {
throw new Error("Input must be a string");
}
if (!Number.isInteger(count) || count < 0) {
throw new Error("Count must be a non-negative integer");
}

return str.repeat(count);
}

module.exports = repeat;


// Function implemented to repeat string count times.
41 changes: 29 additions & 12 deletions Sprint-3/2-practice-tdd/repeat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,41 @@ const repeat = require("./repeat");
// When the repeat function is called with these inputs,
// Then it should repeat the str count times and return a new string containing the repeated str values.

// Case 1: Repeats String coun times.

test("should repeat the string count times", () => {
const str = "hello";
const count = 3;
const repeatedStr = repeat(str, count);
expect(repeatedStr).toEqual("hellohellohello");
});

// case: handle Count of 1:
// Given a target string str and a count equal to 1,
// When the repeat function is called with these inputs,
// Then it should return the original str without repetition, ensuring that a count of 1 results in no repetition.
// case 2: handle Count of 1:

// case: Handle Count of 0:
// Given a target string str and a count equal to 0,
// When the repeat function is called with these inputs,
// Then it should return an empty string, ensuring that a count of 0 results in an empty output.
test("should return the original string when count is 1", () => {
const str = "hello";
const count = 1;
expect(repeat(str, count)).toEqual("hello");
});
// Returns the original str without repetition, ensuring that a count of 1 results in no repetition.

// case: Negative Count:
// Given a target string str and a negative integer count,
// When the repeat function is called with these inputs,
// Then it should throw an error or return an appropriate error message, as negative counts are not valid.
// case 3: Handle Count of 0:

test("should return an empty string when count is 0", () => {
const str = "hello";
const count = 0;
expect(repeat(str, count)).toEqual("");
});

// Returns an empty string, ensuring that a count of 0 results in an empty output.

// Case 4: Negative count

test("should throw an error when count is negative", () => {
const str = "hello";
const count = -2;
expect(() => repeat(str, count)).toThrow("Count must be a non-negative integer");
});
// Throws an error or return an appropriate error message, as negative counts are not valid.

// Tested for all case using npx jest
18 changes: 17 additions & 1 deletion Sprint-3/3-stretch/find.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,23 @@ console.log(find("code your future", "z"));
// Use the Python Visualiser to help you play computer with this example and observe how this code is executed
// Pay particular attention to the following:

// a) How the index variable updates during the call to find
// a) How the index variable updates during the call to find a char

// As the code runs, index starts from zero and runs until the length of the string,
// It starts from zero and in each loop it gets incremented until a char is found within the string.

// b) What is the if statement used to check

// It checks if the char is in the string. If it exists.. next step is to return the index of the char.


// c) Why is index++ being used?

// To check if the given char is present starting from 0 till str.length.


// d) What is the condition index < str.length used for?

// As long as index is less than the length of the string, keep looping until the char is found.

// Added explanation to the questions.
13 changes: 10 additions & 3 deletions Sprint-3/3-stretch/password-validator.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
function passwordValidator(password) {
return password.length < 5 ? false : true
function isValidPassword(password, previousPasswords = []) {
return (
password.length >= 5 &&
/[A-Z]/.test(password) &&
/[a-z]/.test(password) &&
/[0-9]/.test(password) &&
/[!#$%.*&]/.test(password) &&
!previousPasswords.includes(password)
);
}


module.exports = passwordValidator;
module.exports = isValidPassword;
49 changes: 41 additions & 8 deletions Sprint-3/3-stretch/password-validator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,45 @@ To be valid, a password must:
You must breakdown this problem in order to solve it. Find one test case first and get that working
*/
const isValidPassword = require("./password-validator");
const previousPasswords = ["Mmd1!", "XyZ2$", "Tes5%"];


test("password has at least 5 characters", () => {
// Arrange
const password = "12345";
// Act
const result = isValidPassword(password);
// Assert
expect(result).toEqual(true);
}
);
const password = "Ki55$";
const result = isValidPassword(password, previousPasswords); // pass the array
expect(result).toEqual(true);
});

test("password has at least one uppercase", () => {
const password = "Uo85*";
const result = isValidPassword(password, previousPasswords);
expect(result).toEqual(true);npx
});

test("password has at least one lowercase", () => {
const password = "Qf#45";
const result = isValidPassword(password, previousPasswords);
expect(result).toEqual(true);
});

test("password has at least one number", () => {
const password = "Cz!35";
const result = isValidPassword(password, previousPasswords);
expect(result).toEqual(true);
});

test("password has at least one special symbol", () => {
const password = "Re*19";
const result = isValidPassword(password, previousPasswords);
expect(result).toEqual(true);
});

test("password must not be a previous password", () => {
const password = "Mmd1!";
const result = isValidPassword(password, previousPasswords);
expect(result).toEqual(false);

});


// Password-validator implemented and tested.