You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Written here like this: 1/2 == Numerator/Denominator
6
-
// the first test and first case is written for you
7
-
// complete the rest of the tests and cases
8
-
// write one test at a time, and make it pass, build your solution up methodically
1
+
// Implement a function isProperFraction,
2
+
// when given two numbers, a numerator and a denominator, it should return true if the
3
+
// the given numbers form a proper fraction, and false otherwise.
4
+
5
+
// Assumption: The parameters are always valid numbers (excluding NaN and Infinity).
6
+
7
+
// Note: If you are unfamiliar with proper fractions, please look up its mathematical definition.
8
+
9
+
// Acceptance criteria:
10
+
// After you have implemented the function, write tests to cover all the cases, and
11
+
// execute the code to ensure all tests pass.
9
12
10
13
functionisProperFraction(numerator,denominator){
11
-
if(numerator<denominator){
12
-
returntrue;
13
-
}
14
+
// TODO: Implement this function
14
15
}
15
16
16
17
// The line below allows us to load the isProperFraction function into tests in other files.
17
18
// This will be useful in the "rewrite tests with jest" step.
18
19
module.exports=isProperFraction;
19
20
20
-
// here's our helper again
21
+
// Here's our helper again
21
22
functionassertEquals(actualOutput,targetOutput){
22
23
console.assert(
23
24
actualOutput===targetOutput,
24
25
`Expected ${actualOutput} to equal ${targetOutput}`
25
26
);
26
27
}
27
28
28
-
// Acceptance criteria:
29
+
// TODO: Write tests to cover all cases.
30
+
// What combinations of numerators and denominators should you test?
29
31
30
-
// Proper Fraction check:
31
-
// Input: numerator = 2, denominator = 3
32
-
// target output: true
33
-
// Explanation: The fraction 2/3 is a proper fraction, where the numerator is less than the denominator. The function should return true.
34
-
constproperFraction=isProperFraction(2,3);
35
-
assertEquals(properFraction,true);
36
-
37
-
// Improper Fraction check:
38
-
// Input: numerator = 5, denominator = 2
39
-
// target output: false
40
-
// Explanation: The fraction 5/2 is an improper fraction, where the numerator is greater than or equal to the denominator. The function should return false.
41
-
constimproperFraction=isProperFraction(5,2);
42
-
assertEquals(improperFraction,false);
43
-
44
-
// Negative Fraction check:
45
-
// Input: numerator = -4, denominator = 7
46
-
// target output: true
47
-
// Explanation: The fraction -4/7 is a proper fraction because the absolute value of the numerator (4) is less than the denominator (7). The function should return true.
48
-
constnegativeFraction=isProperFraction(-4,7);
49
-
// ====> complete with your assertion
50
-
51
-
// Equal Numerator and Denominator check:
52
-
// Input: numerator = 3, denominator = 3
53
-
// target output: false
54
-
// Explanation: The fraction 3/3 is not a proper fraction because the numerator is equal to the denominator. The function should return false.
// For examples, "A♠", "2♥", "10♥", "J♣", "Q♦", "K♦".
12
+
13
+
// When the card is an ace ("A"), the function should return 11.
14
+
// When the card is a face card ("J", "Q", "K"), the function should return 10.
15
+
// When the card is a number card ("2" to "10"), the function should return its numeric value.
16
+
17
+
// When the card string is invalid (not following the above format), the function should
18
+
// throw an error.
19
+
20
+
// Acceptance criteria:
21
+
// After you have implemented the function, write tests to cover all the cases, and
22
+
// execute the code to ensure all tests pass.
23
+
10
24
functiongetCardValue(card){
11
-
if(rank==="A"){
12
-
return11;
13
-
}
25
+
// TODO: Complete the implementation
14
26
}
15
27
16
28
// The line below allows us to load the getCardValue function into tests in other files.
17
29
// This will be useful in the "rewrite tests with jest" step.
18
30
module.exports=getCardValue;
19
31
20
-
// You need to write assertions for your function to check it works in different cases
21
-
// we're going to use this helper function to make our assertions easier to read
22
-
// if the actual output matches the target output, the test will pass
32
+
// Helper functions to make our assertions easier to read.
23
33
functionassertEquals(actualOutput,targetOutput){
24
34
console.assert(
25
35
actualOutput===targetOutput,
26
36
`Expected ${actualOutput} to equal ${targetOutput}`
27
37
);
28
38
}
29
-
// Acceptance criteria:
30
39
31
-
// Given a card string in the format "A♠" (representing a card in blackjack - the last character will always be an emoji for a suit, and all characters before will be a number 2-10, or one letter of J, Q, K, A),
32
-
// When the function getCardValue is called with this card string as input,
33
-
// Then it should return the numerical card value
34
-
constaceofSpades=getCardValue("A♠");
35
-
assertEquals(aceofSpades,11);
36
-
37
-
// Handle Number Cards (2-10):
38
-
// Given a card with a rank between "2" and "9",
39
-
// When the function is called with such a card,
40
-
// Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5).
41
-
constfiveofHearts=getCardValue("5♥");
42
-
// ====> write your test here, and then add a line to pass the test in the function above
43
-
44
-
// Handle Face Cards (J, Q, K):
45
-
// Given a card with a rank of "10," "J," "Q," or "K",
46
-
// When the function is called with such a card,
47
-
// Then it should return the value 10, as these cards are worth 10 points each in blackjack.
48
-
49
-
// Handle Ace (A):
50
-
// Given a card with a rank of "A",
51
-
// When the function is called with an Ace,
52
-
// Then it should, by default, assume the Ace is worth 11 points, which is a common rule in blackjack.
53
-
54
-
// Handle Invalid Cards:
55
-
// Given a card with an invalid rank (neither a number nor a recognized face card),
56
-
// When the function is called with such a card,
57
-
// Then it should throw an error indicating "Invalid card rank."
40
+
// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
41
+
// Examples:
42
+
assertEquals(getCardValue("9♠"),9);
43
+
44
+
45
+
// Handling invalid cards
46
+
try{
47
+
getCardValue("invalid");
48
+
49
+
// This line will not be reached if an error is thrown as expected
50
+
console.error("Error was not thrown for invalid card");
51
+
}catch(e){
52
+
}
53
+
54
+
// What other invalid card cases can you think of?
0 commit comments