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

West Midlands | Alireza Seifi Shalamzari | Module-Structuring-and-Testing-Data | Week 3 #231

Open
wants to merge 27 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
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
Binary file added Sprint-3/Test Images/get-angle-type.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Sprint-3/Test Images/get-card-value.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Sprint-3/Test Images/is-proper-fraction.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Sprint-3/Test Images/is-valid-triangle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 38 additions & 0 deletions Sprint-3/implement/get-angle-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,41 @@
// 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) {
// Check if the angle is exactly 90 degrees
if (angle === 90) {
return "Right angle";
}
// Check if the angle is less than 90 degrees
else if (angle < 90) {
return "Acute angle";
}
// Check if the angle is greater than 90 degrees and less than 180 degrees
else if (angle > 90 && angle < 180) {
return "Obtuse angle";
}
else if (angle === 180) {
return "Straight angle";
}
// Check if the angle is greater than 180 degrees and less than 360 degrees
else if (angle > 180 && angle < 360) {
return "Reflex angle";
}
//check if the angle is not an angle returns an error
else {
return "Invalid angle";
}
}
console.log(getAngleType(45.5)); // Should print "Invalid angle"
console.log(getAngleType("45")); // Should print "Invalid angle"
console.log(getAngleType(null)); // Should print "Invalid angle"
console.log(getAngleType(undefined)); // Should print "Invalid angle"
console.log(getAngleType(true)); // Should print "Invalid angle"
console.log(getAngleType(false)); // Should print "Invalid angle"
console.log(getAngleType(NaN)); // Should print "Invalid angle"
console.log(getAngleType(Infinity)); // Should print "Invalid angle"
console.log(getAngleType(-Infinity)); // Should print "Invalid angle"
console.log(getAngleType("hello")); // Should print "Invalid angle"
console.log(getAngleType([1, 2, 3])); // Should print "Invalid

45 changes: 45 additions & 0 deletions Sprint-3/implement/get-angle-type.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// get-angle-type.test.js
const getAngleType = require('./get-angle-type');

describe('getAngleType', () => {
test('should return "Right angle" for 90 degrees', () => {
expect(getAngleType(90)).toBe("Right angle");
});

test('should return "Acute angle" for angles less than 90 degrees', () => {
expect(getAngleType(45)).toBe("Acute angle");
expect(getAngleType(0)).toBe("Acute angle");
});

test('should return "Obtuse angle" for angles greater than 90 and less than 180 degrees', () => {
expect(getAngleType(135)).toBe("Obtuse angle");
});

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

test('should return "Reflex angle" for angles greater than 180 and less than 360 degrees', () => {
expect(getAngleType(225)).toBe("Reflex angle");
expect(getAngleType(359)).toBe("Reflex angle");
});

test('should return "Invalid angle" for angles less than 0 or greater than or equal to 360', () => {
expect(getAngleType(-45)).toBe("Invalid angle");
expect(getAngleType(360)).toBe("Invalid angle");
expect(getAngleType(400)).toBe("Invalid angle");
});

test('should return "Invalid angle" for non-numeric inputs', () => {
expect(getAngleType("45")).toBe("Invalid angle");
expect(getAngleType(null)).toBe("Invalid angle");
expect(getAngleType(undefined)).toBe("Invalid angle");
expect(getAngleType(true)).toBe("Invalid angle");
expect(getAngleType(false)).toBe("Invalid angle");
expect(getAngleType(NaN)).toBe("Invalid angle");
expect(getAngleType(Infinity)).toBe("Invalid angle");
expect(getAngleType(-Infinity)).toBe("Invalid angle");
expect(getAngleType("hello")).toBe("Invalid angle");
expect(getAngleType([1, 2, 3])).toBe("Invalid angle");
});
});
47 changes: 47 additions & 0 deletions Sprint-3/implement/get-card-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,50 @@
// 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) {
// Remove the suit from the card string
const rank = card.slice(0, -1);

// Define valid ranks
const validRanks = new Set(["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"]);

// Check if the rank is valid
if (!validRanks.has(rank)) {
throw new Error("Invalid card rank.");
}

// Handle Number Cards (2-9)
if (rank >= "2" && rank <= "9") {
Copy link

Choose a reason for hiding this comment

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

Todo

This check is not solid enough.
You can test your function by calling getCardValue("23♠") or getCardValue("899♠") to see what they return.

Copy link
Author

Choose a reason for hiding this comment

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

I did work on this and I found a solution!
I added a set of valid ranks so only the valid ones can return an actual value

return parseInt(rank);
} else if (rank === "10" || rank === "J" || rank === "Q" || rank === "K") { // Handle Face Cards (10, J, Q, K)
return 10;
} else if (rank === "A") { // Handle Ace (A)
return 11;
}
}


// Assertions to test the function
try {
console.assert(getCardValue("2♠") === 2, "Test Case 1 Failed");
console.assert(getCardValue("5♦") === 5, "Test Case 2 Failed");
console.assert(getCardValue("10♥") === 10, "Test Case 3 Failed");
console.assert(getCardValue("J♣") === 10, "Test Case 4 Failed");
console.assert(getCardValue("Q♠") === 10, "Test Case 5 Failed");
console.assert(getCardValue("K♦") === 10, "Test Case 6 Failed"); //using 10 it should pass and 20 should fail
console.assert(getCardValue("A♣") === 11, "Test Case 7 Failed");
console.log(getCardValue("23♠"));
console.log(getCardValue("899♠"));

// Testing invalid card
try {
getCardValue("Z♠");
} catch (e) {
console.assert(e.message === "Invalid card rank.", "Test Case 8 Failed");
}

console.log("All test cases passed!");
} catch (error) {
console.error(error.message);
}
21 changes: 21 additions & 0 deletions Sprint-3/implement/is-proper-fraction.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,24 @@
// 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) {
// Get the absolute values of numerator and denominator
const absNumerator = Math.abs(numerator);
const absDenominator = Math.abs(denominator);

