-
-
Notifications
You must be signed in to change notification settings - Fork 220
LONDON | MAY_25 | EMILIANO URUENA | SPRINT3 #595
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
1019587
a998f9f
e137935
ab48743
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Implement a function getAngleType | ||
// Build up your function case by case, writing tests as you go | ||
// The first test and case is written for you. The next case has a test, but no code. | ||
// Execute this script in your terminal | ||
// node 1-get-angle-type.js | ||
// The assertion error will tell you what the expected output is | ||
// Write the code to pass the test | ||
// Then, write the next test! :) Go through this process until all the cases are implemented | ||
|
||
function getAngleType(angle) { | ||
if (angle === 90) return "Right angle"; | ||
// read to the end, complete line 36, then pass your test here | ||
} | ||
|
||
// we're going to use this helper function to make our assertions easier to read | ||
// if the actual output matches the target output, the test will pass | ||
function assertEquals(actualOutput, targetOutput) { | ||
console.assert( | ||
actualOutput === targetOutput, | ||
`Expected ${actualOutput} to equal ${targetOutput}` | ||
); | ||
} | ||
|
||
// Acceptance criteria: | ||
|
||
// Given an angle in degrees, | ||
// When the function getAngleType is called with this angle, | ||
// Then it should: | ||
|
||
// Case 1: Identify Right Angles: | ||
// When the angle is exactly 90 degrees, | ||
// Then the function should return "Right angle" | ||
const right = getAngleType(90); | ||
assertEquals(right, "Right angle"); | ||
|
||
// Case 2: Identify Acute Angles: | ||
// When the angle is less than 90 degrees, | ||
// Then the function should return "Acute angle" | ||
const acute = getAngleType(45); | ||
assertEquals(acute, "Acute angle"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just curious if you got this assertion statement to work? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is working now. |
||
|
||
// Case 3: Identify Obtuse Angles: | ||
// When the angle is greater than 90 degrees and less than 180 degrees, | ||
// Then the function should return "Obtuse angle" | ||
const obtuse = getAngleType(120); | ||
// ====> write your test here, and then add a line to pass the test in the function above | ||
|
||
// Case 4: Identify Straight Angles: | ||
// When the angle is exactly 180 degrees, | ||
// Then the function should return "Straight angle" | ||
// ====> write your test here, and then add a line to pass the test in the function above | ||
|
||
// Case 5: Identify Reflex Angles: | ||
// When the angle is greater than 180 degrees and less than 360 degrees, | ||
// Then the function should return "Reflex angle" | ||
// ====> write your test here, and then add a line to pass the test in the function above |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Implement a function isProperFraction | ||
// Write assertions for your function to check it works in different cases | ||
// Terms: | ||
// Fractions: https://www.bbc.co.uk/bitesize/topics/zt9n6g8/articles/zjxpp4j | ||
// Written here like this: 1/2 == Numerator/Denominator | ||
// the first test and first case is written for you | ||
// complete the rest of the tests and cases | ||
// write one test at a time, and make it pass, build your solution up methodically | ||
|
||
function isProperFraction(numerator, denominator) { | ||
if (numerator < denominator) return true; | ||
} | ||
|
||
// here's our helper again | ||
function assertEquals(actualOutput, targetOutput) { | ||
console.assert( | ||
actualOutput === targetOutput, | ||
`Expected ${actualOutput} to equal ${targetOutput}` | ||
); | ||
} | ||
|
||
// Acceptance criteria: | ||
|
||
// Proper Fraction check: | ||
// Input: numerator = 2, denominator = 3 | ||
// target output: true | ||
// Explanation: The fraction 2/3 is a proper fraction, where the numerator is less than the denominator. The function should return true. | ||
const properFraction = isProperFraction(2, 3); | ||
assertEquals(properFraction, true); | ||
|
||
// Improper Fraction check: | ||
// Input: numerator = 5, denominator = 2 | ||
// target output: false | ||
// 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. | ||
const improperFraction = isProperFraction(5, 2); | ||
assertEquals(improperFraction, false); | ||
|
||
// Negative Fraction check: | ||
// Input: numerator = -4, denominator = 7 | ||
// target output: true | ||
// 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. | ||
const negativeFraction = isProperFraction(-4, 7); | ||
// ====> complete with your assertion | ||
|
||
// Equal Numerator and Denominator check: | ||
// Input: numerator = 3, denominator = 3 | ||
// target output: false | ||
// Explanation: The fraction 3/3 is not a proper fraction because the numerator is equal to the denominator. The function should return false. | ||
const equalFraction = isProperFraction(3, 3); | ||
// ====> complete with your assertion | ||
|
||
// Stretch: | ||
// What other scenarios could you test for? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another test case scenario that you could cover is when the absolute numerator value is bigger than the denominator? Example There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a good point, I added to the next lines. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// This problem involves playing cards: https://en.wikipedia.org/wiki/Standard_52-card_deck | ||
|
||
// You will need to implement a function getCardValue | ||
// the function takes a single parameter, a string representing a playing card | ||
// the function should return the numerical value of the card | ||
// the first test and first case is written for you | ||
// complete the rest of the tests and cases | ||
// write one test at a time, and make it pass, build your solution up methodically | ||
// just make one change at a time -- don't rush -- programmers are deep and careful thinkers | ||
function getCardValue(card) { | ||
if (rank === "A") return 11; | ||
} | ||
|
||
// You need to write assertions for your function to check it works in different cases | ||
// we're going to use this helper function to make our assertions easier to read | ||
// if the actual output matches the target output, the test will pass | ||
function assertEquals(actualOutput, targetOutput) { | ||
console.assert( | ||
actualOutput === targetOutput, | ||
`Expected ${actualOutput} to equal ${targetOutput}` | ||
); | ||
} | ||
// Acceptance criteria: | ||
|
||
// 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), | ||
// When the function getCardValue is called with this card string as input, | ||
// Then it should return the numerical card value | ||
const aceofSpades = getCardValue("A♠"); | ||
assertEquals(aceofSpades, 11); | ||
|
||
// Handle Number Cards (2-10): | ||
// Given a card with a rank between "2" and "9", | ||
// When the function is called with such a card, | ||
// Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5). | ||
const fiveofHearts = getCardValue("5♥"); | ||
// ====> write your test here, and then add a line to pass the test in the function above | ||
|
||
// Handle Face Cards (J, Q, K): | ||
// Given a card with a rank of "10," "J," "Q," or "K", | ||
// When the function is called with such a card, | ||
// Then it should return the value 10, as these cards are worth 10 points each in blackjack. | ||
|
||
// Handle Ace (A): | ||
// Given a card with a rank of "A", | ||
// When the function is called with an Ace, | ||
// Then it should, by default, assume the Ace is worth 11 points, which is a common rule in blackjack. | ||
|
||
// Handle Invalid Cards: | ||
// Given a card with an invalid rank (neither a number nor a recognized face card), | ||
// When the function is called with such a card, | ||
// Then it should throw an error indicating "Invalid card rank." |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
function getAngleType(angle) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function looks good There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you Sr. |
||
if (angle === 90) return "Right angle"; | ||
if (angle < 90) return "Acute angle"; | ||
if (angle >90 && angle < 180) return "Obtuse angle"; | ||
if (angle === 180) return "Straight angle"; | ||
if (angle > 180) return "Reflex angle"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Stretch: Is the check for What about when the inputted angle is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi.. I've added the line 2 for angles upper 360 degrees. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just added in case of invalid values like 361.
|
||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
// Don't get bogged down in this detail | ||
// Jest uses CommonJS module syntax by default as it's quite old | ||
// We will upgrade our approach to ES6 modules in the next course module, so for now | ||
// we have just written the CommonJS module.exports syntax for you | ||
module.exports = getAngleType; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
const getAngleType = require("./1-get-angle-type"); | ||
|
||
test("Should identify Right angle for exactly 90 degrees", () => { | ||
expect(getAngleType(90)).toEqual("Right angle"); | ||
}); | ||
|
||
// REPLACE the comments with the tests | ||
// make your test descriptions as clear and readable as possible | ||
|
||
// Case 2: Identify Acute Angles: | ||
// When the angle is less than 90 degrees, | ||
// Then the function should return "Acute angle" | ||
test("Should identify 'Acute angle' for angles less than 90 degrees (e.g., 45°) ", () => { | ||
expect(getAngleType(45)).toEqual("Acute angle"); | ||
}); | ||
|
||
// Case 3: Identify Obtuse Angles: | ||
// When the angle is greater than 90 degrees and less than 180 degrees, | ||
// Then the function should return "Obtuse angle" | ||
test("Should identify 'Obtuse angle' for angles greater than 90 and less than 180 degrees (e.g., 120°)", () => { | ||
expect(getAngleType(120)).toEqual("Obtuse angle"); | ||
}); | ||
|
||
// Case 4: Identify Straight Angles: | ||
// When the angle is exactly 180 degrees, | ||
// Then the function should return "Straight angle" | ||
test("should identify 'Straight angle' for angles of exactly 180°", () => { | ||
expect(getAngleType(180)).toEqual("Straight angle"); | ||
}); | ||
|
||
// Case 5: Identify Reflex Angles: | ||
// When the angle is greater than 180 degrees and less than 360 degrees, | ||
// Then the function should return "Reflex angle" | ||
test("should identify 'Reflex angle' for angles greater than 180 and less than 360 degrees (e.g., 200°)", () => { | ||
expect(getAngleType(200)).toEqual("Reflex angle"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Including multiple expect statements for reflex angles, obtuse & acute angles would be nice.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't consider it necessary, but It's a good point. |
||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
function isProperFraction(numerator, denominator) { | ||
if (numerator < 0 ) numerator *= -1; | ||
if (denominator < 0 ) denominator *= -1; | ||
if (numerator < denominator) return true; | ||
if (numerator >= denominator) return false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function looks clean and the logic is easy to follow. Students have also used this concise check:
What if we enter a denominator which is 0? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought that the function only would receive valid fractions. But I just added the line in that case. |
||
} | ||
|
||
module.exports = isProperFraction; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
const isProperFraction = require("./2-is-proper-fraction"); | ||
|
||
test("should return true for a proper fraction", () => { | ||
expect(isProperFraction(2, 3)).toEqual(true); | ||
}); | ||
|
||
// Case 2: Identify Improper Fractions: | ||
test("should return false for an improper fraction", () => { | ||
expect(isProperFraction(5, 2)).toEqual(false); | ||
}); | ||
|
||
// Case 3: Identify Negative Fractions: | ||
test("should return true for a Negative Fraction", () => { | ||
expect(isProperFraction(-4, 7)).toEqual(true); | ||
}); | ||
|
||
// Case 4: Identify Equal Numerator and Denominator: | ||
test("should return false for improper fraction because the numerator is equal to the denominator", () => { | ||
expect(isProperFraction(3, 3)).toEqual(false); | ||
}); | ||
|
||
// | ||
test("should return false, The fraction -5/-2 is a improper fraction because the absolute value of the numerator (5) is more than the absolute value of denominator (2). The function should return false.", () => { | ||
expect(isProperFraction(-5, -2)).toEqual(false); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
function getCardValue(card) { | ||
let rank = card.slice(0,-1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good job. The use of the slice string method to remove the last character of the card is appreciated. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Many thanks. |
||
if (rank === "A") return 11; | ||
if (Number.isInteger(Number(rank))) return Number(rank); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens if the user enters a invalid card value? Example:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wow that's a good point. I added some conditions Number(rank) >= 2 && Number(rank) <= 10 to validate that. |
||
if (rank === 'K' || rank === 'J' || rank === 'Q'){ return 10; | ||
} else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this case the function seems to work as expected, be careful when using The
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Underestood. |
||
return "Invalid card rank.";} | ||
} | ||
module.exports = getCardValue; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
const getCardValue = require("./3-get-card-value"); | ||
|
||
test("should return 11 for Ace of Spades", () => { | ||
const aceofSpades = getCardValue("A♠"); | ||
expect(aceofSpades).toEqual(11); | ||
}); | ||
|
||
// Case 2: Handle Number Cards (2-10): | ||
test("should return 5 for five of Hearts", () => { | ||
expect(getCardValue("5♥")).toEqual(5); | ||
}); | ||
|
||
// Case 3: Handle Face Cards (J, Q, K): | ||
test("should return 10 for King of Hearts", () => { | ||
expect(getCardValue("K♥")).toEqual(10); | ||
}); | ||
|
||
// Case 4: Handle Ace (A): | ||
test("should return 11 for Ace of Diamonds", () => { | ||
expect(getCardValue("A♦")).toEqual(11); | ||
}); | ||
|
||
// Case 5: Handle Invalid Cards: | ||
test("should return 'Invalid card rank.' for invalid cards.", () => { | ||
expect(getCardValue("Z♠")).toEqual("Invalid card rank."); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
function countChar(stringOfCharacters, findCharacter) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The logic in this function uses a for loop effectively, well done. |
||
let count = 0; | ||
for (i = 0 ;i < stringOfCharacters.length; i++) { | ||
if (stringOfCharacters[i] === findCharacter) count += 1; | ||
} | ||
return count; | ||
} | ||
|
||
module.exports = countChar; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// implement a function countChar that counts the number of times a character occurs in a string | ||
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: | ||
|
||
// Scenario: Multiple Occurrences | ||
// Given the input string str, | ||
// And a character char that may occur multiple times with overlaps within str (e.g., 'a' in 'aaaaa'), | ||
// When the function is called with these inputs, | ||
// Then it should correctly count overlapping occurrences of char (e.g., 'a' appears five times in 'aaaaa'). | ||
|
||
test("should count multiple occurrences of a character", () => { | ||
const str = "aaaaa"; | ||
const char = "a"; | ||
const count = countChar(str, char); | ||
expect(count).toEqual(5); | ||
}); | ||
|
||
// Scenario: No Occurrences | ||
// Given the input string str, | ||
// And a character char that does not exist within the case-sensitive str, | ||
// When the function is called with these inputs, | ||
// Then it should return 0, indicating that no occurrences of the char were found in the case-sensitive str. | ||
test("should count multiple occurrences of a character", () => { | ||
const str = "astronaut"; | ||
const char = "t"; | ||
const count = countChar(str, char); | ||
expect(count).toEqual(2); | ||
}); | ||
|
||
test("should count multiple occurrences of a character", () => { | ||
const str = "multiply"; | ||
const char = "e"; | ||
const count = countChar(str, char); | ||
expect(count).toEqual(0); | ||
}); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How would the function "behave" if we provided an empty string for both the |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
function getOrdinalNumber(num) { | ||
let sufix = 'th'; | ||
if (num === 11 || num === 12 || num === 13){ | ||
sufix = 'th'; | ||
return num + sufix; | ||
} else { | ||
if (num.toString().slice(-1) === '1') sufix = 'st'; | ||
if (num.toString().slice(-1) === '2') sufix = 'nd'; | ||
if (num.toString().slice(-1) === '3') sufix = 'rd'; | ||
return num + sufix; | ||
} | ||
} | ||
|
||
module.exports = getOrdinalNumber; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can see that there is an implementation of
getAngleType
in2-mandatory-rewrite/1-get-angle-type.js
.Perhaps it would also be good to copy that to this file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right! I just did it.