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

CYF-ITP-South Africa | Natalie Patel | Module-Structuring-and-Testing-Data | Sprint 3 #229

Open
wants to merge 26 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
539b04e
Wrote the function for getAngleType
NataliePatel Dec 3, 2024
35898f3
Initial test cases written in test.js file
NataliePatel Dec 4, 2024
4cd6e85
Updated json files
NataliePatel Dec 5, 2024
970d3a3
Corrected test file name
NataliePatel Dec 5, 2024
8bc9668
Corrected test file name
NataliePatel Dec 5, 2024
ecdc094
Created folder for tests and corrected errors in .js code
NataliePatel Dec 5, 2024
10a66ec
Initial commit for is-proper-fraction .js and .test.js files
NataliePatel Dec 5, 2024
a2d867e
Wrote function and test code
NataliePatel Dec 5, 2024
9cf5a0e
Tested and fixed failed tests
NataliePatel Dec 5, 2024
ff0b518
Wrote tests and updated .js file to ensure passes
NataliePatel Dec 10, 2024
024d1dc
Initial commit for is-valid-triangle exercise
NataliePatel Dec 10, 2024
2f88c6a
Wrote .js and .test.js files and passed tests
NataliePatel Dec 10, 2024
a117de1
Initial commit for rotate-char .js and .test.js files
NataliePatel Dec 13, 2024
353f076
Wrote and tested .js and .test.js files for rotate-char
NataliePatel Dec 13, 2024
28ef0a5
Wrote .js and .test.js for revise count.js
NataliePatel Dec 13, 2024
780fd32
Completed investigate find.js solutions
NataliePatel Dec 15, 2024
8b6570e
Wrote get-ordinal-number.js file
NataliePatel Dec 15, 2024
df48596
Updated get-ordinal-number.js and test.js files
NataliePatel Dec 15, 2024
e516fc4
Wrote tests for get-ordinal-number
NataliePatel Dec 15, 2024
5ece594
Initial commit for isPrime and fixed comment in find.js
NataliePatel Dec 15, 2024
29b658e
Wrote .js and .test.js files for isPrime
NataliePatel Dec 15, 2024
e79bc0a
Initial commit for passwordValidator
NataliePatel Dec 15, 2024
84c7f64
Wrote passwordValidator .js and test.js files
NataliePatel Dec 16, 2024
a4e9a9d
Wrote .js and test.js files for repeat function
NataliePatel Dec 16, 2024
72665a7
Wrote .js and .test.js files for validateCard function
NataliePatel Dec 16, 2024
5de85c4
Updated get-card-value to handle for 3 or more characters
NataliePatel Dec 17, 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
62 changes: 62 additions & 0 deletions Sprint-3/Tests/implement/get-angle-type.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/* Testing criteria
If the input is 90, the output should be "Right angle"
If the input is less than 90, the output should be "Acute angle"
If the input is more than 90, the output should be "Obtuse angle"
If the input is 180, the output should be "Straight angle"
If the input is more than 180 but less than 360, the output should be "Reflex angle"
If the input is any value outside of these parameters, the output should be "Please enter a valid angle"
*/

