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

Karla Grajales | Sprint-3 - Module-Structuring-and-Testing-Data | SPRINT 3 #238

Open
wants to merge 32 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
d97f55b
initializing the function to identify angles
Grajales-K Dec 23, 2024
17e5647
removed function
Grajales-K Dec 23, 2024
3d51b68
installed npm dependencies to test with Jest
Grajales-K Dec 26, 2024
6a0595d
added console.assert() test for get-angle-type.js
Grajales-K Dec 26, 2024
ae24499
implemented test with jest and update name of the file as get-angle-t…
Grajales-K Dec 26, 2024
ca012a7
chabged the script to jest
Grajales-K Dec 26, 2024
4a61d73
adding conditon to handle differents options
Grajales-K Dec 27, 2024
121696e
changed to more easy function with if statements
Grajales-K Dec 28, 2024
48e6a8c
added the console.assert test
Grajales-K Dec 28, 2024
3ba6e74
isProperFraction easy estructured
Grajales-K Dec 28, 2024
6de4e81
added isValidTriangle fuction
Grajales-K Dec 28, 2024
1d07973
rotateCharacter function done
Grajales-K Dec 28, 2024
8303039
console.log test done
Grajales-K Dec 28, 2024
777d332
rotate-char.js update lines spaces
Grajales-K Dec 29, 2024
89fcaf5
Added comment to explain how the code runs
Grajales-K Dec 29, 2024
a733352
added function countChar
Grajales-K Dec 30, 2024
ebd07d2
is-valid-triangle.js updated
Grajales-K Dec 30, 2024
05b00c9
added password.js file and test cases
Grajales-K Dec 30, 2024
801006e
added the function repeatString
Grajales-K Dec 30, 2024
d04bbba
added some comments in the function
Grajales-K Dec 31, 2024
9c9fc1a
added isPrime function
Grajales-K Dec 31, 2024
ce6c536
added test cases for isPrime fuction
Grajales-K Dec 31, 2024
3e9b02e
added repeat fuction and comments of how code is working
Grajales-K Dec 31, 2024
ad68622
updated function name from ordinal.js to get-ordinal-number.js
Grajales-K Dec 31, 2024
08136b1
added comments to the function getOrdinalNumber
Grajales-K Dec 31, 2024
3ba56dc
validate and handle cases with 0 and decimal numbers and letter no in…
Grajales-K Jan 16, 2025
bf0e4d8
validate negative numbers in the function
Grajales-K Jan 16, 2025
2396b83
validate in one line the triangle inequality
Grajales-K Jan 16, 2025
d06f31f
validate the short password in the test
Grajales-K Jan 16, 2025
dad6802
fixed commentarios and funcionality in thes and functions
Grajales-K Jan 16, 2025
dfdc1bf
update comments
Grajales-K Jan 16, 2025
180ac5d
added console.assert test
Grajales-K Jan 16, 2025
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
6 changes: 0 additions & 6 deletions Sprint-2/errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,3 @@
// Predict and explain first...

// this function should square any number but instead we're going to get an error

function square(3) {
return num * num;
}


27 changes: 0 additions & 27 deletions Sprint-3/implement/get-angle-type.js

This file was deleted.

113 changes: 113 additions & 0 deletions Sprint-3/implement/get-angle-type.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
// Implement a function getAngleType

// Acceptance criteria:

// Given an angle in degrees,
// When the function getAngleType is called with this angle,
// Then it should:

// 1. Identify Right Angles:
// When the angle is exactly 90 degrees,
// Then the function should return "Right angle"

// 2. Identify Acute Angles:
// When the angle is less than 90 degrees",
// Then the function should return "Acute angle"

// 3. Identify Obtuse Angles:
// When the angle is greater than 90 degrees and less than 180 degrees,
// Then the function should return "Obtuse angle"

// 4. Identify Straight Angles:
// When the angle is exactly 180 degrees,
// Then the function should return "Straight angle"

// 5. 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 ===============================
//the use of else if is to compare one of the conditions

function getAngleType(angle){

if (angle === 90) {
return "Right angle";
} else if (angle > 0 && 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";
}
return "Invalid angle";
}


// console.log(getAngleType(90)); // "Right angle"
// console.log(getAngleType(45)); // "Acute angle"
// console.log(getAngleType(135)); // "Obtuse angle"
// console.log(getAngleType(180)); // "Straight angle"
// console.log(getAngleType(270)); // "Reflex angle"
// console.log(getAngleType(-30)); // "Invalid angle"



// ============== test with console.assert() ==================
//these test are more simple to write and read and only return the fail message
// console.assert(expression, message);