// Check if the denominator is zero and throw an error if it is
if (absDenominator === 0) {
throw new Error("Denominator cannot be zero");
}

// Check if the absolute value of the numerator is less than the absolute value of the denominator
return absNumerator < absDenominator;
}

//Testing the function
console.log(isProperFraction(2, 3)); //should return True
console.log(isProperFraction(5, 2)); //should return False
console.log(isProperFraction(-4, 7)); //should return True
Copy link

Choose a reason for hiding this comment

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

In mathematics, -4/7 == 4/-7, and -4/-7 == 4/7.
So, ideally isProperFraction() should recognise all of them as proper fractions.

Hint: If you compute the absolute value of both parameters inside the function first, the code can become much simpler.

Copy link
Author

Choose a reason for hiding this comment

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

I added a part which first make absolute numbers then check them

console.log(isProperFraction(3, 3)); //should return False
console.log(isProperFraction(3, 0)); //should throw an error

19 changes: 19 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,22 @@
// 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) {
// Check for valid input: all sides must be greater than zero
if (a <= 0 || b <= 0 || c <= 0) {
return false;
}
// Check the Triangle Inequality: the sum of any two sides must be greater than the third
if (a + b <= c || a + c <= b || b + c <= a) {
return false;
}
return true;
}

//Testing the functionality
console.log(isValidTriangle(3, 3, 3)); // Expected output: true
console.log(isValidTriangle(3, 3, 6)); // Expected output: false
console.log(isValidTriangle(3, 0, 3)); // Expected output: false


62 changes: 59 additions & 3 deletions Sprint-3/implement/rotate-char.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
// 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("f", 1)); // Output: "g" //failed

// Scenario: Rotate Uppercase Letters:
// Given an uppercase letter character 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("A", 3)); // Output: "D" //failed
console.log(rotateCharacter("F", 1)); // Output: "G"

// Scenario: Leave Non-Letter Characters Unchanged:
Expand All @@ -39,5 +39,61 @@ console.log(rotateCharacter("7", 5)); // Output: "7" (unchanged, not a letter)
// 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" (preserves case, but wraps around)
console.log(rotateCharacter("z", 1)); // Output: "a" (preserves case, but wraps around) //failed
console.log(rotateCharacter("Y", 2)); // Output: "A" (preserves case, but wraps around)


// The function should handle both lowercase and uppercase letters, as well as non-letter characters, and
// correctly rotate the letters by the specified shift value while preserving their original case and handling wraparound
// correctly. The function should return the rotated character as a string, or the original character if
// it is not a letter. The function should handle both positive and negative shift values, and
// should correctly handle wraparound for both lowercase and uppercase letters. The function should
// preserve the original case of the input character, and should return the rotated character in the same
// case as the original character. The function should handle both single-character and multi-character
// inputs, and should return the rotated character as a string. The function should handle both ASCII
// and non-ASCII characters, and should correctly rotate the characters by the specified shift value while
// preserving their original case and handling wraparound correctly. The function should return the rotated
// character as a string, or the original character if it is not a letter. The function
// should handle both positive and negative shift values, and should correctly handle wraparound for both
// lowercase and uppercase letters. The function should preserve the original case of the input character, and
// should return the rotated character in the same case as the original character. The function should handle
// both single-character and multi-character inputs, and should return the rotated character as a string
// The rotateCharacter function can be enhanced to handle negative shifts and multi-character strings.
// This will allow for more flexible usage in text encryption and decryption scenarios.

function rotateCharacter(char, shift) {
// Check if the character is a letter (either uppercase or lowercase)
if (char.match(/[a-zA-Z]/)) {
// Determine the base character (either 'a' or 'A') based on the case of the input character
let baseChar = char.toLowerCase() === 'a' ? 'a' : 'A';
// Calculate the new character by shifting the base character by the specified shift
let newChar = String.fromCharCode((char.charCodeAt(0) - baseChar.charCodeAt(0) + shift + 26) % 26 + baseChar.charCodeAt(0));
return newChar;
}
// If the character is not a letter, return it unchanged
return char;
}

// New function to handle multi-character strings
function rotateString(str, shift) {
let result = '';
for (let i = 0; i < str.length; i++) {
result += rotateCharacter(str[i], shift);
}
return result;
}

// Test cases for multi-character strings
console.log(rotateString("hello", 2)); // Output: "jgnnq" //failed
console.log(rotateString("Hello, World!", 3)); // Output: "Khoor, Zruog!" //failed
console.log(rotateString("Zebra 123!", 5)); // Output: "Fjwdwf 123!" //failed

// Test cases for negative shifts
console.log(rotateCharacter("d", -1)); // Output: "c" //failed
console.log(rotateCharacter("A", -3)); // Output: "X" //failed
console.log(rotateString("Hello, World!", -3)); // Output: "Ebiil, Tloia!" //failed

// The rotateCharacter function now supports both positive and negative shifts,
// and the rotateString function allows for rotating entire strings while preserving non-letter characters.
// The rotateCharacter function can be enhanced to handle negative shifts and multi-character strings.
// This will allow for more flexible usage in text encryption and decryption scenarios.
Loading