Skip to content

London | Saba Farjamfard |Module-Structuring-and-Testing-data | Sprint 3 | Week 3 #160

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

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
0ef3b40
Completing sprint-3/Implement/get-angle-type.js
sabafarjamfard Nov 14, 2024
879e1e8
completing sprint-3/implement/get-card-value.js
sabafarjamfard Nov 14, 2024
a8532e5
completing sprint-3/implement/is-proper-fraction.js
sabafarjamfard Nov 14, 2024
fd4e76d
completing sprint-3/implement/is-valid-triangle.js
sabafarjamfard Nov 15, 2024
6e960af
completing sprint-3/implement/rotate-char.js
sabafarjamfard Nov 15, 2024
7b3d968
completing sprint-2/revise/implement/count.test.js
sabafarjamfard Nov 15, 2024
404c59a
completing sprint-3/revise/implement/count.test.js
sabafarjamfard Nov 15, 2024
eed572c
completing sprint-3/revise/implement/card-validator-1.js
sabafarjamfard Nov 15, 2024
01b0e55
completing sprint-3/revise/implement/card-validator-1.js
sabafarjamfard Nov 15, 2024
506356d
completing sprint-3/revise/implement/ get-ordinal-number.test.js
sabafarjamfard Nov 15, 2024
e836797
completing sprint-3/revise/implement/ is-prime.test.js
sabafarjamfard Nov 15, 2024
bd1d4dd
completing sprint-3/revise/implement/password-validator.test.js
sabafarjamfard Nov 15, 2024
5bae5d3
completing sprint-3/revise/implement/repeat.test.js
sabafarjamfard Nov 15, 2024
27044bc
completing sprint-3/revise/investigate/find.js
sabafarjamfard Nov 15, 2024
2baca3b
Merge branch 'main' into sprint-3-exercise-3
sabafarjamfard Nov 27, 2024
119cdf5
completing/sprint-3-all-test
sabafarjamfard Dec 2, 2024
0fb5862
merged sprint-3-exercise-3 into local branch
sabafarjamfard Dec 2, 2024
f31f756
completing/Sprint-3/implement/is-valid-triangle.js
sabafarjamfard Dec 5, 2024
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
14 changes: 14 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,17 @@
// Identify Reflex Angles:
// When the angle is greater than 180 degrees and less than 360 degrees,
// Then the function should return "Reflex 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";
}

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

describe("getAngleType", () => {
test("should return 'Invalid angle' for negative angles", () => {
expect(getAngleType(-1)).toBe("Invalid angle");
});

test("should return 'Invalid angle' for angles >= 360", () => {
expect(getAngleType(360)).toBe("Invalid angle");
expect(getAngleType(400)).toBe("Invalid angle");
});

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

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

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

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

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

test("should return 'Reflex angle' for angles between 180 and 360 degrees", () => {
expect(getAngleType(270)).toBe("Reflex angle");
expect(getAngleType(359.999)).toBe("Reflex angle");
});

test("should return 'Invalid angle' for non-numeric inputs", () => {
expect(getAngleType("90")).toBe("Invalid angle");
expect(getAngleType(null)).toBe("Invalid angle");
expect(getAngleType(undefined)).toBe("Invalid angle");
expect(getAngleType(NaN)).toBe("Invalid angle");
});
});
23 changes: 19 additions & 4 deletions Sprint-3/implement/get-card-value.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
// This problem involves playing cards: https://en.wikipedia.org/wiki/Standard_52-card_deck

// You will need to implement a function getCardValue

// You need to write assertions for your function to check it works in different cases

// 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),

// 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

Expand All @@ -29,3 +27,20 @@
// 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) {
const rank = card.slice(0, -1); // Extract all except the last character
const validRanks = ['A', 'K', 'Q', 'J', '10', '9', '8', '7', '6', '5', '4', '3', '2'];

if (!validRanks.includes(rank)) {
throw new Error("Invalid card rank");
}

if (rank === 'A') return 11;
if (['K', 'Q', 'J', '10'].includes(rank)) return 10;

return parseInt(rank, 10); // Convert numeric ranks to a number
}

