Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
23 changes: 19 additions & 4 deletions Sprint-3/2-practice-tdd/count.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
function countChar(stringOfCharacters, findCharacter) {
return 5
}
// function countChar(stringOfCharacters, findCharacter) {
// let count = 0;
// for (let i = 0; i < stringOfCharacters.length; i++) {
// if (stringOfCharacters[i] === findCharacter) {
// count++;
// }
// }
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The ITP assignment does need countChar method to be implemented so that the tests, which call countChar will pass

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.

Done ✅


// return count;
// }
// module.exports = countChar;
let numbers=[1,2,3,4,5,6,1,2,3,4,5,6]
let idxArray=[];
numbers.find((el,ix,numbers)=>{
if (el===3){

idxArray.push(ix);}
})
console.log(idxArray);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

this piece of code does not seem to be related to the assignment.
please let me know if i can help you understand the problem better
or perhaps this was an accident?

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.

You're absolutely right the extra code with the numbers array was unrelated to the assignment and was left in accidentally during my testing. I've now uncommented and finalized the correct countChar implementation, removed all unrelated code snippets and verified that the function passes the expected test cases.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ah okay, yes that makes sense. Just remember to always do a final check before pushing up

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.

Thank you so much for your helpful feedback I really appreciate the time and effort you took to review my submission. 🙏
I've noted your reminder about doing a final check before pushing, and I'll make sure to keep that in mind for future submissions.
Just to confirm: is there anything else you'd like me to adjust or improve in the code? If everything looks good on your end, would you mind updating the status label to Complete when you get a chance?
Thanks again for your support!

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

hi @FAprogrammerO thanks, only a pleasure!
no sorry i meant to change it to complete.
done that now


module.exports = countChar;
34 changes: 29 additions & 5 deletions Sprint-3/2-practice-tdd/count.test.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,48 @@
// ═══════════════════════════════════════════════════════
// 🎯 Personal Implementation: countChar Function Tests

// Purpose: Test-first practice for counting character occurrences
// Style: Gherkin-style BDD comments + Jest assertions
// ═══════════════════════════════════════════════════════

// implement a function countChar that counts the number of times a character occurs in a string
const countChar = require("./count");

// ───────────────────────────────────────────────────────
// 📋 Specification:
// Given a string `str` and a single character `char` to search for,
// When the countChar function is called with these inputs,
// Then it should:
// Then it should return the number of times `char` appears in `str`.
// ───────────────────────────────────────────────────────

// ═══════════════════════════════════════════════════════
// 🧪 Test Scenarios
// ═══════════════════════════════════════════════════════

// Scenario: Multiple Occurrences
// ──────────────────────────────
// Given the input string `str`,
// And a character `char` that occurs one or more times in `str` (e.g., 'a' in 'aaaaa'),
// When the function is called with these inputs,
// Then it should correctly count occurrences of `char`.

test("should count multiple occurrences of a character", () => {
const str = "aaaaa";
const char = "a";
const str = "aaaaa"; // 🎯 Input: repeated character
const char = "a"; // 🔍 Search for: 'a'
const count = countChar(str, char);
expect(count).toEqual(5);
expect(count).toEqual(5); // ✅ Expect: 5 occurrences
});

// Scenario: No Occurrences
// ─────────────────────────
// Given the input string `str`,
// 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 return 0 for no occurence", () => {
const str = "asdf"; // 🎯 Input: no 'l' present
const char = "l"; // 🔍 Search for: 'l' (not in string)
const count = countChar(str, char);
expect(count).toEqual(0); // ✅ Expect: 0 occurrences
});


15 changes: 13 additions & 2 deletions Sprint-3/2-practice-tdd/get-ordinal-number.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
function getOrdinalNumber(num) {
return "1st";
let numberToString = String(num);
let numberLastDigit = numberToString.slice(-1);
let numberLast2Digits = numberToString.slice(-2);

if (numberLastDigit === "1" && numberLast2Digits !== "11")
return numberToString + "st";
if (numberLastDigit === "2" && numberLast2Digits !== "12")
return numberToString + "nd";
if (numberLastDigit === "3" && numberLast2Digits !== "13")
return numberToString + "rd";
return numberToString + "th";
}

module.exports = getOrdinalNumber;

module.exports = getOrdinalNumber;
63 changes: 45 additions & 18 deletions Sprint-3/2-practice-tdd/get-ordinal-number.test.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,47 @@

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.

