Skip to content
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

NW-6 | AREEB-SATTAR | JS1| [TECH ED] Complete week 3 exercises | WEEK-3 #154

Open
wants to merge 6 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
16 changes: 15 additions & 1 deletion week-3/debug/format-as-12-hours.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
function formatAs12HourClock(time) {
if (Number(time.slice(0, 2)) > 12) {
return `${Number(time.slice(0, 2)) - 12}:00 pm`;
let timeFormat = (Number(time.slice(0, 2)) - 12)
if (timeFormat <10 )
Copy link
Member

Choose a reason for hiding this comment

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

Can you think of a way this can be done without an if statement? JavaScript's standard library has a great function for this use case 😉

return `0${timeFormat}:${time.slice(3, 5)} pm`;
else
return`${timeFormat}:${time.slice(3, 5)} pm`;
}
return `${time} am`;
}
Expand All @@ -23,8 +27,18 @@ console.assert(
targetOutput2
);

const currentOutput1 = formatAs12HourClock("17:42");
const targetOutput1 = "05:42 pm"
console.assert(
currentOutput1 === targetOutput1,
`current output: %s, target output: %s`, currentOutput1, targetOutput1
)

// formatAs12HourClock currently has a 🐛

// 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
// Ans. The problem is that the hours are being calculated fine but minutes are not being considered as we don't have a calculation for minutes
// c) Now fix the bug and re-run all your assertions
// Ans. Replaced 00 in line 3 with ${time.slice(3,5)} to return the minutes part back, and added another if statement to get the desired result
// from line 3 to line 8.
24 changes: 24 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,27 @@
// 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){
return "Reflex Angle"
}
else
Copy link

Choose a reason for hiding this comment

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

an angle of 360 itself is not taken care of

return "Angle cannot be greater than 360 or less than 0"
}

console.log(getAngleType(90));

43 changes: 43 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,46 @@
// 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(str) {
Copy link
Member

Choose a reason for hiding this comment

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

Be careful of the acceptance criteria on line 9. str might be a value such as A♠, which contains both the card suit and value

if (str === "A") {
return 11;
} else if (str === "J" || str === "Q" || str === "K") {
return 10;
} else if (str > 1 && str < 11) {
return +str;
} else return "Invalid card rank";
}
// console.log( typeof getCardValue("2"));

console.log("Checking result for 2");
const currentOutput = getCardValue("2");
const targetOutput = 2;
console.assert(
currentOutput === targetOutput,
`current output: ${currentOutput}, target output: ${targetOutput} `
);

console.log("Checking result for 5");
const currentOutput1 = getCardValue("5");
const targetOutput1 = 5;
console.assert(
currentOutput1 === targetOutput1,
`current output: ${currentOutput1}, target output: ${targetOutput1} `
);

console.log("Checking result for K");
const currentOutput2 = getCardValue("K");
const targetOutput2 = 10; // This will be true for "10", "J", "K" and "Q"
console.assert(
currentOutput2 === targetOutput2,
`current output: ${currentOutput2}, target output: ${targetOutput2} `
);

console.log("Checking result for A");
const currentOutput3 = getCardValue("A");
const targetOutput3 = 11;
console.assert(
currentOutput3 === targetOutput3,
`current output: ${currentOutput3}, target output: ${targetOutput3} `
);
38 changes: 38 additions & 0 deletions week-3/implement/is-proper-fraction.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// You wil need to implement a function isProperFraction
// You need to write assertions for your function to check it works in different cases

const { log } = require("console");

// Terms:
// Fractions: https://www.bbc.co.uk/bitesize/topics/zt9n6g8/articles/zjxpp4j
// Written here like this: 1/2 == Numerator/Denominator
Expand Down Expand Up @@ -33,3 +35,39 @@
// 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(a, b) {
Copy link

Choose a reason for hiding this comment

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

what do you think will happen when we pass 3/-5 into your code?

if (b === 0) {
return "error";
}
if (a < b) {
return true;
}
if (a > b) {
return false;
}
if (a < 0) {
return true;
}
if (a === b) {
return false;
}
}

console.log(isProperFraction(3, 3));

console.log("This Assertion is fails when it's not a proper fraction");
const currentOutput = isProperFraction(8, 9);
const targetOutput = true;
console.assert(
currentOutput === targetOutput,
`current output: ${currentOutput}, target output: ${targetOutput} `
);

console.log("This Assertion is fails when it's a proper fraction");
const currentOutput1 = isProperFraction(8, 4);
const targetOutput1 = false;
console.assert(
currentOutput1 === targetOutput1,
`current output: ${currentOutput1}, target output: ${targetOutput1} `
);
14 changes: 14 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,17 @@
// 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+b<=c || a+c<=b || b+c<=a){
return false;
}
else if (a<=0 || b<=0 || c<=0){
return false;
}
else if(a+b>c && a+c>b && b+c>a){
Copy link
Member

Choose a reason for hiding this comment

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

This logic here is the opposite of the logic on line 43. For example, a + b <= c must be false for us to reach this line of code. Therefore a + b > c will always be true at line 49. The same goes for the other statements.

For that reason the checks here on line 49 aren't necessary

return true;
}
}

console.log(isValidTriangle(3,3,3));
37 changes: 37 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,40 @@
// 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
// I already stored it in a variable in first problem because we are using same thing twice which can make code look
// confusing and by storing it in variable it makes things easier for someone unfamiliar with code to understand.
function formatAs12HourClock(time) {
if (Number(time.slice(0, 2)) > 12) {
Copy link
Member

@JayMayer JayMayer Dec 1, 2023

Choose a reason for hiding this comment

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

I think we can take this further. What if we moved the expression Number(time.slice(0, 2)) into a variable after line 9, then use that variable in the if statement and line 11. By also giving the variable a name such as hours, it would be easier to glance at the code and tell what is happening in the logic.

let timeFormat = Number(time.slice(0, 2)) - 12;
if (timeFormat < 10) return `0${timeFormat}:${time.slice(3, 5)} pm`;
else return `${timeFormat}:${time.slice(3, 5)} 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
);

const currentOutput1 = formatAs12HourClock("17:42");
const targetOutput1 = "05:42 pm";
console.assert(
currentOutput1 === targetOutput1,
`current output: %s, target output: %s`,
currentOutput1,
targetOutput1
);
13 changes: 11 additions & 2 deletions week-3/refactor/is-vowel.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ function isVowel(letter) {
return (
letter === "a" ||
letter === "e" ||
letter === "i" ||
letter === "i" ||
letter === "i" ||// removed same line twice in the function here
letter === "o" ||
letter === "u"
);
Expand Down Expand Up @@ -40,3 +39,13 @@ console.assert(
currentOutput3,
targetOutput3
);

console.log("case: letter i...");
const currentOutput4 = isVowel("i");
const targetOutput4 = true;
console.assert(
currentOutput4 === targetOutput4,
"current output: %s, target output: %s",
currentOutput4,
targetOutput4
);