module.exports = getCardValue;
23 changes: 23 additions & 0 deletions Sprint-3/implement/get-card-value.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const getCardValue = require('./get-card-value');

describe('getCardValue', () => {
test('should return correct value for number cards (2-10)', () => {
expect(getCardValue('2♠')).toBe(2);
expect(getCardValue('10♣')).toBe(10);
});

test('should return correct value for face cards (J, Q, K)', () => {
expect(getCardValue('J♦')).toBe(10);
expect(getCardValue('Q♥')).toBe(10);
expect(getCardValue('K♠')).toBe(10);
});

test('should return 11 for Ace (A)', () => {
expect(getCardValue('A♠')).toBe(11);
});

test('should throw an error for invalid card rank', () => {
expect(() => getCardValue('Z♠')).toThrow('Invalid card rank');
expect(() => getCardValue('1♣')).toThrow('Invalid card rank');
});
});
30 changes: 21 additions & 9 deletions Sprint-3/implement/is-proper-fraction.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
// You wil need to implement a function isProperFraction
// You need to write assertions for your function to check it works in different cases

// Terms:
// Fractions: https://www.bbc.co.uk/bitesize/topics/zt9n6g8/articles/zjxpp4j
// Written here like this: 1/2 == Numerator/Denominator

// Acceptance criteria:

// Proper Fraction check:
// Input: numerator = 2, denominator = 3
// target output: true
// Explanation: The fraction 2/3 is a proper fraction, where the numerator is less than the denominator. The function should return true.

// Explanation: The fraction 2/3 is a proper fraction, where the numerator is less than the denominator.
//The function should return true.
// Improper Fraction check:
// Input: numerator = 5, denominator = 2
// target output: false
// Explanation: The fraction 5/2 is an improper fraction, where the numerator is greater than or equal to the denominator. The function should return false.
// Explanation: The fraction 5/2 is an improper fraction, where the numerator is greater than or equal to the denominator.
//The function should return false.

// Zero Denominator check:
// Input: numerator = 3, denominator = 0
Expand All @@ -25,10 +23,24 @@
// 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.
// 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 (denominator === 0) {
throw new Error("Denominator cannot be zero");
}
return Math.abs(numerator) < Math.abs(denominator);
}

module.exports = isProperFraction ;
25 changes: 25 additions & 0 deletions Sprint-3/implement/is-proper-fraction.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const isProperFraction = require('./is-proper-fraction');

// Path to the function

describe('isProperFraction', () => {
test('should return true for a proper fraction', () => {
expect(isProperFraction(2, 3)).toBe(true);
});

test('should return false for an improper fraction', () => {
expect(isProperFraction(5, 2)).toBe(false);
});

test('should throw error for zero denominator', () => {
expect(() => isProperFraction(3, 0)).toThrow('Denominator cannot be zero');
});

test('should return true for negative proper fraction', () => {
expect(isProperFraction(-4, 7)).toBe(true);
});

test('should return false for equal numerator and denominator', () => {
expect(isProperFraction(3, 3)).toBe(false);
});
});
27 changes: 20 additions & 7 deletions Sprint-3/implement/is-valid-triangle.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,22 @@
// Side a = 3
// Side b = 3
// Side c = 3

// This is a valid triangle, because a plus b = 6 and 6 is greater than 3
// Another way to write this is a + b > c
// It's also true that b + c > a
// It's also true that a + c > b
// In our function isValidTriangle which takes as parameters the lengths of three sides, we need to invalidate
//any triangle where the sum of any two sides is less than or equal to the length of the third side.

// In our function isValidTriangle which takes as parameters the lengths of three sides, we need to invalidate any triangle where the sum of any two sides is less than or equal to the length of the third side.
// and we need to validate any triangle where the sum of any two sides is greater than the length of the third side.

// Acceptance criteria:

// scenario: invalid triangle
// Given the side lengths a, b, and c,
// When the sum of any two side lengths is less than or equal to the length of the third side (i.e., a + b <= c, a + c <= b, b + c <= a),
// Then it should return false because these conditions violate the Triangle Inequality, which states that the sum of the lengths of any two sides of a triangle must be greater than the length of the third side.
// When the sum of any two side lengths is less than or equal to the length of the third side
//(i.e., a + b <= c, a + c <= b, b + c <= a),