// To ensure thorough testing, we need broad scenarios that cover all possible cases.
// Listing individual values, however, can quickly lead to an unmanageable number of test cases.
// Instead of writing tests for individual numbers, consider grouping all possible input values
// into meaningful categories. Then, select representative samples from each category to test.
// This approach improves coverage and makes our tests easier to maintain.

// 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");

// ───────────────────────────────────────────────────────
// 📋 Rule Summary:
// • 1 → "1st", 2 → "2nd", 3 → "3rd" (unless teen)
// • 11, 12, 13 → always "th" (teen exception)
// • All others → last digit determines suffix
// ───────────────────────────────────────────────────────

// ── Case 1: Numbers ending in 1 (excluding teens) → "st" ──
test("appends 'st' to numbers ending in 1, except 11", () => {
expect(getOrdinalNumber(1)).toEqual("1st"); // single digit
expect(getOrdinalNumber(21)).toEqual("21st"); // double digit
expect(getOrdinalNumber(131)).toEqual("131st");// triple digit
});

// ── Case 2: Numbers ending in 2 (excluding teens) → "nd" ──
test("appends 'nd' to numbers ending in 2, except 12", () => {
expect(getOrdinalNumber(2)).toEqual("2nd");
expect(getOrdinalNumber(22)).toEqual("22nd");
expect(getOrdinalNumber(122)).toEqual("122nd");
});

// ── Case 3: Numbers ending in 3 (excluding teens) → "rd" ──
test("appends 'rd' to numbers ending in 3, except 13", () => {
expect(getOrdinalNumber(3)).toEqual("3rd");
expect(getOrdinalNumber(23)).toEqual("23rd");
expect(getOrdinalNumber(123)).toEqual("123rd");
});

// ── Case 4: Default "th" suffix (teens + digits 0,4-9) ──
test("appends 'th' for teens (11-13) and digits 0,4-9", () => {
// Teen exceptions (critical edge case)
expect(getOrdinalNumber(11)).toEqual("11th");
expect(getOrdinalNumber(12)).toEqual("12th");
expect(getOrdinalNumber(13)).toEqual("13th");
expect(getOrdinalNumber(111)).toEqual("111th"); // larger teen

// Default suffix digits
expect(getOrdinalNumber(4)).toEqual("4th");
expect(getOrdinalNumber(10)).toEqual("10th");
expect(getOrdinalNumber(25)).toEqual("25th");
});



7 changes: 4 additions & 3 deletions Sprint-3/2-practice-tdd/repeat-str.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
function repeatStr() {
return "hellohellohello";
function repeatStr(str, count) {
if (count < 0) throw new Error("negative counts are not valid");
return str.repeat(count);
}

module.exports = repeatStr;
module.exports = repeatStr;
55 changes: 30 additions & 25 deletions Sprint-3/2-practice-tdd/repeat-str.test.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,37 @@
// Implement a function repeatStr
const repeatStr = require("./repeat-str");
// Given a target string `str` and a positive integer `count`,
// When the repeatStr function is called with these inputs,
// 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.
// ───────────────────────────────────────────────────────
// Rule Summary:
// • count > 1 → str repeated count times ("hi", 3 → "hihihi")
// • count = 1 → original string unchanged
// • count = 0 → empty string ""
// • count < 0 → throw error (invalid input)
// ───────────────────────────────────────────────────────

test("should repeat the string count times", () => {
const str = "hello";
const count = 3;
const repeatedStr = repeatStr(str, count);
expect(repeatedStr).toEqual("hellohellohello");
// ── Case 1: Multiple repetitions (count > 1) ──
test("repeats string correctly for count > 1", () => {
expect(repeatStr("hello", 3)).toEqual("hellohellohello");
expect(repeatStr("a", 5)).toEqual("aaaaa");
expect(repeatStr("xy", 2)).toEqual("xyxy");
});

// 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.
// ── Case 2: Single repetition (count = 1) ──
test("returns original string unchanged when count = 1", () => {
expect(repeatStr("bye", 1)).toEqual("bye");
expect(repeatStr("x", 1)).toEqual("x");
expect(repeatStr("test", 1)).toEqual("test");
});

// ── Case 3: Zero repetition (count = 0) ──
test("returns empty string when count = 0", () => {
expect(repeatStr("no", 0)).toEqual("");
expect(repeatStr("anything", 0)).toEqual("");
expect(repeatStr("", 0)).toEqual("");
});

// 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.
// ── Case 4: Invalid input (negative count) ──
test("throws error for negative count values", () => {
expect(() => repeatStr("str", -1)).toThrow("negative counts are not valid");
expect(() => repeatStr("x", -10)).toThrow("negative counts are not valid");
});

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