-
-
Notifications
You must be signed in to change notification settings - Fork 50
NW6 | Pedram Amani | Module-JS1 | Week3 #186
base: main
Are you sure you want to change the base?
Changes from all commits
44431a0
8a72ab8
bfe90f7
92fb2f8
64804a6
c018832
ff2c999
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 |
---|---|---|
|
@@ -21,3 +21,26 @@ | |
// 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) { | ||
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) { | ||
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 have learned that we can use if statements again and again rather that else if, I have had quite a lot of issues using else if in few code blocks |
||
return "Reflex angle"; | ||
} else { | ||
return "Invalid angle"; | ||
} | ||
} | ||
|
||
console.assert(getAngleType(90) === "Right angle"); | ||
console.assert(getAngleType(45) === "Acute angle"); | ||
console.assert(getAngleType(120) === "Obtuse angle"); | ||
console.assert(getAngleType(180) === "Straight angle"); | ||
console.assert(getAngleType(270) === "Reflex angle"); | ||
console.assert(getAngleType(360) === "Invalid angle"); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,3 +33,28 @@ | |
// 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) { | ||
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 well done. |
||
if (denominator === 0) { | ||
throw new Error("Denominator cannot be zero"); | ||
} else { | ||
if (Math.abs(numerator) < Math.abs(denominator)) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
console.assert(isProperFraction(2, 3) === true, "Test 1"); | ||
console.assert(isProperFraction(5, 2) === false, "Test 2"); | ||
console.assert(isProperFraction(-4, 7) === true, "Test 3"); | ||
console.assert(isProperFraction(3, 3) === false, "Test 4"); | ||
console.assert(() => isProperFraction(3, 0), "Test 5"); | ||
|
||
console.log(isProperFraction(-3, 1)); | ||
console.log(isProperFraction(3, 11)); | ||
console.log(isProperFraction(1, 1)); | ||
console.log(isProperFraction(2, -21)); | ||
console.log(isProperFraction(-31, -1)); | ||
console.log(isProperFraction(-3, 0)); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,3 +38,28 @@ | |
// 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(sideA, sideB, sideC) { | ||
if ( | ||
typeof sideA !== `number` || | ||
typeof sideB !== `number` || | ||
typeof sideC !== `number` || | ||
sideA <= 0 || | ||
sideB <= 0 || | ||
sideC <= 0 || | ||
sideA + sideB <= sideC || | ||
sideA + sideC <= sideB || | ||
sideB + sideC <= sideA | ||
) { | ||
return false; | ||
} else { | ||
return true; | ||
} | ||
} | ||
|
||
console.log(isValidTriangle(3, 3, 3)); | ||
console.log(isValidTriangle(3, 3, 5)); | ||
console.log(isValidTriangle(3, 4, 5)); | ||
console.log(isValidTriangle(1, 1, 3)); | ||
console.log(isValidTriangle(-2, 2, 5)); | ||
console.log(isValidTriangle(7, "apple", 5)); | ||
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 implementation with multiple examples |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,61 @@ | |
// 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 | ||
|
||
// Given code in the file in debug folder: | ||
|
||
// function formatAs12HourClock(time) { | ||
// if (Number(time.slice(0, 2)) > 12) { | ||
// return `${Number(time.slice(0, 2)) - 12}:00 pm`; | ||
// } | ||
// return `${time} am`; | ||
// } | ||
|
||
// const currentOutput = formatAs12HourClock("08:00"); | ||
// const targetOutput = "08: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 | ||
// ); | ||
|
||
// Improved and fixed version of it: | ||
|
||
function formatAs12HourClock(time) { | ||
const hours = Number(time.slice(0, 2)); | ||
const minutes = Number(time.slice(3)); | ||
|
||
if (hours >= 12) { | ||
const formattedHoursAfternoon = | ||
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 is a concept called Do not Repeat Yourself (DRY) it is a concept where you write a code once and you reuse it, instead of rewritten the same code again, you can read more about. |
||
hours === 12 ? hours : hours - 12 < 10 ? `0${hours - 12}` : hours - 12; | ||
const formattedMinutesAfternoon = minutes < 10 ? `0${minutes}` : minutes; | ||
return `${formattedHoursAfternoon}:${formattedMinutesAfternoon} pm`; | ||
} else { | ||
const formattedHoursMorning = | ||
hours === 0 ? hours : hours < 10 ? `0${hours}` : hours; | ||
const formattedMinutesMorning = minutes < 10 ? `0${minutes}` : minutes; | ||
return `${formattedHoursMorning}:${formattedMinutesMorning} am`; | ||
} | ||
} | ||
|
||
// Explanation: | ||
|
||
// * USE OF VARIABLES: The introduction of hours and minutes variables makes the code more readable, | ||
// and the variable names clearly convey their purpose. | ||
|
||
// * TERNARY OPERATOR: The use of ternary operators for conditional expressions, | ||
// simplifies the logic and reduces the need for nested if statements. | ||
|
||
// * CONSISTENT FORMATTING: The consistent formatting of hours and minutes with leading zeros ensures that the output follows a standard format. | ||
|
||
// * CLARITY IN LOGIC: The logic for handling afternoon and morning hours is clear and concise. |
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.
what happens when the angle passed is less than 0 ?