Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Sprint-3/2-practice-tdd/count.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
function countChar(stringOfCharacters, findCharacter) {
return 5
let count = 0;
for (let i = 0; i < stringOfCharacters.length; i++)
if (stringOfCharacters[i] === findCharacter) {
count++;
}
return count;
}

module.exports = countChar;
33 changes: 33 additions & 0 deletions Sprint-3/2-practice-tdd/count.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,36 @@ test("should count multiple occurrences of a character", () => {
// 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 for no occurrences of the character", () => {
const str = "hello world";
const char = "z";
const count = countChar(str, char); // Calls countChar with the test inputs and saves the returned value
expect(count).toEqual(0); // checks that your function returns 0 for that test case.
});

// Scenario: Single Occurrence
// Given the input string str,
// And a character char that appears exactly once in the case-sensitive str,
// When the function is called with these inputs,
// Then it should return 1, indicating a single occurrence of the char was found in the str.

test("Should count the ocurrences of the character", () => {
const str = "hello world";
const char = "h";
const count = countChar(str, char);
expect(count).toEqual(1);
});

// Scenario: Case Sensitivity
// Given the input string str containing both uppercase and lowercase versions of a character,
// And a character char that matches only one case,
// When the function is called with these inputs,
// Then it should count only the exact case matches, not both.

test("Should count the uppercase ocurrences of the character", () => {
const str = "heLLo world";
const char = "L";
const count = countChar(str, char);
expect(count).toEqual(2);
});
24 changes: 21 additions & 3 deletions Sprint-3/2-practice-tdd/get-ordinal-number.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
function getOrdinalNumber(num) {
return "1st";
function getOrdinalNumber(inputNumber) {
inputNumber = Number(inputNumber); // Convert input to a number (handles string inputs edge case)
const absoluteNumber = Math.abs(inputNumber); // Use absolute value (handles negatives)
const lastTwo = absoluteNumber % 100; // Get the last two digits (for 'teens' exception)
let suffix = "th"; // Default suffix is 'th'

if (lastTwo < 11 || lastTwo > 13) {
// || is the "or" operator i learned by solving this function
// If not 11, 12, or 13, use last digit rule
const lastDigit = absoluteNumber % 10; // Get the last digit

if (lastDigit === 1) {
suffix = "st"; // 1st, 21st, etc.
} else if (lastDigit === 2) {
suffix = "nd"; // 2nd, 22nd, etc.
} else if (lastDigit === 3) {
suffix = "rd"; // 3rd, 23rd, etc.
}
}
return inputNumber + suffix; // Return the number with the correct suffix
}

module.exports = getOrdinalNumber;
module.exports = getOrdinalNumber; // Export the function for use in other files
56 changes: 56 additions & 0 deletions Sprint-3/2-practice-tdd/get-ordinal-number.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,59 @@ const getOrdinalNumber = require("./get-ordinal-number");
test("should return '1st' for 1", () => {
expect(getOrdinalNumber(1)).toEqual("1st");
});

// While i was doing my research I stumbled upon .toBe for exact equality.
// .toBe() checks for exact equality using === (strict equality).
// You can use it for primitive values: strings, numbers, booleans, null, or undefined.

// Basics
test("should return '2nd' for 2", () => {
expect(getOrdinalNumber(2)).toBe("2nd");
});

test("should return '3rd' for 3", () => {
expect(getOrdinalNumber(3)).toBe("3rd");
});

test("should return '4th' for 4", () => {
expect(getOrdinalNumber(4)).toBe("4th");
});

// Teens ordinal numbers
test("should return '11th' for 11", () => {
expect(getOrdinalNumber(11)).toBe("11th");
});

test("should return '12th' for 12", () => {
expect(getOrdinalNumber(12)).toBe("12th");
});

test("should return '13th' for 13", () => {
expect(getOrdinalNumber(13)).toBe("13th");
});

// Last-digit rule
test("should return '21st' for 21", () => {
expect(getOrdinalNumber(21)).toBe("21st");
});

test("should return '22nd' for 22", () => {
expect(getOrdinalNumber(22)).toBe("22nd");
});

test("should return '23rd' for 23", () => {
expect(getOrdinalNumber(23)).toBe("23rd");
});

test("should return '24th' for 24", () => {
expect(getOrdinalNumber(24)).toBe("24th");
});

// Edge cases
test("should return '0th' for 0", () => {
expect(getOrdinalNumber(0)).toBe("0th");
});

test("should return '-1st' for -1", () => {
expect(getOrdinalNumber(-1)).toBe("-1st");
});
18 changes: 16 additions & 2 deletions Sprint-3/2-practice-tdd/repeat.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
function repeat() {
return "hellohellohello";
function repeat(str, count) {
if (count < 0) {
throw new Error("Count must be non-negative number"); // 'throw' creates and sends an error, stopping the function immediately
}
if (count === 0) {
return "";
}
if (count === 1) {
return str;
}
let result = ""; // Start with an empty string to build the result
for (let i = 0; i < count; i++) {
// Loop 'count' times (i starts at 0, goes up to count +1)
result += str; // Used an addition assignment operator. instead of result = result + str
}
return result;
}

module.exports = repeat;
20 changes: 20 additions & 0 deletions Sprint-3/2-practice-tdd/repeat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,32 @@ test("should repeat the string count times", () => {
// 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.

test("should count a single occurrence of the character", () => {
const str = "hello";
const count = 1;
const repeatedStr = repeat(str, count);
expect(repeatedStr).toEqual("hello");
});

// 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 an empty string when count is 0", () => {
const str = "hello";
const count = 0;
const repeatedStr = repeat(str, count);
expect(repeatedStr).toEqual("");
});

// 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.

test("should trow an error when count is negative", () => {
const str = "hello";
const count = -2;
expect(() => repeat(str, count)).toThrow("Count must be non-negative number");
});