-
-
Notifications
You must be signed in to change notification settings - Fork 50
NW6 | Nazanin-Saedi | Week-3-JS1 | week3 #163
base: main
Are you sure you want to change the base?
Changes from all commits
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 |
---|---|---|
|
@@ -24,7 +24,62 @@ console.assert( | |
); | ||
|
||
// formatAs12HourClock currently has a 🐛 | ||
const result = formatAs12HourClock("17:42"); | ||
asserts.strictEqual(result, "5:42 pm", "conversion to 12-hour clock is incorrect"); | ||
|
||
|
||
// a) Write an assertion to check the return value of formatAs12HourClock when it is called with an input "17:42" | ||
|
||
const result = formatAs12HourClock("17:42"); | ||
console.assert( | ||
result === "5:42 pm", | ||
"Conversion to 12-hour clock is incorrect" | ||
); | ||
|
||
|
||
|
||
// b) Check the assertion output and explain what the bug is | ||
The bug is in the formatAs12HourClock function. | ||
It seems that the minutes are not being considered, and the function always | ||
returns ":00 pm" or ":00 am". | ||
Additionally, it doesn't handle noon and midnight correctly. | ||
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 is an excellent summary of the bug. Best to comment it so that the code will continue to run even after you've added the explanation |
||
|
||
|
||
|
||
|
||
// c) Now fix the bug and re-run all your assertions | ||
|
||
function formatAs12HourClock(time) { | ||
const hours = Number(time.slice(0, 2)); | ||
const minutes = time.slice(3); | ||
const period = hours >= 12 ? "pm" : "am"; | ||
const formattedHours = hours % 12 || 12; // Handle 12:00 as 12 pm | ||
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 is a good little touch - I don't think many people clicked on to the bug with formatting 12 pm. However, defining a function with the same twice in one file will cause fatal errors, so it would be best to remove or comment out the original copy of the function before adding this copy - you're trying to make this file as a whole run. |
||
|
||
return `${formattedHours}:${minutes} ${period}`; | ||
} | ||
|
||
// Re-run assertions | ||
|
||
const currentOutput = formatAs12HourClock("08:00"); | ||
const targetOutput = "8:00 am"; | ||
console.assert( | ||
currentOutput === targetOutput, | ||
"current output: %s, target output: %s", | ||
currentOutput, | ||
targetOutput | ||
); | ||
|
||
const currentOutput2 = formatAs12HourClock("23:00"); | ||
const targetOutput2 = "11:00 pm"; | ||
console.assert( | ||
currentOutput2 === targetOutput2, | ||
"current output: %s, target output: %s", | ||
currentOutput2, | ||
targetOutput2 | ||
); | ||
|
||
const result = formatAs12HourClock("17:42"); | ||
console.assert( | ||
result === "5:42 pm", | ||
"Conversion to 12-hour clock is incorrect" | ||
); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,3 +21,61 @@ | |
// Identify Reflex Angles: | ||
// When the angle is greater than 180 degrees and less than 360 degrees, | ||
// Then the function should return "Reflex angle" | ||
// Function to get the type of angle | ||
function getAngleType(angle) { | ||
if (angle === 90) { | ||
return "right angel"; | ||
} else if (angle < 90) { | ||
return "Acute angle"; | ||
} else if (angle === 180) { | ||
return "straight angle"; | ||
} else if (angle > 90 && angle < 180) { | ||
return "obtuse angle"; | ||
} else if (angle > 180 && angle < 360) { | ||
return "Reflex angle"; | ||
} else { | ||
return "Invalid angle"; | ||
} | ||
} | ||
|
||
// Tests for each acceptance criterion | ||
|
||
// Test for Right Angle | ||
const rightAngleTest = getAngleType(90); | ||
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. Looking good! Good to see the addition of tests for every aspect of the requirements. |
||
console.assert( | ||
rightAngleTest === "Right angel", | ||
`Expected "Right angle" but got "${rightAngleTest}"` | ||
); | ||
|
||
// Test for Acute Angle | ||
const acuteAngleTest = getAngleType(60); | ||
console.assert( | ||
acuteAngleTest === "Acute angle", | ||
`Expected "Acute angle" but got "${acuteAngleTest}"` | ||
); | ||
|
||
|
||
// Test for Obtuse Angle | ||
const obtuseAngleTest = getAngleType(120); | ||
console.assert( | ||
obtuseAngleTest === "Obtuse angle", | ||
`Expected "Obtuse angle" but got "${obtuseAngleTest}"` | ||
); | ||
|
||
|
||
// Test for Straight Angle | ||
const straightAngleTest = getAngleType(180); | ||
console.assert( | ||
straightAngleTest === "straight angle", | ||
`Expected "Straight angle" but got "${straightAngleTest}"` | ||
); | ||
|
||
|
||
// Test for Reflex Angle | ||
const reflexAngleTest = getAngleType(270); | ||
console.assert( | ||
reflexAngleTest === "Reflex angle", | ||
`Expected "Reflex angle" but got "${reflexAngleTest}"` | ||
); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,3 +29,53 @@ | |
// 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 rank = card.slice(0, -1); | ||
} | ||
|
||
// Extracting the rank part of the card string | ||
if (/[2-9]|10/.test(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. This works however, just a comment on your use of regular expressions. Regex is a very powerful tool, but it's not particauluarly easy to understand at a glance, so I prefer to use the string method and matching against arrays when possible. This is a matter of your preferrences but I would recommend using regex sparingly |
||
// Handle Number Cards (2-10) | ||
return parseInt(rank, 10); | ||
} else if (/^[JQK]$/.test(rank)) { | ||
// Handle Face Cards (J, Q, K) | ||
return 10; | ||
} else if (rank === 'A') { | ||
// Handle Ace (A) | ||
return 11; | ||
} else { | ||
// Handle Invalid Cards | ||
throw new Error("Invalid card rank."); | ||
} | ||
|
||
|
||
// Assertions for each acceptance criterion | ||
|
||
// Test for Numeric Card | ||
const numericCardTest = getCardValue("5♠"); | ||
console.assert( | ||
numericCardTest === 5, | ||
`Expected 5 for numeric card, but got ${numericCardTest}` | ||
); | ||
|
||
// Test for Face Card | ||
const faceCardTest = getCardValue("Q♦"); | ||
console.assert( | ||
faceCardTest === 10, | ||
`Expected 10 for face card, but got ${faceCardTest}` | ||
); | ||
|
||
// Test for Ace | ||
const aceCardTest = getCardValue("A♥"); | ||
console.assert( | ||
aceCardTest === 11, | ||
`Expected 11 for Ace, but got ${aceCardTest}` | ||
); | ||
|
||
// Test for Invalid Card | ||
console.assert( | ||
() => getCardValue("X♣"), | ||
new Error("Invalid card 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. Good choice of tests, covering all the bases |
||
"Function did not throw expected error for invalid card" | ||
); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,3 +33,47 @@ | |
// 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 (denominator === 0) { | ||
throw new Error("Denominator cannot be zero"); | ||
} | ||
|
||
return 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 is a really simple and streamline solution to the problem, well done :) |
||
} | ||
|
||
// Assertions for each acceptance criterion | ||
|
||
// Test for Proper Fraction | ||
const properFractionTest = isProperFraction(2, 3); | ||
console.assert( | ||
properFractionTest === true, | ||
"Expected true for a proper fraction, but got false" | ||
); | ||
|
||
// Test for Improper Fraction | ||
const improperFractionTest = isProperFraction(5, 2); | ||
console.assert( | ||
improperFractionTest === false, | ||
"Expected false for an improper fraction, but got true" | ||
); | ||
|
||
// Test for Zero Denominator | ||
console.assert( | ||
() => isProperFraction(3, 0), | ||
new Error("Denominator cannot be zero"), | ||
"Function did not throw expected error for zero denominator" | ||
); | ||
|
||
// Test for Negative Fraction | ||
const negativeFractionTest = isProperFraction(-4, 7); | ||
console.assert( | ||
negativeFractionTest === true, | ||
"Expected true for a negative proper fraction, but got false" | ||
); | ||
|
||
// Test for Equal Numerator and Denominator | ||
const equalNumeratorDenominatorTest = isProperFraction(3, 3); | ||
console.assert( | ||
equalNumeratorDenominatorTest === false, | ||
"Expected false for equal numerator and denominator, but got true" | ||
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. 👍 |
||
); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,3 +38,42 @@ | |
// 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. | ||
function isValidTriangle(a, b, c) { | ||
if (a <= 0 || b <= 0 || c <= 0) { | ||
return false; | ||
// Check for zero or negative side lengths | ||
} | ||
|
||
return a + b > c && a + c > b && b + c > a; | ||
// Check the Triangle Inequality | ||
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. Looking good, apprecate the comments as well |
||
} | ||
|
||
// Assertions for each acceptance criterion | ||
|
||
// Test for Invalid Triangle (violates Triangle Inequality) | ||
const invalidTriangleTest = isValidTriangle(3, 3, 7); | ||
console.assert( | ||
invalidTriangleTest === false, | ||
"Expected false for an invalid triangle, but got true" | ||
); | ||
|
||
// Test for Invalid Triangle (negative side length) | ||
const negativeSideLengthTest = isValidTriangle(3, -4, 5); | ||
console.assert( | ||
negativeSideLengthTest === false, | ||
"Expected false for negative side length, but got true" | ||
); | ||
|
||
// Test for Invalid Triangle (zero side length) | ||
const zeroSideLengthTest = isValidTriangle(0, 4, 5); | ||
console.assert( | ||
zeroSideLengthTest === false, | ||
"Expected false for zero side length, but got true" | ||
); | ||
|
||
// Test for Valid Triangle | ||
const validTriangleTest = isValidTriangle(3, 4, 5); | ||
console.assert( | ||
validTriangleTest === true, | ||
"Expected true for a valid triangle, but got 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. 👍 |
||
); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,25 @@ | |
// Store this expression in a variable and reference it twice in the function in the correct place | ||
|
||
// Explain why it makes more sense to store this expression in a variable | ||
|
||
function formatAs12HourClock(time) { | ||
const hours = Number(time.slice(0, 2)); | ||
const minutes = time.slice(3); | ||
|
||
// Store the expression in a variable for better readability | ||
const formattedHours = hours % 12 || 12; | ||
const period = hours >= 12 ? "pm" : "am"; | ||
|
||
return `${formattedHours}:${minutes} ${period}`; | ||
} | ||
|
||
|
||
//By storing the result of Number(time.slice(0, 2)) in the hours variable, | ||
//we avoid recalculating it multiple times within the function. | ||
//This not only improves readability but also enhances performance by eliminating redundant computations. | ||
//Storing the result in a variable makes the code cleaner, more concise, and easier to maintain. | ||
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 good 👍 |
||
|
||
|
||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,3 +40,4 @@ console.assert( | |
currentOutput3, | ||
targetOutput3 | ||
); | ||
|
||
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 work in this file wasn't done - need to refactor isVowel |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,3 +40,28 @@ console.log(rotateCharacter("7", 5)); // Output: "7" (unchanged, not a letter) | |
// 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'). | ||
console.log(rotateCharacter("z", 1)); // Output: "a" (unchanged, not a letter) | ||
|
||
function rotateCharacter(char, shift) { | ||
// Check if the character is a lowercase letter | ||
if (char >= "a" && char <= "z") { | ||
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. Well done on attempting the strech goal here. It's looking really good:) |
||
const rotatedCharCode = ((char.charCodeAt(0) - "a".charCodeAt(0) + shift) % 26 + 26) % 26 + "a".charCodeAt(0); | ||
return String.fromCharCode(rotatedCharCode); | ||
} | ||
|
||
// Check if the character is an uppercase letter | ||
if (char >= "A" && char <= "Z") { | ||
const rotatedCharCode = ((char.charCodeAt(0) - "A".charCodeAt(0) + shift) % 26 + 26) % 26 + "A".charCodeAt(0); | ||
return String.fromCharCode(rotatedCharCode); | ||
} | ||
|
||
// If the character is not a letter, return it unchanged | ||
return char; | ||
} | ||
|
||
// Test cases | ||
console.log(rotateCharacter("a", 3)); // Output: "d" | ||
console.log(rotateCharacter("f", 1)); // Output: "g" | ||
console.log(rotateCharacter("A", 3)); // Output: "D" | ||
console.log(rotateCharacter("F", 1)); // Output: "G" | ||
console.log(rotateCharacter("7", 5)); // Output: "7" (unchanged, not a letter) | ||
console.log(rotateCharacter("z", 1)); // Output: "a" |
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.
👍