Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
node_modules
.DS_Store
.vscode
**/.DS_Store
.vscode/
**/.DS_Store
8 changes: 7 additions & 1 deletion Sprint-3/2-practice-tdd/count.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
function countChar(stringOfCharacters, findCharacter) {
return 5

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

Comment thread
cjyuan marked this conversation as resolved.
Outdated
module.exports = countChar;
53 changes: 53 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,56 @@ test("should count multiple occurrences of a character", () => {
// And a character `char` that does not exist within `str`.
// When the function is called with these inputs,
// Then it should return 0, indicating that no occurrences of `char` were found.

test("should count multiple occurrences of a character", () => {
const str = "aaaaa";
const char = "a";
const count = countChar(str, char);
expect(count).toEqual(5);
});

test("should count multiple occurrences of a character", () => {
const str = "blind";
const char = "a";
const count = countChar(str, char);
expect(count).toEqual(0);
});

test("should count multiple occurrences of a character", () => {
const str = "blood";
const char = "o";
const count = countChar(str, char);
expect(count).toEqual(2);
});

test("should count multiple occurrences of a character", () => {
const str = "blood";
const char = "o";
const count = countChar(str, char);
expect(count).toEqual(2);
});

test("should count multiple occurrences of a character", () => {
const str = "blood";
const char = "l";
const count = countChar(str, char);
expect(count).toEqual(1);
});

test("should count multiple occurrences of a character", () => {
const str = "bbbrf";
const char = "b";
const count = countChar(str, char);
expect(count).toEqual(3);
});

test("should count multiple occurrences of a character", () => {
const str = "ooooa";
const char = "o";
const count = countChar(str, char);
expect(count).toEqual(4);
});
Comment thread
cjyuan marked this conversation as resolved.

// handling invalid input
// the tests work assuming that only letters are in the string
// numbers and special characters are not tested for
17 changes: 16 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,20 @@
function getOrdinalNumber(num) {
return "1st";
// Check if input is a number
if (typeof num !== "number" || isNaN(num)) {
Comment thread
cjyuan marked this conversation as resolved.
Outdated
throw new Error("Input must be a valid number");
}

const j = num % 10; // checks what last number is
const k = num % 100; // checks what last two numbers are, needed to check for 11
Comment thread
cjyuan marked this conversation as resolved.
Outdated

if (k === 11 || k === 12 || k === 13) {
return num + "th";
}
if (j === 1) return num + "st";
if (j === 2) return num + "nd";
if (j === 3) return num + "rd";

return num + "th";
}

module.exports = getOrdinalNumber;
32 changes: 32 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 @@ -13,8 +13,40 @@ const getOrdinalNumber = require("./get-ordinal-number");
// Case 1: Numbers ending with 1 (but not 11)
// When the number ends with 1, except those ending with 11,
// Then the function should return a string by appending "st" to the number.

test("should append 'st' for numbers ending with 1, except those ending with 11", () => {
expect(getOrdinalNumber(1)).toEqual("1st");
expect(getOrdinalNumber(21)).toEqual("21st");
expect(getOrdinalNumber(131)).toEqual("131st");
});

// Case 2: Numbers ending with 2
// When the number ends with 2,
// Then the function should return a string by appending "nd" to the number.

test("should append 'nd' for numbers ending with 2", () => {
expect(getOrdinalNumber(2)).toEqual("2nd");
expect(getOrdinalNumber(32)).toEqual("32nd");
expect(getOrdinalNumber(252)).toEqual("252nd");
});

// Case 3: Numbers ending with 3
// When the number ends with 3,
// Then the function should return a string by appending "rd" to the number.

test("should append 'rd' for numbers ending with 3", () => {
Comment thread
cjyuan marked this conversation as resolved.
Outdated
expect(getOrdinalNumber(3)).toEqual("3rd");
expect(getOrdinalNumber(33)).toEqual("33rd");
expect(getOrdinalNumber(133)).toEqual("133rd");
});

// Case 4: The remaining numbers
// When the number ends with 1, except those ending with 11
// For all other numbers
// Then the function should return a string by appending "th" to the number.

test("should append 'th' for remaining numbers", () => {
expect(getOrdinalNumber(20)).toEqual("20th");
Comment thread
cjyuan marked this conversation as resolved.
Outdated
expect(getOrdinalNumber(11)).toEqual("11th");
expect(getOrdinalNumber(99)).toEqual("99th");
});
12 changes: 10 additions & 2 deletions Sprint-3/2-practice-tdd/repeat-str.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
function repeatStr() {
return "hellohellohello";
function repeatStr(str, count) {
if (count < 0) {
throw new Error("Count must be positive");
}

if (count === 0) {
return " ";
}

return str.repeat(count);
}

module.exports = repeatStr;
22 changes: 21 additions & 1 deletion Sprint-3/2-practice-tdd/repeat-str.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 repeatStr function is called with these inputs,
// Then it should return the original `str` without repetition.

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

// Case: Handle count of 0:
// Given a target string `str` and a `count` equal to 0,
// When the repeatStr function is called with these inputs,
// When the repeatStr function is called with these inputs
// Then it should return an empty string.

test("should repeat the string count times", () => {
const str = "hello";
const count = 0;
const repeatedStr = repeatStr(str, count);
Comment thread
cjyuan marked this conversation as resolved.
Outdated
expect(repeatedStr).toEqual(" ");
});

// Case: Handle negative count:
// Given a target string `str` and a negative integer `count`,
// When the repeatStr function is called with these inputs,
// Then it should throw an error, as negative counts are not valid.

test("should throw an error when count is negative", () => {
const str = "hello";
const count = -3;
expect(() => repeatStr(str, count)).toThrow("Count must be positive");
});
Loading