const {getAngleType} = require ('../../implement/get-angle-type');
describe("getAngleType function", () => {
//Typical cases
test("should return 'Right angle' when 90 degrees", () => {
expect(getAngleType(90)).toBe("Right angle");
});

test("should return 'Acute angle' when less than 90 degrees", () => {
expect(getAngleType(60)).toBe("Acute angle");
});

test("should return 'Obtuse angle' when more than 90 degrees", () => {
expect(getAngleType(130)).toBe("Obtuse angle");
});

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

test("should return 'Reflex angle' when more than 180 but less than 360 degrees", () => {
expect(getAngleType(240)).toBe("Reflex angle");
});


//Edge cases
test("should return 'Acute angle' when less than 90 degrees", () => {
expect(getAngleType(76)).toBe("Acute angle");
});

test("should return 'Obtuse angle' when more than 90 degrees", () => {
expect(getAngleType(169)).toBe("Obtuse angle");
});

test("should return 'Reflex angle' when more than 180 but less than 360 degrees", () => {
expect(getAngleType(321)).toBe("Reflex angle");
});


//Invalid cases
test("should return 'Please enter a valid angle' when negative degrees", () => {
expect(getAngleType(-45)).toBe("Please enter a valid angle");
});

test("should return 'Please enter a valid angle' when equal to or greater than 360 degrees", () => {
expect(getAngleType(360)).toBe("Please enter a valid angle");
});

test("should return 'Please enter a valid angle' for inputs that are not numbers", () => {
expect(getAngleType("string")).toBe("Please enter a valid angle");
expect(getAngleType(null)).toBe("Please enter a valid angle");
expect(getAngleType(undefined)).toBe("Please enter a valid angle");
});
});
50 changes: 50 additions & 0 deletions Sprint-3/Tests/implement/get-card-value.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* Testing criteria - returning a numerical value for card inputs of different types
The card input will consist of a letter (JKQA), a number (2-10) and an emoji to represent suit (♠♥♦♣)
If the input is not a string, an error must be thrown.
When this card string is entered, it must return a numeric value.Cards with inputs between "2-9" should return numeric equivalent.
The cards "10 J Q K" should return the value of 10.
The card with input "A" should return a default value of 11.
When no suit emoji has been entered, it must return "Please enter a card suit emoji"
All other inputs should throw error "Invalid card rank"
♠♥♦♣
*/

const {getCardValue} = require ('../../implement/get-card-value');
describe("getCardValue function", () => {
//Invalid cases

test("should throw error when input is not a string, a recognised card number or face card", () => {
expect(() =>getCardValue(23456789)).toThrow("Invalid card rank or card suit");
expect(() =>getCardValue("11♠")).toThrow("Invalid card rank or card suit"); //must be numbers 2-10
expect(() =>getCardValue("-2♦")).toThrow("Invalid card rank or card suit"); //must be positive number
expect(() =>getCardValue("P♥")).toThrow("Invalid card rank or card suit"); //must be valid face card
expect(() =>getCardValue("J💔")).toThrow("Invalid card rank or card suit"); //must be valid emoji suit
expect(() =>getCardValue(null)).toThrow("Invalid card rank or card suit"); //must have an entry
expect(() =>getCardValue(undefined)).toThrow("Invalid card rank or card suit"); //must be a defined entry

});

//Error case for when no suit emoji is entered
test("should return 'Invalid card rank or card suit' when there are less than 2 characters", () => {
expect(() =>getCardValue("4")).toThrow("Invalid card rank or card suit");
});


//Typical cases
test("should return numerical '5' when string 5 and emoji suit is input", () => {
expect(getCardValue("5♣")).toBe(5);
});

test("should return '10' when card input is 10, J, Q, K", () => {
expect(getCardValue("J♠")).toBe(10);
expect(getCardValue("10♦")).toBe(10);
expect(getCardValue("Q♥")).toBe(10);
expect(getCardValue("K♣")).toBe(10);
expect(getCardValue("10♠")).toBe(10);
});

test("should return '11' when card input is A", () => {
expect(getCardValue("A♥")).toBe(11);
})

});
33 changes: 33 additions & 0 deletions Sprint-3/Tests/implement/is-valid-triangle.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* Testing criteria
The results should either be true or false to indicate a valid triangle.
If any of the sides = 0 or a value is negative, it must return "false"
If the sum of any two sides is <= the 3rd side, it must return "false"
If the sum of any two sides is greater than the 3rd side, it must return "true"
If string characters and not numbers are input, it must throw an error "Please enter numeric triangle side values"
*/


