Skip to content
This repository was archived by the owner on Apr 18, 2025. It is now read-only.

NW6 | Pedram Amani | Module-JS1 | Week3 #186

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 65 additions & 3 deletions week-3/debug/format-as-12-hours.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
// function formatAs12HourClock(time) {
// if (Number(time.slice(0, 2)) > 12) {
// return `${Number(time.slice(0, 2)) - 12}:${Number(time.slice(3))} pm`;
// }
// return `${time} am`;
// }

function formatAs12HourClock(time) {
if (Number(time.slice(0, 2)) > 12) {
return `${Number(time.slice(0, 2)) - 12}:00 pm`;
const hours = Number(time.slice(0, 2));
const minutes = Number(time.slice(3));

if (hours >= 12) {
const formattedHoursAfternoon =
hours === 12 ? hours : hours - 12 < 10 ? `0${hours - 12}` : hours - 12;
const formattedMinutesAfternoon = minutes < 10 ? `0${minutes}` : minutes;
return `${formattedHoursAfternoon}:${formattedMinutesAfternoon} pm`;
}
return `${time} am`;

const formattedHoursMorning =
hours === 0 ? hours : hours < 10 ? `0${hours}` : hours;
const formattedMinutesMorning = minutes < 10 ? `0${minutes}` : minutes;
return `${formattedHoursMorning}:${formattedMinutesMorning} am`;
}

const currentOutput = formatAs12HourClock("08:00");
Expand All @@ -28,3 +45,48 @@ console.assert(
// a) Write an assertion to check the return value of formatAs12HourClock when it is called with an input "17:42"
// b) Check the assertion output and explain what the bug is
// c) Now fix the bug and re-run all your assertions

const currentOutput3 = formatAs12HourClock("17:42");
const targetOutput3 = "05:42 pm";

console.assert(
currentOutput3 === targetOutput3,
"current output: %s, target output: %s",
currentOutput3,
targetOutput3
);

// Answer:

// b) When the hours part are greater than 12, the function adds "00" to show the minutes part which is not correct.
// Also the function does not appropriately work to display the four digit number formatting

const currentOutput4 = formatAs12HourClock("00:00");
const targetOutput4 = "00:00 am";

console.assert(
currentOutput3 === targetOutput3,
"current output: %s, target output: %s",
currentOutput3,
targetOutput3
);

const currentOutput5 = formatAs12HourClock("01:02");
const targetOutput5 = "01:02 am";

console.assert(
currentOutput3 === targetOutput3,
"current output: %s, target output: %s",
currentOutput3,
targetOutput3
);

const currentOutput6 = formatAs12HourClock("23:23");
const targetOutput6 = "11:23 pm";

console.assert(
currentOutput3 === targetOutput3,
"current output: %s, target output: %s",
currentOutput3,
targetOutput3
);
23 changes: 23 additions & 0 deletions week-3/implement/get-angle-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link

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 ?

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) {
Copy link
Contributor

Choose a reason for hiding this comment

The 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");
21 changes: 21 additions & 0 deletions week-3/implement/get-card-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,24 @@
// 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);

if (Number(rank) >= 2 && Number(rank) <= 10) {
return Number(rank);
} else if (rank === "J" || rank === "Q" || rank === "K") {
return 10;
} else if (rank === "A") {
return 11;
} else {
return "Invalid card rank";
}
}

console.assert(getCardValue("5♠") === 5, "Test 1");
console.assert(getCardValue("J♦") === 10, "Test 2");
console.assert(getCardValue("A♥") === 11, "Test 3");
console.assert(getCardValue("1♣") === "Invalid card rank", "Test 4");
console.assert(getCardValue("11♣") === "Invalid card rank", "Test 5");
console.assert(getCardValue("M♣") === "Invalid card rank", "Test 6");
25 changes: 25 additions & 0 deletions week-3/implement/is-proper-fraction.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link

Choose a reason for hiding this comment

The 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));
25 changes: 25 additions & 0 deletions week-3/implement/is-valid-triangle.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good implementation with multiple examples

58 changes: 58 additions & 0 deletions week-3/refactor/format-as-12-hours.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Copy link

Choose a reason for hiding this comment

The 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. formattedHoursAfternoon and formattedHoursMorning have a couple of common code, can you think of a way to extract that into a reusable function?

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.
37 changes: 33 additions & 4 deletions week-3/refactor/is-vowel.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,24 @@ function isVowel(letter) {
letter === "a" ||
letter === "e" ||
letter === "i" ||
letter === "i" ||
letter === "o" ||
letter === "u"
);
}

// here is an implementation of isVowel - this function checks if a letter is a vowel

console.log("case: letter a...");
let letter = "a";

console.log(
"It is " +
(isVowel(letter) ? "true" : "false") +
" that letter '" +
letter +
"' is a vowel."
);

// console.log("case: letter a...");
const currentOutput = isVowel("a");
const targetOutput = true;
console.assert(
Expand All @@ -21,7 +30,17 @@ console.assert(
targetOutput
);

console.log("case: letter e...");
letter = "e";

console.log(
"It is " +
(isVowel(letter) ? "true" : "false") +
" that letter '" +
letter +
"' is a vowel."
);

// console.log("case: letter e...");
const currentOutput2 = isVowel("e");
const targetOutput2 = true;
console.assert(
Expand All @@ -31,7 +50,17 @@ console.assert(
targetOutput2
);

console.log("case: letter k...");
letter = "k";

console.log(
"It is " +
(isVowel(letter) ? "true" : "false") +
" that letter '" +
letter +
"' is a vowel."
);

// console.log("case: letter k...");
const currentOutput3 = isVowel("k");
const targetOutput3 = false;
console.assert(
Expand Down