Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// execute the code to ensure all tests pass.

function isProperFraction(numerator, denominator) {
// TODO: Implement this function
return Math.abs(numerator) < Math.abs(denominator)
}

// The line below allows us to load the isProperFraction function into tests in other files.
Expand All @@ -31,3 +31,27 @@ function assertEquals(actualOutput, targetOutput) {

// Example: 1/2 is a proper fraction
assertEquals(isProperFraction(1, 2), true);

//Case 2: 5/5 is not a proper fraction
assertEquals(isProperFraction(5,5), false);

//Case 3: (-3)/(-5) is a proper fraction
assertEquals(isFinite(-3,-5), true);

//Case 4: (-4)/6 is a proper fraction
assertEquals(isProperFraction(-4,6),true);

//case 5: 5/(-8) is a proper fraction
assertEquals(isProperFraction(5,-8), true);

//case 6: 9/7 is not a proper fraction
assertEquals(isProperFraction(9,7), false);

//case 7: 7/0 is not a proper fraction
assertEquals(isProperFraction(7,0), false);

//case 8: 0/6 is a proper fraction
assertEquals(isProperFraction(0,6), true);

//case 9: 0/-8 is a proper fraction
assertEquals(isProperFraction(0,-8), true);
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,23 @@
// execute the code to ensure all tests pass.

function getCardValue(card) {
// TODO: Implement this function
}
const validSuits = ["♠", "♥", "♦", "♣"];
const validRanks = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"];

const suit = card.slice(-1);
const rank = card.slice(0, -1);


if (!validSuits.includes(suit) || !validRanks.includes(rank)) {
throw new Error("Invalid card");
}

if (rank === "A") return 11;
if (["J", "Q", "K"].includes(rank)) return 10;

return Number(rank);
}


// The line below allows us to load the getCardValue function into tests in other files.
// This will be useful in the "rewrite tests with jest" step.
Expand All @@ -38,15 +53,47 @@ function assertEquals(actualOutput, targetOutput) {
}

// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
// Examples:
// Examples 1 numbers:
assertEquals(getCardValue("9♠"), 9);
assertEquals(getCardValue("5♣"), 5);

//Examples A:
assertEquals(getCardValue("A♦"), 11)

//Examples J Q K:,
assertEquals(getCardValue("Q♥"), 10)
assertEquals(getCardValue("K♥"), 10)

// Handling invalid cards
try {
getCardValue("invalid");
//Examples invalid numbers:
assertThrow(()=>getCardValue("1♥"))
assertThrow(()=>getCardValue("11♥"))

//Examples invalid suit:
assertThrow(()=>getCardValue("A*"))

//Examples missing rank or suit:
assertThrow(()=>getCardValue("♥"))
assertThrow(()=>getCardValue("5"))

//Example extra suit:
assertThrow(()=>getCardValue("Q♠♠"))


function assertThrow(fn){
try { (fn)
// we try to run this function, if it throws, stop running this bit and run the catch below

// This line will not be reached if an error is thrown as expected
console.error("Error was not thrown for invalid card");
} catch (e) {}
} catch (error) {
// if the above code throws, we catch the error here, that stops the whole program crashing
console.log('there was an error getting card value!',error)
}}

console.log(' we finished running getCardValue')

// What other invalid card cases can you think of?

//Examples wrong type:
assertThrow(()=>getCardValue("null"))
assertThrow(()=>getCardValue("42"))
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,24 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => {
});

// Case 2: Right angle
test ('should return "Right angle" when angle ===90',() => {
expect(getAngleType(90)).toEqual("Right angle");
})
// Case 3: Obtuse angles
test ('should return "Obtuse angle" when (90<angle && angle <180)',() => {
expect(getAngleType(135)).toEqual("Obtuse angle");
})
// Case 4: Straight angle
test ('should return "Straight angle" when angle ===180',() => {
expect(getAngleType(180)).toEqual("Straight angle");
})

// Case 5: Reflex angles
test ('should return "Obtuse angle" when (180<angle && angle <360)',() => {
expect(getAngleType(275)).toEqual("Reflex angle");
})

// Case 6: Invalid angles
test('should return "Invalid angle"when (angle>=360)', ()=> {
expect(getAngleType(360)).toEqual("Invalid angle");
})
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,30 @@ const isProperFraction = require("../implement/2-is-proper-fraction");

// TODO: Write tests in Jest syntax to cover all combinations of positives, negatives, zeros, and other categories.

// Special case: numerator is zero
// Special case: denominator is zero
test(`should return false when denominator is zero`, () => {
expect(isProperFraction(1, 0)).toEqual(false);
});

//case:absolute numerator is bigger than absolute denominator
test ('should return false when numerator absolute is bigger than denominator absolute',() => {
expect(isProperFraction(5, -4)).toEqual(false);
})

//case:numerator is the same as denominator
test('should return false if numerator is the same as denominator', () =>{
expect(isProperFraction(-9,-9)).toEqual(false)
})

