Skip to content
2 changes: 1 addition & 1 deletion Sprint-3/2-practice-tdd/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ Write the tests _before_ the code that will make them pass.
Recommended order:

1. `count.test.js`
1. `repeat.test.js`
1. `repeat-str.test.js`
1. `get-ordinal-number.test.js`
18 changes: 18 additions & 0 deletions Sprint-3/2-practice-tdd/count.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,21 @@ function countChar(stringOfCharacters, findCharacter) {
}

module.exports = countChar;

const countChar = require('../countChar');

describe('countChar()', () => {
test('always returns 5', () => {
expect(countChar('anything', 'a')).toBe(5);
});

test('still returns 5 with empty string', () => {
expect(countChar('', 'z')).toBe(5);
});

test('still returns 5 with special characters', () => {
expect(countChar('!!!', '!')).toBe(5);
});
});
//this is the end.
// Last line
41 changes: 41 additions & 0 deletions Sprint-3/2-practice-tdd/count.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
// implement a function countChar that counts the number of times a character occurs in a string
const countChar = require("./count");

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

module.exports = countChar;

// Given a string str and a single character char to search for,
// When the countChar function is called with these inputs,
// Then it should:
Expand All @@ -16,6 +27,36 @@ test("should count multiple occurrences of a character", () => {
const count = countChar(str, char);
expect(count).toEqual(5);
});
test("should return 0 when the character is not found", () => {
const str = "hello";
const char = "z";
const count = countChar(str, char);
expect(count).toEqual(0);
});

// Scenario: Case Sensitivity
// It should be case-sensitive, meaning 'A' != 'a'.
test("should be case-sensitive when counting characters", () => {
const str = "Banana";
expect(countChar(str, "a")).toEqual(3);
expect(countChar(str, "A")).toEqual(0);
});

// Scenario: Empty String
// It should return 0 when the input string is empty.
test("should return 0 when the input string is empty", () => {
const str = "";
const char = "a";
const count = countChar(str, char);
expect(count).toEqual(0);
});

// Scenario: Special Characters
// It should correctly count spaces and punctuation.
test("should count special characters like spaces or punctuation", () => {
expect(countChar("a b a b", " ")).toEqual(2);
expect(countChar("wow!!!", "!")).toEqual(3);
});

// Scenario: No Occurrences
// Given the input string str,
Expand Down
58 changes: 57 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,61 @@

function getOrdinalNumber(num) {
return "1st";
const remainder10 = num % 10;
const remainder100 = num % 100;

if (remainder100 >= 11 && remainder100 <= 13) {
return `${num}th`;
}

switch (remainder10) {
case 1:
return `${num}st`;
case 2:
return `${num}nd`;
case 3:
return `${num}rd`;
default:
return `${num}th`;
}
}

module.exports = getOrdinalNumber;


const getOrdinalNumber = require("./getOrdinalNumber");

describe("getOrdinalNumber()", () => {
test("should return '1st' for 1", () => {
expect(getOrdinalNumber(1)).toBe("1st");
});

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

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

test("should return correct suffixes for 21, 22, 23", () => {
expect(getOrdinalNumber(21)).toBe("21st");
expect(getOrdinalNumber(22)).toBe("22nd");
expect(getOrdinalNumber(23)).toBe("23rd");
});

test("should return '111th' for numbers ending with 11, 12, 13 even if larger", () => {
expect(getOrdinalNumber(111)).toBe("111th");
expect(getOrdinalNumber(112)).toBe("112th");
expect(getOrdinalNumber(113)).toBe("113th");
});
});
7 changes: 7 additions & 0 deletions Sprint-3/2-practice-tdd/get-ordinal-number.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
const getOrdinalNumber = require("./get-ordinal-number");

function getOrdinalNumber(num) {
return "1st";
}

module.exports = getOrdinalNumber;

// In this week's prep, we started implementing getOrdinalNumber

// continue testing and implementing getOrdinalNumber for additional cases
Expand Down
5 changes: 5 additions & 0 deletions Sprint-3/2-practice-tdd/repeat-str.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function repeatStr() {
return "hellohellohello";
}

module.exports = repeatStr;
72 changes: 72 additions & 0 deletions Sprint-3/2-practice-tdd/repeat-str.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// 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:
function repeat(str, count) {
if (count < 0) {
throw new Error("Count must be a non-negative integer");
}
return str.repeat(count);
}

module.exports = repeat;

