-
-
Notifications
You must be signed in to change notification settings - Fork 184
London | Saba Farjamfard |Module-Structuring-and-Testing-data | Sprint 3 | Week 3 #160
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
base: main
Are you sure you want to change the base?
Changes from 15 commits
0ef3b40
879e1e8
a8532e5
fd4e76d
6e960af
7b3d968
404c59a
eed572c
01b0e55
506356d
e836797
bd1d4dd
5bae5d3
27044bc
2baca3b
119cdf5
0fb5862
f31f756
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 |
---|---|---|
|
@@ -25,3 +25,34 @@ | |
// Identify Reflex Angles: | ||
// When the angle is greater than 180 degrees and less than 360 degrees, | ||
// Then the function should return "Reflex angle" | ||
|
||
function getAngleType(angle) { | ||
if (angle === 90) { | ||
return "Right angle"; | ||
} else if (angle < 90 && angle >=0) { | ||
return "Acute angle"; | ||
} else if (angle > 90 && angle < 180) { | ||
return "Obtuse angle"; | ||
} else if (angle === 180) { | ||
return "Straight angle"; | ||
} else if (angle > 180 && angle < 360) { | ||
return "Reflex angle"; | ||
} else { | ||
return "Invalid angle"; | ||
} | ||
} | ||
module.exports = getAngleType; | ||
|
||
|
||
//Test code | ||
if (require.main === module) { | ||
const getAngleType = require('./get-angle-type'); | ||
|
||
console.log(getAngleType(90) === "Right angle" ? "Pass" : "Fail"); | ||
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. There aren't any assertions in here. Can you go through a bit what your goal is here? 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. hello 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. Could also test 0, a borderline case. 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. hello |
||
console.log(getAngleType(45) === "Acute angle" ? "Pass" : "Fail"); | ||
console.log(getAngleType(120) === "Obtuse angle" ? "Pass" : "Fail"); | ||
console.log(getAngleType(180) === "Straight angle" ? "Pass" : "Fail"); | ||
console.log(getAngleType(270) === "Reflex angle" ? "Pass" : "Fail"); | ||
console.log(getAngleType(360) === "Invalid angle" ? "Pass" : "Fail"); | ||
console.log(getAngleType(-30) === "Invalid angle" ? "Pass" : "Fail"); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,10 @@ | ||
// This problem involves playing cards: https://en.wikipedia.org/wiki/Standard_52-card_deck | ||
|
||
// You will need to implement a function getCardValue | ||
|
||
// You need to write assertions for your function to check it works in different cases | ||
|
||
// 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), | ||
|
||
// 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 | ||
|
||
|
@@ -29,3 +27,50 @@ | |
// 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." | ||
|
||
|
||
function getCardValue(card) { | ||
const cardRank = card[0]; // The first character represents the rank of the card | ||
|
||
// If the card is a number between 2 and 9 | ||
if (cardRank >= '2' && cardRank <= '9') { | ||
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. Part of the description mentions:
So that implies the rank could possibly be "20" or "B", or "111". The description does not say anything about the suite (the last character), so we can presume the last character is always a valid suite character. |
||
return parseInt(cardRank); | ||
} else if (cardRank === '1' && card[1] === '0') { // Card 10 | ||
return 10; | ||
} | ||
// Face cards J, Q, K | ||
else if (cardRank === 'J' || cardRank === 'Q' || cardRank === 'K') { | ||
return 10; | ||
} | ||
// Ace card A | ||
else if (cardRank === 'A') { | ||
return 11; | ||
} | ||
// Invalid card | ||
else { | ||
throw new Error("Invalid card rank"); | ||
} | ||
} | ||
|
||
module.exports = getCardValue; | ||
|
||
|
||
|
||
if (require.main === module) { | ||
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 think we've got in a bit of a muddle here! 😅 It doesn't need to be this complicated. You can just write your assertions (and they should be assertions, not logs) without this check. 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, |
||
try { | ||
console.log(getCardValue("5") === 5 ? "Pass" : "Fail"); // Numeric card 5 | ||
console.log(getCardValue("9") === 9 ? "Pass" : "Fail"); // Numeric card 9 | ||
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 value passed to the function should always have a suite character as the last character. For examples, "A♠", "10♠". You can copy the character in the description (or just substitute the suite character by another character since the function is not required to validate the suite character). Using a boundary value such as "2♠" as a test case is always a good idea. Many program fails because they do not check the boundary cases properly. |
||
console.log(getCardValue("10") === 10 ? "Pass" : "Fail"); // Card 10 | ||
console.log(getCardValue("J") === 10 ? "Pass" : "Fail"); // Face card J | ||
console.log(getCardValue("Q") === 10 ? "Pass" : "Fail"); // Face card Q | ||
console.log(getCardValue("K") === 10 ? "Pass" : "Fail"); // Face card K | ||
console.log(getCardValue("A") === 11 ? "Pass" : "Fail"); // Ace card A | ||
console.log(getCardValue("X") === "Invalid card rank" ? "Fail" : "Pass"); // Invalid card | ||
} catch (error) { | ||
if (error.message === "Invalid card rank") { | ||
console.log("Pass"); | ||
} else { | ||
console.log("Fail"); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,19 @@ | ||
// You wil need to implement a function isProperFraction | ||
// You need to 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 | ||
|
||
// 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. | ||
|
||
// Explanation: The fraction 2/3 is a proper fraction, where the numerator is less than the denominator. | ||
//The function should return 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. | ||
// 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. | ||
|
||
// Zero Denominator check: | ||
// Input: numerator = 3, denominator = 0 | ||
|
@@ -25,10 +23,63 @@ | |
// 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. | ||
// 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. | ||
|
||
// 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. | ||
// These acceptance criteria cover a range of scenarios to ensure that the isProperFraction function handles both proper and improper fractions correctly and handles potential errors such as a zero denominator. | ||
// Explanation: The fraction 3/3 is not a proper fraction because the numerator is equal to the denominator. | ||
//The function should return false. | ||
|
||
// These acceptance criteria cover a range of scenarios to ensure that the isProperFraction function handles | ||
//both proper and improper fractions correctly and handles potential errors such as a zero denominator. | ||
|
||
|
||
function isProperFraction(numerator, denominator) { | ||
// If the denominator is zero, throw an error | ||
if (denominator === 0) { | ||
throw new Error("Denominator cannot be zero"); | ||
} | ||
|
||
// Check if numerator and denominator are equal (not a proper fraction) | ||
if (Math.abs(numerator) === Math.abs(denominator)) { | ||
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 check is optional because |
||
return false; | ||
} | ||
|
||
// Check if it's a proper fraction by comparing absolute values | ||
// Also ensures proper handling of negative fractions | ||
return Math.abs(numerator) < Math.abs(denominator); | ||
} | ||
|
||
module.exports = isProperFraction; | ||
|
||
//code test | ||
if (require.main === module) { | ||
try { | ||
// Proper Fraction check: numerator = 2, denominator = 3 | ||
console.log(isProperFraction(2, 3) === true ? "Pass" : "Fail"); // Proper fraction: 2/3 should return true | ||
|
||
// Improper Fraction check: numerator = 5, denominator = 2 | ||
console.log(isProperFraction(5, 2) === false ? "Pass" : "Fail"); // Improper fraction: 5/2 should return false | ||
|
||
// Zero Denominator check: numerator = 3, denominator = 0 | ||
try { | ||
isProperFraction(3, 0); | ||
console.log("Fail"); // This should throw an error, so if it executes, it should print Fail | ||
} catch (error) { | ||
console.log(error.message === "Denominator cannot be zero" ? "Pass" : "Fail"); | ||
} | ||
// Negative Fraction check: numerator = -4, denominator = 7 | ||
console.log(isProperFraction(-4, 7) === true ? "Pass" : "Fail"); // Proper fraction: -4/7 should return true | ||
|
||
// Equal Numerator and Denominator check: numerator = 3, denominator = 3 | ||
console.log(isProperFraction(3, 3) === false ? "Pass" : "Fail"); // Improper fraction: 3/3 should return false | ||
|
||
// Negative Improper Fraction check: numerator = -8, denominator = -5 | ||
console.log(isProperFraction(-8, -5) === false ? "Pass" : "Fail"); // Improper fraction: -8/-5 should return false | ||
|
||
} catch (error) { | ||
console.error("Test failed:", error); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,21 +5,22 @@ | |
// Side a = 3 | ||
// Side b = 3 | ||
// Side c = 3 | ||
|
||
// This is a valid triangle, because a plus b = 6 and 6 is greater than 3 | ||
// Another way to write this is a + b > c | ||
// It's also true that b + c > a | ||
// It's also true that a + c > b | ||
// In our function isValidTriangle which takes as parameters the lengths of three sides, we need to invalidate | ||
//any triangle where the sum of any two sides is less than or equal to the length of the third side. | ||
|
||
// In our function isValidTriangle which takes as parameters the lengths of three sides, we need to invalidate any triangle where the sum of any two sides is less than or equal to the length of the third side. | ||
// and we need to validate any triangle where the sum of any two sides is greater than the length of the third side. | ||
|
||
// Acceptance criteria: | ||
|
||
// scenario: invalid triangle | ||
// Given the side lengths a, b, and c, | ||
// When the sum of any two side lengths is less than or equal to the length of the third side (i.e., a + b <= c, a + c <= b, b + c <= a), | ||
// Then it should return false because these conditions violate the Triangle Inequality, which states that the sum of the lengths of any two sides of a triangle must be greater than the length of the third side. | ||
// When the sum of any two side lengths is less than or equal to the length of the third side | ||
//(i.e., a + b <= c, a + c <= b, b + c <= a), | ||
|
||
// Then it should return false because these conditions violate the Triangle Inequality, which states that the sum of | ||
//the lengths of any two sides of a triangle must be greater than the length of the third side. | ||
|
||
// scenario: invalid triangle | ||
// Check for Valid Input: | ||
|
@@ -32,4 +33,48 @@ | |
// When the function is called with these values as input, | ||
// Then it should return true because the input forms a valid triangle. | ||
|
||
// This specification outlines the behavior of the isValidTriangle function for different input scenarios, ensuring it properly checks for invalid side lengths and whether they form a valid triangle according to the Triangle Inequality Theorem. | ||
// This specification outlines the behavior of the isValidTriangle function for different input scenarios, | ||
//ensuring it properly checks for invalid side lengths and whether they form a valid triangle according to the Triangle Inequality Theorem. | ||
|
||
function isValidTriangle(a, b, c) { | ||
// Check if any side is less than or equal to zero | ||
if (a <= 0 || b <= 0 || c <= 0) { | ||
return false; | ||
} | ||
|
||
// Check the Triangle Inequality conditions | ||
if (a + b > c && a + c > b && b + c > a) { | ||
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. Since after line 45, the return value is determined only by the value of |
||
return true; | ||
} | ||
|
||
// If any of the Triangle Inequality conditions fail, return false | ||
return false; | ||
} | ||
|
||
module.exports = isValidTriangle; | ||
|
||
//test code | ||
if (require.main === module) { | ||
try { | ||
// Test for a valid triangle | ||
console.log(isValidTriangle(3, 4, 5) === true ? "Pass" : "Fail"); // Should return true (valid triangle) | ||
|
||
// Test for an invalid triangle where the sum of two sides is equal to the third side | ||
console.log(isValidTriangle(1, 2, 3) === false ? "Pass" : "Fail"); // Should return false (invalid triangle) | ||
|
||
// Test for an invalid triangle where one side is zero | ||
console.log(isValidTriangle(0, 4, 5) === false ? "Pass" : "Fail"); // Should return false (side is zero) | ||
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. Could also add test cases that test invalid value in the 2nd and 3rd parameters. |
||
|
||
// Test for an invalid triangle where one side is negative | ||
console.log(isValidTriangle(-3, 4, 5) === false ? "Pass" : "Fail"); // Should return false (side is negative) | ||
|
||
// Test for an invalid triangle where the sum of two sides is less than the third side | ||
console.log(isValidTriangle(1, 1, 3) === false ? "Pass" : "Fail"); // Should return false (invalid triangle) | ||
|
||
// Test for a valid triangle where all sides are positive and satisfy the Triangle Inequality | ||
console.log(isValidTriangle(6, 8, 10) === true ? "Pass" : "Fail"); // Should return true (valid triangle) | ||
|
||
} catch (error) { | ||
console.error("Test failed:", error); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,29 +15,66 @@ | |
// Scenario: Rotate Lowercase Letters: | ||
// Given a lowercase letter character and a positive integer shift, | ||
// When the function is called with these inputs, | ||
// Then it should rotate the lowercase letter by shift positions within the lowercase alphabet, wrapping around if necessary, and return the rotated lowercase letter as a string. | ||
// Then it should rotate the lowercase letter by shift positions within the lowercase alphabet, wrapping around if necessary, | ||
//and return the rotated lowercase letter as a string. | ||
console.log(rotateCharacter("a", 3)); // Output: "d" | ||
console.log(rotateCharacter("f", 1)); // Output: "g" | ||
|
||
// Scenario: Rotate Uppercase Letters: | ||
// Given an uppercase letter character and a positive integer shift, | ||
// When the function is called with these inputs, | ||
// Then it should rotate the uppercase letter by shift positions within the uppercase alphabet, wrapping around if necessary, and return the rotated uppercase letter as a string. | ||
// Then it should rotate the uppercase letter by shift positions within the uppercase alphabet, wrapping around if necessary, | ||
//and return the rotated uppercase letter as a string. | ||
console.log(rotateCharacter("A", 3)); // Output: "D" | ||
console.log(rotateCharacter("F", 1)); // Output: "G" | ||
|
||
// Scenario: Leave Non-Letter Characters Unchanged: | ||
// Given a character that is not a letter (neither uppercase nor lowercase) and any positive or negative shift value, | ||
// When the function is called with these inputs, | ||
// Then it should return the character unchanged. | ||
// This specification outlines the behavior of the rotateCharacter function for different input scenarios, including valid and invalid characters, and defines the expected output or action for each case. | ||
// This specification outlines the behavior of the rotateCharacter function for different input scenarios, including valid | ||
//and invalid characters, and defines the expected output or action for each case. | ||
console.log(rotateCharacter("7", 5)); // Output: "7" (unchanged, not a letter) | ||
|
||
// Scenario: Shifting a Character with Wraparound | ||
// Given a character char within the lowercase alphabet range (e.g., 'z') or the uppercase alphabet range (e.g., 'Z'), | ||
// And a positive integer shift that causes the character to wrap around the alphabet when rotated (e.g., a shift of 3 for 'z' or 'Z'), | ||
// And a positive integer shift that causes the character to wrap around the alphabet when rotated (e.g., | ||
//a shift of 3 for 'z' or 'Z'), | ||
|
||
// When the rotateCharacter function is called with char and shift as inputs, | ||
// Then it should correctly rotate the character by shift positions within the alphabet while handling the wraparound, | ||
// And the function should return the rotated character as a string (e.g., 'z' rotated by 3 should become 'c', 'Z' rotated by 3 should become 'C'). | ||
// And the function should return the rotated character as a string (e.g., 'z' rotated by 3 should become 'c', 'Z' | ||
//rotated by 3 should become 'C'). | ||
console.log(rotateCharacter("z", 1)); // Output: "a" (preserves case, but wraps around) | ||
console.log(rotateCharacter("Y", 2)); // Output: "A" (preserves case, but wraps around) | ||
|
||
|
||
function rotateCharacter(char, shift) { | ||
// Check if the character is a lowercase letter | ||
if (char >= 'a' && char <= 'z') { | ||
let start = 'a'.charCodeAt(0); // ASCII code for 'a' | ||
let offset = (char.charCodeAt(0) - start + shift) % 26; | ||
return String.fromCharCode(start + (offset >= 0 ? offset : offset + 26)); | ||
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. Very nice that you also considered negative shift value! |
||
} | ||
|
||
// Check if the character is an uppercase letter | ||
if (char >= 'A' && char <= 'Z') { | ||
let start = 'A'.charCodeAt(0); // ASCII code for 'A' | ||
let offset = (char.charCodeAt(0) - start + shift) % 26; | ||
return String.fromCharCode(start + (offset >= 0 ? offset : offset + 26)); | ||
} | ||
|
||
// If the character is not a letter, return it unchanged | ||
return char; | ||
} | ||
|
||
// Test code | ||
if (require.main === module) { | ||
console.log(rotateCharacter("a", 3) === 'd' ? "Pass" : "Fail"); // Expected output: "d" | ||
console.log(rotateCharacter("z", 3) === 'c' ? "Pass" : "Fail"); // Expected output: "c" (wrap around) | ||
console.log(rotateCharacter("A", 3) === 'D' ? "Pass" : "Fail"); // Expected output: "D" | ||
console.log(rotateCharacter("F", 1) === 'G' ? "Pass" : "Fail"); // Expected output: "G" | ||
console.log(rotateCharacter("7", 5) === '7' ? "Pass" : "Fail"); // Expected output: "7" (non-letter character) | ||
console.log(rotateCharacter("y", 2) === 'a' ? "Pass" : "Fail"); // Expected output: "a" (wrap around) | ||
console.log(rotateCharacter("Z", 3) === 'C' ? "Pass" : "Fail"); // Expected output: "C" (wrap around) | ||
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. Could also include test cases with larger shift values and negative shift values. |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Function to check if a credit card number is valid | ||
function isValidCreditCard(number) { | ||
// Rule 1: Check if the number is exactly 16 digits and contains only numbers | ||
if (typeof number !== 'string' || number.length !== 16 || !/^\d+$/.test(number)) { | ||
return false; | ||
} | ||
|
||
// Rule 2: Check if there are at least two different digits | ||
const uniqueDigits = new Set(number); | ||
if (uniqueDigits.size < 2) { | ||
return false; | ||
} | ||
|
||
// Rule 3: Check if the last digit is even | ||
const lastDigit = parseInt(number[number.length - 1], 10); | ||
if (lastDigit % 2 !== 0) { | ||
return false; | ||
} | ||
|
||
// Rule 4: Check if the sum of all digits is greater than 16 | ||
const sum = number.split('').reduce((acc, digit) => acc + parseInt(digit, 10), 0); | ||
if (sum <= 16) { | ||
return false; | ||
} | ||
|
||
// If all rules are met, the card is valid | ||
return true; | ||
} | ||
|
||
// Tests | ||
console.log(isValidCreditCard("9999777788880000")); // should return true | ||
console.log(isValidCreditCard("6666666666661666")); // should return true | ||
console.log(isValidCreditCard("a92332119c011112")); // should return false (invalid characters) | ||
console.log(isValidCreditCard("4444444444444444")); // should return false (only one type of digit) | ||
console.log(isValidCreditCard("1111111111111110")); // should return false (sum is less than 16) | ||
console.log(isValidCreditCard("6666666666666661")); // should return false (odd final number) | ||
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. Why not check also cases where number of digits is not exactly 16? |
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.
Oh hm, what's going on here?