//case: absolute numerator is less than absolute denominator
test ('should return true when numerator absolute is less than denominator absolute',() => {
expect(isProperFraction(3,-8)).toEqual(true);
})

//case: numerator is zero
test(`should return true when nominator is zero`, () => {
expect(isProperFraction(0,-3)).toEqual(true);
});



Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,21 @@ test(`Should return 11 when given an ace card`, () => {
// Face Cards (J, Q, K)
// Invalid Cards

// Case 2 : Number(2-10)
test('should return the exact number when given an number card', () =>{
expect(getCardValue("5♠")).toEqual(5);
})

//case 3 : face card(J,Q,K)
test('should return number when given face card', () =>{
expect(getCardValue("K♠")).toEqual(10);
})

// case 4: Invalid cards
test('throws new Error', () => {
expect(() => getCardValue("9**")).toThrow("Invalid card");
});

// To learn how to test whether a function throws an error as expected in Jest,
// please refer to the Jest documentation:
// https://jestjs.io/docs/expect#tothrowerror
Expand Down
10 changes: 9 additions & 1 deletion Sprint-3/2-practice-tdd/count.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
function countChar(stringOfCharacters, findCharacter) {
return 5
let count = 0
for(let i=0;i<stringOfCharacters.length;i++ ){
if (stringOfCharacters[i]===findCharacter){
count++
}
}
return count
}

console.log(countChar("where","e"))

module.exports = countChar;
9 changes: 8 additions & 1 deletion Sprint-3/2-practice-tdd/count.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
const countChar = require("./count");
// 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:
test("should count occurrences of a character", () => {
expect(countChar("tired", "d")).toEqual(1)
})


// Scenario: Multiple Occurrences
// Given the input string `str`,
Expand All @@ -22,3 +26,6 @@ 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 no occurrences of a character", () => {
expect(countChar("bananataste","w")).toEqual(0)
})
22 changes: 21 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,25 @@
function getOrdinalNumber(num) {
return "1st";
lastDigit = num % 10;
lastTwoDigits = num % 100;

if (lastTwoDigits >= 11 && lastTwoDigits <= 13) {
return num + "th";
}

switch (lastDigit) {
case 1:
return num + "st";
case 2:
return num + "nd";
case 3:
return num + "rd";
default:
return num + "th";
}
}

console.log(getOrdinalNumber(112))



module.exports = getOrdinalNumber;
28 changes: 28 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 @@ -18,3 +18,31 @@ test("should append 'st' for numbers ending with 1, except those ending with 11"
expect(getOrdinalNumber(21)).toEqual("21st");
expect(getOrdinalNumber(131)).toEqual("131st");
});

// case 2: last two digits ending with 11,12,13
test("should append 'th' for numbers with last digits 11,12,13", () =>{
expect(getOrdinalNumber(11)).toEqual("11th");
expect(getOrdinalNumber(313)).toEqual("313th")
expect(getOrdinalNumber(2112)).toEqual("2112th")
})

// Case 3: Numbers ending with 2(but not 12)
test("should append 'nd' for numbers ending with 2, except those ending with 12", () => {
expect(getOrdinalNumber(2)).toEqual("2nd");
expect(getOrdinalNumber(52)).toEqual("52nd");
expect(getOrdinalNumber(232)).toEqual("232nd");
});

// Case 4: Numbers ending with 3(but not 13)
test("should append 'rd' for numbers ending with 3, except those ending with 13", () => {
expect(getOrdinalNumber(3)).toEqual("3rd");
expect(getOrdinalNumber(83)).toEqual("83rd");
expect(getOrdinalNumber(463)).toEqual("463rd");
});

// case 5: Numbers ending with 4-9 and 0
test("should append 'th' for numbers with last 4-9 and 0", () =>{
expect(getOrdinalNumber(8)).toEqual("8th");
expect(getOrdinalNumber(3130)).toEqual("3130th")
expect(getOrdinalNumber(2119)).toEqual("2119th")
})
10 changes: 8 additions & 2 deletions Sprint-3/2-practice-tdd/repeat-str.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
function repeatStr() {
return "hellohellohello";
function repeatStr(str,count) {
if (count>0) return str.repeat(count)
if (count===0) return ""
if (count<0) return"invalid count"
}

console.log(repeatStr("hello",5))
console.log(repeatStr("nihao",1))
console.log(repeatStr("nihao",-5))

module.exports = repeatStr;
9 changes: 9 additions & 0 deletions Sprint-3/2-practice-tdd/repeat-str.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,22 @@ test("should repeat the string count times", () => {
// 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.
test("should repeat the string only once", () => {
expect(repeatStr("nihao",1)).toEqual("nihao");
});

// 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.
test("should repeat the string 0", () => {
expect(repeatStr("newyear",0)).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 repeat the string negative times", () => {
expect(repeatStr("celebration", -5)).toEqual("invalid count");
});
Empty file added example.js
Empty file.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"keywords": [],
"author": "Code Your Future",
"license": "ISC",
"dependencies": {
"jest": "^29.7.0"
"devDependencies": {
"jest": "^30.2.0"
}
}
}