Skip to content

Commit 3e9bea5

Browse files
author
cjyuan
committed
Updated 1-implement-and-rewrite-test
1 parent 3635e58 commit 3e9bea5

File tree

3 files changed

+76
-125
lines changed

3 files changed

+76
-125
lines changed
Lines changed: 21 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,43 @@
11
// Implement a function getAngleType
2-
// Build up your function case by case, writing tests as you go
3-
// The first test and case is written for you. The next case has a test, but no code.
4-
// Execute this script in your terminal
5-
// node 1-get-angle-type.js
6-
// The assertion error will tell you what the expected output is
7-
// Write the code to pass the test
8-
// Then, write the next test! :) Go through this process until all the cases are implemented
2+
//
3+
// When given an angle in degrees, it should return a string indicating the type of angle:
4+
// - "Acute angle" for angles greater than 0 and less than 90 degrees
5+
// - "Right angle" for exactly 90 degrees
6+
// - "Obtuse angle" for angles greater than 90 degrees and less than 180 degrees
7+
// - "Straight angle" for exactly 180 degrees
8+
// - "Reflex angle" for angles greater than 180 degrees and less than 360 degrees
9+
//
10+
// If the parameter is out of the valid range, the function should return "Invalid angle".
11+
12+
// Assumption: The parameter is a valid number. (You do not need to handle non-numeric inputs.)
13+
14+
// Acceptance criteria:
15+
// After you have implemented the function, write tests to cover all the cases, and
16+
// execute the code to ensure all tests pass.
917

1018
function getAngleType(angle) {
1119
if (angle === 90) {
1220
return "Right angle";
1321
}
14-
// Run the tests, work out what Case 2 is testing, and implement the required code here.
15-
// Then keep going for the other cases, one at a time.
22+
23+
// TODO: Compete the implementation
1624
}
1725

1826
// The line below allows us to load the getAngleType function into tests in other files.
1927
// This will be useful in the "rewrite tests with jest" step.
2028
module.exports = getAngleType;
2129

22-
// we're going to use this helper function to make our assertions easier to read
23-
// if the actual output matches the target output, the test will pass
30+
// This helper function is written to make our assertions easier to read.
31+
// If the actual output matches the target output, the test will pass
2432
function assertEquals(actualOutput, targetOutput) {
2533
console.assert(
2634
actualOutput === targetOutput,
2735
`Expected ${actualOutput} to equal ${targetOutput}`
2836
);
2937
}
3038

31-
// Acceptance criteria:
32-
33-
// Given an angle in degrees,
34-
// When the function getAngleType is called with this angle,
35-
// Then it should:
36-
37-
// Case 1: Identify Right Angles:
38-
// When the angle is exactly 90 degrees,
39-
// Then the function should return "Right angle"
39+
// TODO: Write tests to cover all cases, including boundary and invalid cases.
40+
// Example: Identify Right Angles
4041
const right = getAngleType(90);
4142
assertEquals(right, "Right angle");
4243

43-
// Case 2: Identify Acute Angles:
44-
// When the angle is less than 90 degrees,
45-
// Then the function should return "Acute angle"
46-
const acute = getAngleType(45);
47-
assertEquals(acute, "Acute angle");
48-
49-
// Case 3: Identify Obtuse Angles:
50-
// When the angle is greater than 90 degrees and less than 180 degrees,
51-
// Then the function should return "Obtuse angle"
52-
const obtuse = getAngleType(120);
53-
// ====> write your test here, and then add a line to pass the test in the function above
54-
55-
// Case 4: Identify Straight Angles:
56-
// When the angle is exactly 180 degrees,
57-
// Then the function should return "Straight angle"
58-
// ====> write your test here, and then add a line to pass the test in the function above
59-
60-
// Case 5: Identify Reflex Angles:
61-
// When the angle is greater than 180 degrees and less than 360 degrees,
62-
// Then the function should return "Reflex angle"
63-
// ====> write your test here, and then add a line to pass the test in the function above
Lines changed: 17 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,33 @@
1-
// Implement a function isProperFraction
2-
// Write assertions for your function to check it works in different cases
3-
// Terms:
4-
// Fractions: https://www.bbc.co.uk/bitesize/topics/zt9n6g8/articles/zjxpp4j
5-
// 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.
912

1013
function isProperFraction(numerator, denominator) {
11-
if (numerator < denominator) {
12-
return true;
13-
}
14+
// TODO: Implement this function
1415
}
1516

1617
// The line below allows us to load the isProperFraction function into tests in other files.
1718
// This will be useful in the "rewrite tests with jest" step.
1819
module.exports = isProperFraction;
1920

20-
// here's our helper again
21+
// Here's our helper again
2122
function assertEquals(actualOutput, targetOutput) {
2223
console.assert(
2324
actualOutput === targetOutput,
2425
`Expected ${actualOutput} to equal ${targetOutput}`
2526
);
2627
}
2728

28-
// Acceptance criteria:
29+
// TODO: Write tests to cover all cases.
30+
// What combinations of numerators and denominators should you test?
2931

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-
const properFraction = 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-
const improperFraction = 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-
const negativeFraction = 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.
55-
const equalFraction = isProperFraction(3, 3);
56-
// ====> complete with your assertion
57-
58-
// Stretch:
59-
// What other scenarios could you test for?
32+
// Example: 1/2 is a proper fraction
33+
assertEquals(isProperFraction(1, 2), true);
Lines changed: 38 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,54 @@
11
// This problem involves playing cards: https://en.wikipedia.org/wiki/Standard_52-card_deck
22

3-
// You will need to implement a function getCardValue
4-
// the function takes a single parameter, a string representing a playing card
5-
// the function should return the numerical value of the card
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
9-
// just make one change at a time -- don't rush -- programmers are deep and careful thinkers
3+
// Implement a function getCardValue,
4+
// when given a string representing a playing card, it should return the numerical value of the card.
5+
6+
// A valid card string will contain a rank followed by the suit.
7+
// The rank can be one of the following strings:
8+
// "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"
9+
// The suit can be one of the following emojis:
10+
// "♠", "♥", "♦", "♣"
11+
// 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+
1024
function getCardValue(card) {
11-
if (rank === "A") {
12-
return 11;
13-
}
25+
// TODO: Complete the implementation
1426
}
1527

1628
// The line below allows us to load the getCardValue function into tests in other files.
1729
// This will be useful in the "rewrite tests with jest" step.
1830
module.exports = getCardValue;
1931

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.
2333
function assertEquals(actualOutput, targetOutput) {
2434
console.assert(
2535
actualOutput === targetOutput,
2636
`Expected ${actualOutput} to equal ${targetOutput}`
2737
);
2838
}
29-
// Acceptance criteria:
3039

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-
const aceofSpades = 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-
const fiveofHearts = 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

Comments
 (0)