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
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Completed Refactor and improved implement
areebsattar committed Nov 19, 2023
commit d6ba89ac03d4e7aa17a59db35d223117a5b70394
30 changes: 20 additions & 10 deletions week-3/implement/get-card-value.js
Original file line number Diff line number Diff line change
@@ -41,24 +41,34 @@ function getCardValue(str) {
}
// console.log( typeof getCardValue("2"));

const currentOutput = getCardValue("A"); //Change the value here to check against different assertions
console.log("Checking result for 2");
const currentOutput = getCardValue("2");
const targetOutput = 2;
const targetOutput1 = 5;
const targetOutput2 = 10; // This will be true for "10", "J", "K" and "Q"
const targetOutput3 = 11;
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(
currentOutput === targetOutput1,
`current output: ${currentOutput}, target output: ${targetOutput1} `
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(
currentOutput === targetOutput2,
`current output: ${currentOutput}, target output: ${targetOutput2} `
currentOutput2 === targetOutput2,
`current output: ${currentOutput2}, target output: ${targetOutput2} `
);

console.log("Checking result for A");
const currentOutput3 = getCardValue("A");
const targetOutput3 = 11;
console.assert(
currentOutput === targetOutput3,
`current output: ${currentOutput}, target output: ${targetOutput3} `
currentOutput3 === targetOutput3,
`current output: ${currentOutput3}, target output: ${targetOutput3} `
);
17 changes: 10 additions & 7 deletions week-3/implement/is-proper-fraction.js
Original file line number Diff line number Diff line change
@@ -51,17 +51,20 @@ function isProperFraction (a,b){
return false;
}
}
const currentOutput = isProperFraction(8,0); //Change the values here to check against different assertions

console.log("This Assertion is fails when it's not a proper fraction");
const currentOutput = isProperFraction(8,9);
const targetOutput = true;
const targetOutput1 = false;
console.assert(
currentOutput === targetOutput,
`This Assertion is fails when it's not a proper fraction
current output: ${currentOutput}, target output: ${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(
currentOutput === targetOutput1,
`This Assertion is fails when it's a proper fraction
current output: ${currentOutput}, target output: ${targetOutput1} `
currentOutput1 === targetOutput1,
`current output: ${currentOutput1}, target output: ${targetOutput1} `
);

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

Choose a reason for hiding this comment

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

Fantastic! Adding a test specifically the code being changed is a great way to make sure it still works after the refactoring!

Original file line number Diff line number Diff line change
@@ -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"
);
@@ -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
);