Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 7 additions & 9 deletions Sprint-3/2-practice-tdd/get-ordinal-number.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
function getOrdinalNumber(num) {
const itsTwoLastDigs = num % 100;
if (itsTwoLastDigs === 11 || itsTwoLastDigs === 12 || itsTwoLastDigs === 13) {
const numberLastTwoDigits = num % 100;

if (numberLastTwoDigits === 11 || numberLastTwoDigits === 12 || numberLastTwoDigits === 13) {
return num + "th";
}

const lastDig = num % 10;
if (lastDig === 1) return num + "st";
if (lastDig === 2) return num + "nd";
if (lastDig === 3) return num + "rd";
const numberLastDigit = num % 10;
if (numberLastDigit === 1) return num + "st";
if (numberLastDigit === 2) return num + "nd";
if (numberLastDigit === 3) return num + "rd";
return num + "th";
}




module.exports = getOrdinalNumber;
58 changes: 29 additions & 29 deletions Sprint-3/2-practice-tdd/get-ordinal-number.test.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
const getOrdinalNumber = require("./get-ordinal-number");
// In this week's prep, we started implementing getOrdinalNumber

// continue testing and implementing getOrdinalNumber for additional cases
// Write your tests using Jest - remember to run your tests often for continual feedback

// Case 1: Identify the ordinal number for 1
// When the number is 1,
// Then the function should return "1st"

describe("getOrdinalNumber", () => {
test("append 'st' to numbers ending in 1 expect 11", () => {
expect(getOrdinalNumber(1)).toEqual("1st");
expect(getOrdinalNumber(101)).toEqual("101st");
});
test("should return '11th' for 11", () => {
expect(getOrdinalNumber(11)).toEqual("11th");
test("returns 'st', 'nd', 'rd', 'th' for standard numbers", () => {
expect(getOrdinalNumber(1)).toBe("1st");
expect(getOrdinalNumber(2)).toBe("2nd");
expect(getOrdinalNumber(3)).toBe("3rd");
expect(getOrdinalNumber(4)).toBe("4th");
expect(getOrdinalNumber(10)).toBe("10th");
});

test("append 'rd' to numbers ending in 3", () => {
expect(getOrdinalNumber(3)).toEqual("3rd");
expect(getOrdinalNumber(23)).toEqual("23rd");
expect(getOrdinalNumber(33)).toEqual("33rd");
expect(getOrdinalNumber(103)).toEqual("103rd");
test("handles numbers ending with 11, 12, 13 as special cases", () => {
expect(getOrdinalNumber(11)).toBe("11th");
expect(getOrdinalNumber(12)).toBe("12th");
expect(getOrdinalNumber(13)).toBe("13th");
expect(getOrdinalNumber(111)).toBe("111th");
expect(getOrdinalNumber(112)).toBe("112th");
expect(getOrdinalNumber(113)).toBe("113th");
});
test("should return '4th' for 4", () => {
expect(getOrdinalNumber(4)).toEqual("4th");

test("handles numbers ending with 1, 2, or 3 correctly beyond 100", () => {
expect(getOrdinalNumber(101)).toBe("101st");
expect(getOrdinalNumber(102)).toBe("102nd");
expect(getOrdinalNumber(103)).toBe("103rd");
expect(getOrdinalNumber(104)).toBe("104th");
expect(getOrdinalNumber(132)).toBe("132nd");
});

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

test("works with large numbers", () => {
expect(getOrdinalNumber(1001)).toBe("1001st");
expect(getOrdinalNumber(1012)).toBe("1012th");
});

test("append 'nd' to numbers ending in 2, except those ending in 12", () => {
expect(getOrdinalNumber(2)).toEqual("2nd");
expect(getOrdinalNumber(22)).toEqual("22nd");
expect(getOrdinalNumber(132)).toEqual("132nd");
test("handles negative numbers", () => {
expect(getOrdinalNumber(-1)).toBe("-1st");
expect(getOrdinalNumber(-11)).toBe("-11th");
});
Copy link
Contributor

Choose a reason for hiding this comment

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

Did you prepare this test because the function is supposed to behave like that, or because your function is behaving like that?

});
5 changes: 2 additions & 3 deletions Sprint-3/2-practice-tdd/repeat-str.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@ test("should repeat the string count times", () => {
// 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 or return an appropriate error message, as negative counts are not valid.
test("should repeat the string count times", () => {
test("should throw an error when count is negative", () => {
const str = "hello";
const count = -1;
const repeatedStr = repeat(str, count);
expect(repeatedStr).toEqual("invalid");
expect(() => repeatStr(str, count)).toThrow("count must be non-negative");
});