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

MIGRACODE-JUL 2024 | PRISCILLA MAC-GATUS | MODULE JS1 | WEEK 3 #59

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
23 changes: 22 additions & 1 deletion week-3/debug/format-as-12-hours.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
function formatAs12HourClock(time) {
if (Number(time.slice(0, 2)) > 12) {
return `${Number(time.slice(0, 2)) - 12}:00 pm`;
let hours = Number(time.slice(0, 2)) - 12;
let minutes = Number(time.slice(3, 5));
minutes = minutes.toString().padStart(2, "0");
return `${hours}:${minutes} pm`;
}
return `${time} am`;
}
Expand All @@ -26,5 +29,23 @@ console.assert(
// formatAs12HourClock currently has a 🐛

// a) Write an assertion to check the return value of formatAs12HourClock when it is called with an input "17:42"

const currentOutput3 = formatAs12HourClock("17:42");
const targetOutput3 = "5:42 pm";
console.assert(
currentOutput3 === targetOutput3,
"current output: %s, target output: %s",
currentOutput3,
targetOutput3
);

// b) Check the assertion output and explain what the bug is

/*Assertion failed: current output: 5:00 pm, target output: 5:42 pm.This because the
equality sign is a strict equality and the value on both sides are not equal.
current output doesn't display the minutes shown in the target output.
Instead of 5:42 pm it displays 5:00pm.

*/

// c) Now fix the bug and re-run all your assertions
20 changes: 20 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,23 @@
// 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";
}
}

console.log(`This is an/a ${getAngleType(90)}.`);
console.log(`This is an/a ${getAngleType(45)}.`);
console.log(`This is an/a ${getAngleType(120)}.`);
console.log(`This is an/a ${getAngleType(180)}.`);
console.log(`This is an/a ${getAngleType(240)}.`);
20 changes: 20 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,23 @@
// 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(cardGiven) {
if (cardGiven === "A♠") {
return 11;
} else if (cardGiven > 2 && cardGiven < 10) {
return Number(cardGiven);
} else if (cardGiven === "J" || cardGiven === "Q" || cardGiven === "K") {
return 10;
} else if (cardGiven === "Ace" || cardGiven === "A") {
return 11;
} else {
return "Invalid card rank.";
}
}

console.assert(getCardValue("A♠"));
console.assert(getCardValue(6));
console.assert(getCardValue("Q"));
console.assert(getCardValue("Ace"));
console.assert(getCardValue("W"));
31 changes: 31 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,34 @@
// 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 (numerator < denominator && denominator !== 0) {
return "This is true";
} else if (numerator > denominator && denominator !== 0) {
return "This is false";
} else if (denominator === 0) {
return "Error!!, Denominator cannot be zero";
} else if (denominator > Math.abs(numerator)) {
return "This is true";
} else if (numerator === denominator) {
return "This is false";
} else {
return "Error!!";
}
}

// Proper Fraction check:
console.assert(isProperFraction(2, 6));

//Improper Fraction check:
console.assert(isProperFraction(9, 6));

// Zero Denominator check:
console.assert(isProperFraction(5, 0));

// Negative Fraction check:
console.assert(isProperFraction(-9, 9));

// Equal Numerator and Denominator check:
console.assert(isProperFraction(5, 5));
51 changes: 51 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,54 @@
// 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.

//--------------------SOLUTION-------------------------------------------
//where a,b,c denotes the length of each side of a triangle.

//false if sum of 2 sides is less than 1 side
//true if sum of 2 sides is greater than 1 side
//false if any side is <= 0

function isValidTriangle(a, b, c) {
if (a + b > c && b + c > a && a + c > b) {
return true;
} else if (a <= 0 || b <= 0 || c <= 0) {
return false;
} else if (a + b <= c || b + c <= a || a + c <= b) {
return false;
} else {
return "Error";
}
}

// sum of 2 sides is less than 1 side

const currentOutput = isValidTriangle(2, 3, 6);
const targetOutput = false;
console.assert(currentOutput === targetOutput, currentOutput, targetOutput);

// sum of 2 sides is greater than 1 side
const currentOutput1 = isValidTriangle(4, 5, 2);
const targetOutput1 = true;
console.assert(currentOutput1 === targetOutput1, currentOutput1, targetOutput1);

// if any side is <= 0
const currentOutput2 = isValidTriangle(0, 3, 3);
const targetOutput2 = false;
console.assert(
currentOutput2 === targetOutput2,

currentOutput2,
targetOutput2
);

//if any side is <= 0

const currentOutput3 = isValidTriangle(-1, 4, 7);
const targetOutput3 = false;
console.assert(
currentOutput3 === targetOutput3,

currentOutput3,
targetOutput3
);
41 changes: 41 additions & 0 deletions week-3/refactor/format-as-12-hours.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,45 @@
// That implementation currently uses the expression Number(time.slice(0, 2)) twice
// Store this expression in a variable and reference it twice in the function in the correct place

function formatAs12HourClock(time) {
const slicedHourConvertedToNumber = Number(time.slice(0, 2));

if (slicedHourConvertedToNumber > 12) {
let hours = slicedHourConvertedToNumber - 12;
let minutes = Number(time.slice(3, 5));
minutes = minutes.toString().padStart(2, "0");
return `${hours}:${minutes} 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 currentOutput3 = formatAs12HourClock("17:42");
const targetOutput3 = "5:42 pm";
console.assert(
currentOutput3 === targetOutput3,
"current output: %s, target output: %s",
currentOutput3,
targetOutput3
);

// Explain why it makes more sense to store this expression in a variable

// We tend to write less code than we should and just like function we can call the variable as many times as we want to within the function.
7 changes: 3 additions & 4 deletions week-3/refactor/is-vowel.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ 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...");
console.log(`case: letter a is ${isVowel("a")}`);
const currentOutput = isVowel("a");
const targetOutput = true;
console.assert(
Expand All @@ -21,7 +20,7 @@ console.assert(
targetOutput
);

console.log("case: letter e...");
console.log(`case: letter e is ${isVowel("e")}`);
const currentOutput2 = isVowel("e");
const targetOutput2 = true;
console.assert(
Expand All @@ -31,7 +30,7 @@ console.assert(
targetOutput2
);

console.log("case: letter k...");
console.log(`case: letter k is ${isVowel("k")}`);
const currentOutput3 = isVowel("k");
const targetOutput3 = false;
console.assert(
Expand Down
17 changes: 17 additions & 0 deletions week-3/stretch/rotate-char.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,23 @@
// This function is commonly used for weak text encryption and decryption,
// where shifting characters by a certain value can obscure their meaning or reveal hidden messages.

function rotateCharacter(character, shift) {
const alphabetUpper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const alphabetLower = "abcdefghijklmnopqrstuvwxyz";

if (alphabetUpper.includes(character)) {
const position = alphabetUpper.indexOf(character);
const newPosition = (position + shift) % 26;
return alphabetUpper[newPosition];
} else if (alphabetLower.includes(character)) {
const position = alphabetLower.indexOf(character);
const newPosition = (position + shift) % 26;
return alphabetLower[newPosition];
}

return character;
}

// Acceptance criteria:

// Given a character (char) and a shift value (shift),
Expand Down