// case: repeat String:
// Given a target string str and a positive integer count,
// When the repeatStr function is called with these inputs,
// Then it should repeat the str count times and return a new string containing the repeated str values.

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

// 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, ensuring that a count of 1 results in no repetition.

// 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, ensuring that a count of 0 results in an empty output.

// case: 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 or return an appropriate error message, as negative counts are not valid.
const repeat = require("./repeat");

// Case 1: Repeat string multiple times
test("should repeat the string count times", () => {
const str = "hello";
const count = 3;
const repeatedStr = repeat(str, count);
expect(repeatedStr).toEqual("hellohellohello");
});

// Case 2: Handle count of 1
test("should return the original string when count is 1", () => {
const str = "world";
const count = 1;
const repeatedStr = repeat(str, count);
expect(repeatedStr).toEqual("world");
});

// Case 3: Handle count of 0
test("should return an empty string when count is 0", () => {
const str = "test";
const count = 0;
const repeatedStr = repeat(str, count);
expect(repeatedStr).toEqual("");
});

// Case 4: Negative count
test("should throw an error when count is negative", () => {
const str = "oops";
const count = -2;
expect(() => repeat(str, count)).toThrow("Count must be a non-negative integer");
});
6 changes: 6 additions & 0 deletions Sprint-3/2-practice-tdd/repeat.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
const repeat = require("./repeat");

function repeat() {
return "hellohellohello";
}

module.exports = repeat;

test("should repeat the given word three times", () => {
expect(repeat("hi", 3)).toBe("hihihi");
});
32 changes: 0 additions & 32 deletions Sprint-3/2-practice-tdd/repeat.test.js

This file was deleted.

61 changes: 61 additions & 0 deletions Sprint-3/3-stretch/card-validator.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,64 @@ These are the requirements your project needs to fulfill:
- Return a boolean from the function to indicate whether the credit card number is valid.

Good luck!

function validateCreditCard(number) {
// Convert input to a string so we can check characters easily
const numStr = String(number);

// Rule 1: Must be 16 digits, all numbers
if (!/^\d{16}$/.test(numStr)) {
return false;
}

// Rule 2: Must contain at least two different digits
const uniqueDigits = new Set(numStr);
if (uniqueDigits.size < 2) {
return false;
}

// Rule 3: The last digit must be even
const lastDigit = parseInt(numStr[numStr.length - 1]);
if (lastDigit % 2 !== 0) {
return false;
}

// Rule 4: The sum of all digits must be greater than 16
const sum = numStr
.split("")
.map(Number)
.reduce((a, b) => a + b, 0);

if (sum <= 16) {
return false;
}

// If all checks pass, return true ✅
return true;
}

module.exports = validateCreditCard;

const validateCreditCard = require("./validateCreditCard");

test("valid credit card numbers should return true", () => {
expect(validateCreditCard("9999777788880000")).toBe(true);
expect(validateCreditCard("6666666666661666")).toBe(true);
});

test("invalid cards with letters should return false", () => {
expect(validateCreditCard("a92332119c011112")).toBe(false);
});

test("invalid cards with all same digits should return false", () => {
expect(validateCreditCard("4444444444444444")).toBe(false);
});

test("invalid cards with sum less than 16 should return false", () => {
expect(validateCreditCard("1111111111111110")).toBe(false);
});

test("invalid cards with odd final digit should return false", () => {
expect(validateCreditCard("6666666666666661")).toBe(false);
});

38 changes: 37 additions & 1 deletion Sprint-3/3-stretch/password-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,40 @@ function passwordValidator(password) {
}


module.exports = passwordValidator;
module.exports = passwordValidator;
function validateCreditCard(number) {
// Convert input to a string so we can check characters easily
const numStr = String(number);

// Rule 1: Must be 16 digits, all numbers
if (!/^\d{16}$/.test(numStr)) {
return false;
}

// Rule 2: Must contain at least two different digits
const uniqueDigits = new Set(numStr);
if (uniqueDigits.size < 2) {
return false;
}

// Rule 3: The last digit must be even
const lastDigit = parseInt(numStr[numStr.length - 1]);
if (lastDigit % 2 !== 0) {
return false;
}

// Rule 4: The sum of all digits must be greater than 16
const sum = numStr
.split("")
.map(Number)
.reduce((a, b) => a + b, 0);

if (sum <= 16) {
return false;
}

// If all checks pass, return true ✅
return true;
}

module.exports = validateCreditCard;