Skip to content
Open
Show file tree
Hide file tree
Changes from 9 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
9 changes: 8 additions & 1 deletion Sprint-3/2-practice-tdd/count.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
function countChar(stringOfCharacters, findCharacter) {
return 5
let count = 0;
for (let char of stringOfCharacters) {
if (char === findCharacter) {
count++;
}
}

return count;
}

module.exports = countChar;
13 changes: 13 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,16 @@ 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", () => {
expect(countChar("aaaaa", 'b")).toEqual(5);
expect(countChar("blind", 'a)).toEqual(0);
expect(countChar("blood", 'o")).toEqual(2);
expect(countChar("bbbrf", 'b")).toEqual(3);
expect(countChar("ooooa", 'o")).toEqual(4);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation is off.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed I had syntax errors with the quotes and when I changed it then saved the file Prettier changed the indentation.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: In VSCode, when a JS file ha syntax errors, its name is usually shown in red color in the Explorer view.

});


// 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)) || !Number.isInteger(num) || !Number.isFinite(num)) {
Comment thread
cjyuan marked this conversation as resolved.
Outdated
throw new Error("Input must be a valid number");
}

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

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

return num + "th";
}

module.exports = getOrdinalNumber;
31 changes: 31 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,39 @@ 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
// For numbers that don't end in 1 (not11), 2 and 3
// the function should return a string by appending "th" to the number.

test("should append 'th' if nunber is 11 or does not end in 1, 2 or 3 ", () => {
expect(getOrdinalNumber(20)).toEqual("20th");
expect(getOrdinalNumber(11)).toEqual("11th");
expect(getOrdinalNumber(99)).toEqual("99th");
});
Comment thread
cjyuan marked this conversation as resolved.
Outdated
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;
37 changes: 28 additions & 9 deletions Sprint-3/2-practice-tdd/repeat-str.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ const repeatStr = require("./repeat-str");
// Then it should:

// Case: handle multiple repetitions:
// Given a target string `str` and a positive integer `count` greater than 1,
// When the repeatStr function is called with these inputs,
// Then it should return a string that contains the original `str` repeated `count` times.
// A string is repeated count number of times
// If a string is "hello" and count is 3 then
// the hello will be output 3 times with no spaces between

test("should repeat the string count times", () => {
const str = "hello";
Expand All @@ -17,16 +17,35 @@ test("should repeat the string count times", () => {
});

// Case: handle count of 1:
// Given a target string `str` and a `count` equal to 1,
// When the repeatStr function is called with these inputs,
// Then it should return the original `str` without repetition.
// If count is 1 then the string is not repeated and
// will be output for example if the string is "Hello"
// then the output will be "Hello"

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,
// Then it should return an empty string.
// If the the string is empty then count will be 0
// and an empty string will be returned

test("should repeat the string count times", () => {
Comment thread
cjyuan marked this conversation as resolved.
Outdated
const str = "hello";
const count = 0;
const repeatedStr = repeatStr(str, count);
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