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

NW6 | Pedro Ricciardi | JS1 Module | [TECH ED] Complete week 3 exercises | Week 3 Backlog #156

Open
wants to merge 13 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
20 changes: 18 additions & 2 deletions 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`;
const hoursInNumber = Number(time.slice(0, 2));
if (hoursInNumber > 12) {
PERicci marked this conversation as resolved.
Show resolved Hide resolved
const hoursIn12hFormat = hoursInNumber - 12;
return `${hoursIn12hFormat.toString().padStart(2, "0")}:${time.slice(
PERicci marked this conversation as resolved.
Show resolved Hide resolved
-2
)} pm`;
}
return `${time} am`;
}
Expand All @@ -26,5 +30,17 @@ 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 = "05: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
// When the time is bigger than 12 the function ignores the minutes and return
// 00 hardcoded. When we try to solve, we need to use padStart to force two digits.
// But, padStart is a method for strings and doesn't work with numbers. So, we have to
// convert the number to a string to execute the padStart.
// c) Now fix the bug and re-run all your assertions
34 changes: 34 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,37 @@
// 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 >= 360 || angle <= 0) {
Copy link

Choose a reason for hiding this comment

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

360 itself is not an invalid angle

return "Invalid angle";
} else if (angle > 180) {
PERicci marked this conversation as resolved.
Show resolved Hide resolved
return "Reflex angle";
} else if (angle == 180) {
return "Straight angle";
} else if (angle > 90) {
return "Obtuse angle";
} else if (angle == 90) {
return "Right angle";
} else {
return "Acute angle";
}
}

function assertion(angle, targetOutput) {
const currentOutput = getAngleType(angle);
console.assert(
currentOutput === targetOutput,
`Test for ${angle}. Current output: %s, target output: %s`,
currentOutput,
targetOutput
);
}

assertion(390, "Invalid angle");
assertion(-20, "Invalid angle");
assertion(270, "Reflex angle");
assertion(180, "Straight angle");
assertion(100, "Obtuse angle");
assertion(90, "Right angle");
assertion(45, "Acute angle");
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(card) {
Copy link

Choose a reason for hiding this comment

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

clean solution

cardSuit = card.slice(-1);
cardRank = card.slice(0, -1);

if (
cardSuit == "♡" ||
cardSuit == "♢" ||
cardSuit == "♣" ||
cardSuit == "♠"
) {
switch (true) {
case cardRank == "A":
return 11;

case cardRank > 1 && cardRank < 11:
return 5;

case cardRank == "J" || cardRank == "Q" || cardRank == "K":
return 10;

default:
return `Invalid card rank.`;
}
} return "Invalid card suit. Please use one of the following: ♡,♢,♣,♠.";
}

function assertion(card, targetOutput) {
const currentOutput = getCardValue(card);
console.assert(
currentOutput === targetOutput,
`Test for ${card}. Current output: %s, target output: %s`,
currentOutput,
targetOutput
);
}

assertion("8♦", "Invalid card suit. Please use one of the following: ♡,♢,♣,♠.");
assertion("13♣", "Invalid card rank.");
assertion("A♠", 11);
assertion("4♢", 5);
assertion("10♡", 5);
assertion("Q♣", 10);
34 changes: 34 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,37 @@
// 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(fraction) {
const [numerator, denominator] = fraction.split("/");
const absoluteNumerator = Math.abs(numerator);
const absoluteDenominator = Math.abs(denominator);

if (isNaN(absoluteNumerator) || isNaN(absoluteDenominator)) {
return "Numerator or Denominator is not a number";
} else if (absoluteDenominator === 0) {
return "Denominator cannot be zero";
} else if (absoluteNumerator < absoluteDenominator) {
return true;
} else {
return false;
}
}

function assertion(fraction, targetOutput) {
const currentOutput = isProperFraction(fraction);
console.assert(
currentOutput === targetOutput,
`Test for ${fraction}. Current output: %s, target output: %s`,
currentOutput,
targetOutput
);
}

assertion("2f/0", "Numerator or Denominator is not a number");
PERicci marked this conversation as resolved.
Show resolved Hide resolved
assertion("2/0", "Denominator cannot be zero");
assertion("1/2", true);
assertion("-2/4", true);
assertion("-2/-4", true);
assertion("3/3", false);
assertion("4/3", false);
27 changes: 27 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,30 @@
// 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 (typeof(a + b + c) !== 'number') {
PERicci marked this conversation as resolved.
Show resolved Hide resolved
return false;
} else if (a <= 0 || b <= 0 || c <= 0) {
return false;
} else if (a > b + c || b > a + c || c > a + b) {
return false;
} else {
return true;
}
}

function assertion(a, b, c, targetOutput) {
const currentOutput = isValidTriangle(a, b, c);
console.assert(
currentOutput === targetOutput,
`Test for ${a}, ${b} and ${c}. Current output: %s, target output: %s`,
currentOutput,
targetOutput
);
}

assertion("1e", 3, 5, false);
assertion(3, 0, 5, false);
assertion(-2, 3, 4, false);
assertion(3, 4, 5, true);
28 changes: 28 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,31 @@
// 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
// Actually, I've already implemented this in the original format-as-12-hours.js file.
// For improved code readability and maintainability, it is a good approach to avoid duplicating the same code.
// If the same logic needs to be reused, it is a better practice to store the code in a variable and reference it when necessary.

function formatAs12HourClock(time) {
const hoursInNumber = Number(time.slice(0, 2));
if (hoursInNumber > 12) {
const hoursIn12hFormat = hoursInNumber - 12;
return `${hoursIn12hFormat.toString().padStart(2, "0")}:${time.slice(
-2
)} pm`;
}
return `${time} am`;
}

function assertion(hour, targetOutput) {
const currentOutput = formatAs12HourClock(hour);
console.assert(
currentOutput === targetOutput,
`Test for ${hour}. Current output: %s, target output: %s`,
currentOutput,
targetOutput
);
}

assertion("08:00", "08:00 am");
assertion("23:00", "11:00 pm");
assertion("17:42", "05:42 pm");
87 changes: 52 additions & 35 deletions week-3/refactor/is-vowel.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,59 @@
function isVowel(letter) {
return (
letter === "a" ||
letter === "e" ||
letter === "i" ||
letter === "i" ||
letter === "o" ||
letter === "u"
);
// Check if is a string and change the log text
if (typeof letter == "string") {
console.log(`Case: letter ${letter}...`);
} else {
console.log(`Case: ${letter}...`);
}

return ["a", "e", "i", "o", "u"].includes(letter);
}

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

console.log("case: letter a...");
const currentOutput = isVowel("a");
const targetOutput = true;
console.assert(
currentOutput === targetOutput,
"current output: %s, target output: %s",
currentOutput,
targetOutput
);
// Assertions replaced for better readability
function assertion(letter, targetOutput) {
const currentOutput = isVowel(letter);
console.assert(
currentOutput === targetOutput,
`Current output: %s, target output: %s`,
currentOutput,
targetOutput
);
}
assertion("a", true);
assertion("e", true);
assertion("k", false);
// Added check for non-string inputs
assertion(4, false);
assertion(false, false);

// console.log("case: letter a...");
// const currentOutput = isVowel("a");
// const targetOutput = true;
// console.assert(
// currentOutput === targetOutput,
// "current output: %s, target output: %s",
// currentOutput,
// targetOutput
// );

console.log("case: letter e...");
const currentOutput2 = isVowel("e");
const targetOutput2 = true;
console.assert(
currentOutput2 === targetOutput2,
"current output: %s, target output: %s",
currentOutput2,
targetOutput2
);
// console.log("case: letter e...");
// const currentOutput2 = isVowel("e");
// const targetOutput2 = true;
// console.assert(
// currentOutput2 === targetOutput2,
// "current output: %s, target output: %s",
// currentOutput2,
// targetOutput2
// );

console.log("case: letter k...");
const currentOutput3 = isVowel("k");
const targetOutput3 = false;
console.assert(
currentOutput3 === targetOutput3,
"current output: %s, target output: %s",
currentOutput3,
targetOutput3
);
// console.log("case: letter k...");
// const currentOutput3 = isVowel("k");
// const targetOutput3 = false;
// console.assert(
// currentOutput3 === targetOutput3,
// "current output: %s, target output: %s",
// currentOutput3,
// targetOutput3
// );
47 changes: 41 additions & 6 deletions week-3/stretch/rotate-char.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,41 @@
// 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 isLetterOrNumber(char) {
return /[a-zA-Z0-9]/.test(char); //check if input is a valid letter or number
}

function rotateCharacter(char, shift) {
if (
char.length === 1 &&
isLetterOrNumber(char) &&
typeof shift == "number" &&
shift >= 0
) {
if (
char.charCodeAt() > 47 &&
char.charCodeAt() < 58 /* check if input is a valid number */
) {
return char;
} else {
const isUpperCase = /[A-Z]/.test(char);
PERicci marked this conversation as resolved.
Show resolved Hide resolved
const charUnicode = char.toUpperCase().charCodeAt();
const realShift = shift >= 26 ? shift % 26 : shift;
const shiftedCharUnicode = charUnicode + realShift;
const shiftedChar =
shiftedCharUnicode > 90
? String.fromCharCode(shiftedCharUnicode - 26)
: String.fromCharCode(shiftedCharUnicode);

return isUpperCase
? shiftedChar.toUpperCase()
: shiftedChar.toLowerCase();
}
} else {
return "Please, insert a valid value. The character should be just 1 letter or 1 positive number, and the shift should be a positive number.";
}
}

// Acceptance criteria:

// Given a character (char) and a shift value (shift),
Expand All @@ -16,27 +51,27 @@
// Given a lowercase letter character (char) and a positive integer shift,
// When the function is called with these inputs,
// Then it should rotate the lowercase letter by shift positions within the lowercase alphabet, wrapping around if necessary, and return the rotated lowercase letter as a string.
console.log(rotateCharacter("a", 3)); // Output: "d"
console.log(rotateCharacter("f", 1)); // Output: "g"
console.log(rotateCharacter("a", 3), "expected d"); // Output: "d"
console.log(rotateCharacter("f", 1), "expected g"); // Output: "g"

// Scenario: Rotate Uppercase Letters:
// Given an uppercase letter character (char) and a positive integer shift,
// When the function is called with these inputs,
// Then it should rotate the uppercase letter by shift positions within the uppercase alphabet, wrapping around if necessary, and return the rotated uppercase letter as a string.
console.log(rotateCharacter("A", 3)); // Output: "D"
console.log(rotateCharacter("F", 1)); // Output: "G"
console.log(rotateCharacter("A", 3), "expected D"); // Output: "D"
console.log(rotateCharacter("F", 1), "expected G"); // Output: "G"

// Scenario: Leave Non-Letter Characters Unchanged:
// Given a character (char) that is not a letter (neither uppercase nor lowercase) and any positive or negative shift value,
// When the function is called with these inputs,
// Then it should return the character unchanged.
// This specification outlines the behavior of the rotateCharacter function for different input scenarios, including valid and invalid characters, and defines the expected output or action for each case.
console.log(rotateCharacter("7", 5)); // Output: "7" (unchanged, not a letter)
console.log(rotateCharacter("7", 5), "expected 7"); // Output: "7" (unchanged, not a letter)

// Scenario: Shifting a Character with Wraparound
// Given a character char within the lowercase alphabet range (e.g., 'z') or the uppercase alphabet range (e.g., 'Z'),
// And a positive integer shift that causes the character to wrap around the alphabet when rotated (e.g., a shift of 3 for 'z' or 'Z'),
// When the rotateCharacter function is called with char and shift as inputs,
// Then it should correctly rotate the character by shift positions within the alphabet while handling the wraparound,
// And the function should return the rotated character as a string (e.g., 'z' rotated by 3 should become 'c', 'Z' rotated by 3 should become 'C').
console.log(rotateCharacter("z", 1)); // Output: "a" (unchanged, not a letter)
console.log(rotateCharacter("z", 1), "expected a"); // Output: "a" (unchanged, not a letter)