console.assert(getAngleType(99) === "Right angle", "Test case failed for Right Angle"); // fail AssertionError [ERR_ASSERTION]: Test case failed for Right Angle
console.assert(getAngleType(40) === "Acute angle", "Test case failed for Acute Angle");
console.assert(getAngleType(135) === "Obtuse Angle", "test case failed for Obtuse Angle");
console.assert(getAngleType(380) === "Straight angle", "Test failed for Straight angle"); // fail AssertionError [ERR_ASSERTION]: Test failed for Straight angle
console.assert(getAngleType(270) === "Reflex Angle", "Test case Failed for Reflex Angle");
console.assert(getAngleType(-10) === "Invalid angle", "Test case Failed for Invalid angle");
console.assert(getAngleType(400) === "Invalid angle", "Test case Failed for Invalid Angle");



// ============== test with Jest ==================
// These tests are more detailed because we can see what was expected and what went wrong
test('Should return "Right angle" for 90 degrees', () => {
const result = getAngleType(90);
expect(result).toBe("Right angle");
});

test('Should return "Acute angle" for angles between 0 and 90', () => {
const result = getAngleType(35);
expect(result).toBe("Acute angle");
});

test('Should return "Obtuse Angle" for angles between 90 and 180', () => {
const result = getAngleType(135);
expect(result).toBe("Obtuse Angle");
});

test('Should return "Straight angle" for 180 degrees', () => {
const result = getAngleType(180);
expect(result).toBe("Straight angle");
});

test('Should return "Reflex Angle" for angles between 180 and 360', () => {
const result = getAngleType(270);
expect(result).toBe("Reflex Angle");
});

test('Should return "Invalid angle" for angles outside 0-360 degrees', () => {
expect(getAngleType(-10)).toBe("Invalid angle");
expect(getAngleType(400)).toBe("Invalid angle");
});





//nodemon
// to run these test and see the changes applied in the same time use < nodemon -x "npm test" get-angle-type.test.js >

// npm
//once you are inside of the file you want to run < npm test -- --watch >
112 changes: 105 additions & 7 deletions Sprint-3/implement/get-card-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,124 @@

// Acceptance criteria:

// Given a card string in the format "A♠" (representing a card in blackjack - the last character will always be an emoji for a suit, and all characters before will be a number 2-10, or one letter of J, Q, K, A),
// 1. Given a card string in the format "A♠" (representing a card in blackjack -
// the last character will always be an emoji for a suit, and all characters
// before will be a number 2-10, or one letter of J, Q, K, A),
// When the function getCardValue is called with this card string as input,
// Then it should return the numerical card value

// Handle Number Cards (2-10):
// 2. Handle Number Cards (2-10):
// Given a card with a rank between "2" and "9",
// When the function is called with such a card,
// Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5).
// Then it should return the numeric value corresponding to the rank
// (e.g., "5" should return 5).

// Handle Face Cards (J, Q, K):
// 3. Handle Face Cards (J, Q, K):
// Given a card with a rank of "10," "J," "Q," or "K",
// When the function is called with such a card,
// Then it should return the value 10, as these cards are worth 10 points each in blackjack.
// Then it should return the value 10, as these cards are worth 10 points each
// in blackjack.

// Handle Ace (A):
// 4. Handle Ace (A):
// Given a card with a rank of "A",
// When the function is called with an Ace,
// Then it should, by default, assume the Ace is worth 11 points, which is a common rule in blackjack.

// Handle Invalid Cards:
// 5. Handle Invalid Cards:
// 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."

// ========================= getCardValue ===========================

// function getCardValue(input) {
// const rank = input.slice(0, -1);
// const suit = input.slice(-1);


// if (input === "10"){
// return 10;
// }else if (rank > 1 && rank < 11) {
// return Number(rank);
// }
// else if (input === "A")
// return 11;
// else if (input.toUpperCase() === "K" || input.toUpperCase() === "Q" || input.toUpperCase() === "J"){
// return 10;
// }

// if (suit !== "♠" && suit !== "♥" && suit !== "♣" && suit !== "♦" && input.length > 1) {
// return "Invalid card rank.";
// }
// }


// ======================== Test cases console.log() =====================

// console.log(getCardValue("10")); // Output: 10
// console.log(getCardValue("10♠")); // Output: 10
// console.log(getCardValue("5♥")); // Output: 5
// console.log(getCardValue("A")); // Output: 11
// console.log(getCardValue("k$")); // output Invalid card rank.
// console.log(getCardValue("k")); // output 10





// ========================== optimized version =======================