const {isValidTriangle} = require ('../../implement/is-valid-triangle');
describe("isValidTriangle function", () => {

//Error cases
test ("should throw an error 'Please enter numeric triangle side values' when inputs are NaN", () => {
expect(() =>isValidTriangle("triangle")).toThrow("Please enter numeric triangle side values");
expect(() =>isValidTriangle(null)).toThrow("Please enter numeric triangle side values"); //must have entries
expect(() =>isValidTriangle(undefined)).toThrow("Please enter numeric triangle side values"); //must be defined entries
});

//Invalid cases
test ("should return 'false' when values are equal to zero or negative", () => {
expect(isValidTriangle(0, -4, 3)).toBe(false);
expect(isValidTriangle(0, 0, 0)).toBe(false);
})

//There is no need to write a test to check for the sum of 2 sides being larger than the 3rd if negative and zero numbers have been handled.

//Valid cases
test ("should return 'true' when the sum of 2 sides is >= 3rd side", () => {
expect(isValidTriangle(3, 4, 5)).toBe(true);
})

});
28 changes: 28 additions & 0 deletions Sprint-3/Tests/implement/rotate-char.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* Testing criteria
An inputted string character must rotate by 3 and output the corresponding alphabet position.
An inputted number must remain the same.
An inputted uppercase letter must output a rotated uppercase letter.
An inputted lowercase letter must output a rotated lowercase letter.
All inputted letters at the end of the alphabet must wrap around to the beginning.
*/

const {rotateCharacter} = require ('../../implement/rotate-char');
describe("rotateCharacter function", () => {

//Invalid cases
test("Please enter a number or character", () => {
expect(() =>rotateCharacter(null)).toThrow("Please enter a number or character"); //must have an entry
expect(() =>rotateCharacter(undefined)).toThrow("Please enter a number or character"); //must be an entry
})

//Typical cases
test("should return lowercase 'b' + rotation when string b and rotation is input", () => {
expect(rotateCharacter("a", 3)).toBe("d");
expect(rotateCharacter("c", 4)).toBe("g");
expect(rotateCharacter("D", 2)).toBe("F");
expect(rotateCharacter("6", 2)).toBe(6);
expect(rotateCharacter("y", 3)).toBe("b");
expect(rotateCharacter("Y", 3)).toBe("B");
});

});
45 changes: 45 additions & 0 deletions Sprint-3/Tests/is-proper-fraction.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/* Testing criteria - to determine that the input is a proper fraction, true or false
The input will consist of 2 numbers, numerator and denominator:
If the denominator is 0, it should throw an error: "Denominator cannot be zero"
If the numerator is 0, it should throw an error: "Numerator cannot be zero"
If either the numerator or denominator is a negative number, it must be converted to absolute values.
If the numerator is smaller than the denominator, it is a proper fraction: true
If the numerator is larger than the denominator it is not a proper fraction: false
If the numerator is equal to the denominator, it is not a proper fraction: false
If any other inputs are entered, it should return: "Please enter a valid numerator and/or denominator"
*/

const {isProperFraction} = require ('../implement/is-proper-fraction');
describe("isProperFraction function", () => {
//Invalid cases

test("should throw error when numerator or denominator is 0", () => {
expect(() => isProperFraction(0, 5)).toThrow("The numerator and/or denominator cannot be zero");
});

test("should throw error when numerator or denominator is 0", () => {
expect(() => isProperFraction(5, 0)).toThrow("The numerator and/or denominator cannot be zero");
});

test("should throw error when numerator or denominator is 0", () => {
expect(() => isProperFraction(0, 0)).toThrow("The numerator and/or denominator cannot be zero");
});

test("should return 'Please enter a valid numerator and/or denominator' for inputs that are not numbers", () => {
expect(isProperFraction("string")).toBe("Please enter a valid numerator and/or denominator");
expect(isProperFraction(null)).toBe("Please enter a valid numerator and/or denominator");
expect(isProperFraction(undefined)).toBe("Please enter a valid numerator and/or denominator");
});
//Typical cases
test("should return 'True' when numerator is smaller than denominator", () => {
expect(isProperFraction(2, 5)).toBe("True");
});

test("should return 'False' when numerator is larger than denominator", () => {
expect(isProperFraction(5, 2)).toBe("False");
});

test("should return 'False' when numerator is equal to the denominator", () => {
expect(isProperFraction(2, 2)).toBe("False");
});
});
25 changes: 25 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,28 @@
// 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 > 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";
}
else if (angle <= 0 && angle >= 360) {
Copy link

Choose a reason for hiding this comment

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

If angle is not any of the valid value, then it must be invalid. So an else would be suffice.

return "Please enter a valid angle";
}
else {
return "Please enter a valid angle";
}
}
module.exports = {getAngleType};
50 changes: 50 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,53 @@
// 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) {