// Then it should return false because these conditions violate the Triangle Inequality, which states that the sum of
//the lengths of any two sides of a triangle must be greater than the length of the third side.

// scenario: invalid triangle
// Check for Valid Input:
Expand All @@ -32,4 +33,16 @@
// When the function is called with these values as input,
// 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.
// 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 (a <= 0 || b <= 0 || c <= 0) {
return false;
}
return (a + b > c && a + c > b && b + c > a)
}


module.exports = isValidTriangle;

15 changes: 15 additions & 0 deletions Sprint-3/implement/is-valid-triangle.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const isValidTriangle = require('./is-valid-triangle');

describe('isValidTriangle', () => {
test('should return true for a valid triangle', () => {
expect(isValidTriangle(3, 4, 5)).toBe(true); // example test
});

test('should return false for an invalid triangle', () => {
expect(isValidTriangle(1, 2, 3)).toBe(false); // example test
});

test('should return false for a triangle with zero or negative side length', () => {
expect(isValidTriangle(0, 4, 5)).toBe(false); // example test
});
});
33 changes: 28 additions & 5 deletions Sprint-3/implement/rotate-char.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,52 @@
// Scenario: Rotate Lowercase Letters:
// Given a lowercase letter character 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.
// 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"

// 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.
// 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"

// Scenario: Leave Non-Letter Characters Unchanged:
// Given a character 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.
// 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)

// 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'),
// 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').
// 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("Y", 2)); // Output: "A" (preserves case, but wraps around)




function rotateCharacter(char, shift) {
if (/[a-z]/.test(char)) {
const base = 'a'.charCodeAt(0);
return String.fromCharCode(((char.charCodeAt(0) - base + shift) % 26 + 26) % 26 + base);
} else if (/[A-Z]/.test(char)) {
const base = 'A'.charCodeAt(0);
return String.fromCharCode(((char.charCodeAt(0) - base + shift) % 26 + 26) % 26 + base);
} else {
return char;
}
}

module.exports = { rotateCharacter };
23 changes: 23 additions & 0 deletions Sprint-3/implement/rotate-char.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const { rotateCharacter } = require('./rotate-char'); // Import the function from rotate-char.js

describe('rotateCharacter', () => {
test('should rotate lowercase letters correctly', () => {
expect(rotateCharacter('a', 3)).toBe('d');
expect(rotateCharacter('z', 1)).toBe('a');
});

test('should rotate uppercase letters correctly', () => {
expect(rotateCharacter('A', 3)).toBe('D');
expect(rotateCharacter('Z', 1)).toBe('A');
});

test('should leave non-letter characters unchanged', () => {
expect(rotateCharacter('7', 5)).toBe('7');
expect(rotateCharacter('@', 2)).toBe('@');
});

test('should handle negative shifts', () => {
expect(rotateCharacter('d', -3)).toBe('a');
expect(rotateCharacter('A', -1)).toBe('Z');
});
});
32 changes: 32 additions & 0 deletions Sprint-3/revise/implement/count.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// implement a function countChar that counts the number of times a character occurs in a string

// Given a string str and a single character char to search for,
// When the countChar function is called with these inputs,
// Then it should:

// Scenario: Multiple Occurrences
// Given the input string str,
// And a character char that may occur multiple times with overlaps within str (e.g., 'a' in 'aaaaa'),
// When the function is called with these inputs,
// Then it should correctly count overlapping occurrences of char (e.g., 'a' appears five times in 'aaaaa').

// Scenario: No Occurrences
// Given the input string str,
// And a character char that does not exist within the case-sensitive str,
// When the function is called with these inputs,
// Then it should return 0, indicating that no occurrences of the char were found in the case-sensitive str.
// Function countChar that returns the number of times a character occurs in a string


function countChar(str, char) {
let count = 0;
for (let i = 0; i < str.length; i++) {
if (str[i] === char) {
count++;
}
}
return count;
}


module-exports = countChar;
Loading