function getCardValue(input) {
// Define valid suits
const validSuits = ["♠", "♥", "♣", "♦"];

// Check if the input contains a valid suit emoji
const suit = input.slice(-1);
if (!validSuits.includes(suit)) {
return "Invalid card rank.";
}

// Extract rank (all characters except the last one)
const rank = input.slice(0, -1);

// Check if the rank is a valid numeric value or face card
if (!isNaN(rank) && Number(rank) >= 2 && Number(rank) <= 10) {
Copy link

Choose a reason for hiding this comment

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

A string representing a decimal numbers such as "2.3" could still pass this test.

A bulletproof approach would be to check if rank is exactly one of "2", "3", ..., "10" before doing the number conversion.

return Number(rank); // Handle numeric cards (2-10)
}

// Handle face cards (J, Q, K)
if (["J", "Q", "K"].includes(rank.toUpperCase())) {
return 10; // Face cards are worth 10
}

// Handle Ace (A)
if (rank.toUpperCase() === "A") {
return 11; // Ace is worth 11
}

// Handle invalid input (such as leading zeros, decimals, etc.)
return "Invalid card rank.";
}

// ======================== Test console.assert =====================

console.assert(getCardValue("10") === 10, "Test case failed for input '10'");
console.assert(getCardValue("10♠") === 10, "Test case failed for input '10♠'");
console.assert(getCardValue("5♥") === 5, "Test case failed for input '5♥'");
console.assert(getCardValue("A") === 11, "Test case failed for input 'A'");
console.assert(getCardValue("K") === 10, "Test case failed for input 'K'");
console.assert(getCardValue("3X") === "Invalid card rank.", "Test case failed for input '3X'");


console.assert(getCardValue("10♠") === 10, "Test case failed for input '10♠'");
console.assert(getCardValue("5♥") === 5, "Test case failed for input '5♥'");
console.assert(getCardValue("A") === 11, "Test case failed for input 'A'");
console.assert(getCardValue("K") === 10, "Test case failed for input 'K'");
console.assert(getCardValue("3X") === "Invalid card rank.", "Test case failed for input '3X'");
console.assert(getCardValue("010♠") === 10, "Test case failed for input '010♠'");
console.assert(getCardValue("02♠") === 2, "Test case failed for input '02♠'");
console.assert(getCardValue("0x02♠") === "Invalid card rank.", "Test case failed for input '0x02♠'");
console.assert(getCardValue("3.14♠") === "Invalid card rank.", "Test case failed for input '3.14♠'");

console.log("All tests passed!");

28 changes: 27 additions & 1 deletion Sprint-3/implement/is-proper-fraction.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,36 @@
// Negative Fraction check:
// Input: numerator = -4, denominator = 7
// target output: true
// Explanation: The fraction -4/7 is a proper fraction because the absolute value of the numerator (4) is less than the denominator (7). The function should return true.
// Explanation: The fraction -4/7 is a proper fraction because
// the absolute value of the numerator (4) is less than the denominator (7). The function should return true.

// Equal Numerator and Denominator check:
// Input: numerator = 3, denominator = 3
// target output: false
// 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) {
// Check for denominator being zero
if (denominator === 0) {
throw new Error("Denominator cannot be zero");
}

// If numerator is equal to denominator, it's not a proper fraction
if (numerator === denominator) {
Copy link

Choose a reason for hiding this comment

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

The condition at line 49 can also handle this case.

return false;
}

// Proper fraction if the absolute value of the numerator is less than the denominator
return Math.abs(numerator) < Math.abs(denominator);
}

// Test cases
console.log(isProperFraction(3, 6)); // true
console.log(isProperFraction(-3, 6)); // true
console.log(isProperFraction(5, 5)); // false
console.log(isProperFraction(-4, 7)); // true
console.log(isProperFraction(4, -7)); // true
console.log(isProperFraction(-4, -7)); // true
console.log(isProperFraction(9, 0)); // Error: Denominator cannot be zero

41 changes: 41 additions & 0 deletions Sprint-3/implement/is-valid-triangle.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,44 @@
// 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(side1, side2, side3){

// if(side1 === 0 || side2 === 0 || side3 === 0){
// return false;
// }

// else if(side1 + side2 > side3){
// return true;
// }
// else if(side2 + side3 > side1){
// return true;
// }
// else if(side1 + side3 > side2){
// return true;
// }
// return false

// }

// console.log(isValidTriangle(3, 20, 20));
// console.log(isValidTriangle(3, 6, 10));
// console.log(isValidTriangle(9, 0, 1));
// console.log(isValidTriangle(5, 0, 1));


// ========================== optimized version =======================

function isValidTriangle(side1, side2, side3) {
// Check for non-positive side lengths or triangle inequality failure
if (side1 <= 0 || side2 <= 0 || side3 <= 0 || side1 + side2 <= side3 || side2 + side3 <= side1 || side1 + side3 <= side2) {
return false;
}
return true;
}

console.log(isValidTriangle(3, 20, 20)); // true
console.log(isValidTriangle(3, 6, 10)); // false
console.log(isValidTriangle(9, 0, 1)); // false
console.log(isValidTriangle(5, 0, 1)); // false
Loading