//The string input must consist of 2 characters if the emoji suit is to be included
if (typeof card !="string" || card.length < 2) {
throw new Error("Invalid card rank or card suit");
}

//The input has to be separated into the rank and suit emoji so that they can be handled separately
let rank = card.slice(0, -1); //If there are 2 characters in the string, this slices just the first character
let suit = card.slice(-1); //This isolates the suit emoji

//Define valid suits and face cards
let validSuits = ["♠", "♥", "♦", "♣"];
let validFaceCards = ["J", "Q", "K", "A"];

//Instructing the only suit emojis that are valid. When the condition is true (the suit input is NOT in the valid array) it throws an error.

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

//Handling when card inputs are 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

Can you also check if getCardValue("23♠") or getCardValue("8ABC♠") are returning the value you expect?

return parseInt(rank, 10); //parseInt parses a string and returns the first integer, 10 indicates decimal radix for numbers 0-9
}

//Handling when card input is 10
if (rank === "10") {
return 10;
}

//Handling face cards worth 10 and the Ace worth 11
if (validFaceCards.includes(rank)) {
if (rank === "A") {
return 11;
}
return 10; //When not the A but the other face cards J, Q, K
}

//If none of the valid rank parameters are matched, throw an error
{
throw new Error("Invalid card rank or card suit");
}
}

module.exports = {getCardValue};



27 changes: 27 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,30 @@
// 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 if numerator OR denominator is 0, throw an error
if (numerator === 0 || denominator === 0) {
Copy link

Choose a reason for hiding this comment

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

0 can be considered a fraction. So numerator should be allowed to be 0.

throw new Error("The numerator and/or denominator cannot be zero");
}

//Ensure that numerator and denominator are absolute values

let num = Math.abs(numerator);
let den = Math.abs(denominator);

if (num < den) {
return "True";
Copy link

Choose a reason for hiding this comment

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

Todo

Should return the Boolean value true or false instead of strings "True" or "False".

}
else if (num > den) {
Copy link

Choose a reason for hiding this comment

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

Assuming num and den are finite numbers.
If num is not less than den, then it would mean num is either greater than or equal to den. As a result, an else would be suffice.

return "False"
}
else if (num === den) {
return "False"
}
//When the numerator and denominator are not numbers
else {
return "Please enter a valid numerator and/or denominator";
}
Comment on lines +56 to +59
Copy link

Choose a reason for hiding this comment

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

If you want to check for invalid types, you should do the checking at the beginning of the function so that subsequent code in the function won't have to deal with invalid values.

}
module.exports = {isProperFraction};
30 changes: 30 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,33 @@
// 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) {
//Using map allows for each value in the array to be processed, this case, to ensure that each value is a number
const sides = [a, b, c].map(value => {
const number = Number(value);
if (isNaN(number)) {
throw new Error("Please enter numeric triangle side values");
}
return number;
});

//The extracted validated numbers need to be assigned using the destructuring method
const [sideA, sideB, sideC] = sides;

//This will handle when values are zero or negative: invalid triangle

if (sideA <= 0 || sideB <= 0 || sideC<= 0) {
return false;
}

//This will handle when the sum of 2 sides is not greater than the third side: invalid triangle
if (sideA + sideB <= sideC || sideA + sideC <= sideB || sideB + sideC <= sideA) {
return false;
}

//This will handle when the criteria have all been met for a valid triangle
return true;

}
module.exports = {isValidTriangle};
Loading