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

SOUTH AFRICA CPT-ITP | LUKE MANYAMAZI | MODULE STRUCTURING AND TESTING | WEEK 3 #212

Open
wants to merge 15 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
28 changes: 28 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,31 @@
// 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) {
let type;

switch (true) {
Copy link

Choose a reason for hiding this comment

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

You are switching or 'looping' through the possible values of which variable? (Hint - not of true, though I see your idea, you used true inside of switch() so that you always go into this switch construction. But this is not the usual way to use switch).

Copy link

Choose a reason for hiding this comment

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

I gave it a second though, I think your approach is valid, though my first though for solving this task would be if-else conditions.

case angle === 90:
type = "Right angle";
break;

case angle < 90 && angle > 0:
type = "Acute angle";
break;

case angle > 90 && angle < 180:
type = "Obtuse angle";
break;

case angle === 180:
type = "Straight line";
break;

case angle > 180 && angle < 360:
type = "Reflex angle";
break;
}
return type;
}
console.log(getAngleType(180));
47 changes: 47 additions & 0 deletions Sprint-3/implement/get-angle-type.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const getAngleType = require("./get-angle-type"); // Import the function

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

// Test for acute angles (between 0 and 90)
test('should return "Acute angle" for angles between 0 and 90', () => {
expect(getAngleType(45)).toBe("Acute angle");
expect(getAngleType(1)).toBe("Acute angle");
});

// Test for obtuse angles (between 90 and 180)
test('should return "Obtuse angle" for angles between 90 and 180', () => {
expect(getAngleType(120)).toBe("Obtuse angle");
expect(getAngleType(179)).toBe("Obtuse angle");
});

// Test for a straight line
test('should return "Straight line" for 180 degrees', () => {
expect(getAngleType(180)).toBe("Straight line");
});

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

// Test for angles outside the valid range (0-360 degrees)
test('should return "Invalid angle" for angles less than 0 or greater than 360', () => {
expect(getAngleType(-10)).toBe("Invalid angle");
expect(getAngleType(400)).toBe("Invalid angle");
});

// Test for angle of 0 degrees (should be invalid)
test('should return "Invalid angle" for 0 degrees', () => {
expect(getAngleType(0)).toBe("Invalid angle");
});

// Test for angle of 360 degrees (should be invalid as well)
test('should return "Invalid angle" for 360 degrees', () => {
expect(getAngleType(360)).toBe("Invalid angle");
});
});
44 changes: 44 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,47 @@
// 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) {
// Here I am getting the value input and slicing it to get only the numerical value
let value = card.slice(0, -1);

// Here I am declaring the cardNumber, faceCard and ace the ace values as per given instructions
let cardNumber = ["2", "3", "4", "5", "6", "7", "8", "9"];
let faceCard = ["10", "J", "K", "Q"];
let ace = "A";

// this below code will change the string value to int using the parseInt function
if (cardNumber.includes(value)) {
return parseInt(value);
}

if (faceCard.includes(value)) {
return 10;
}

if (value === ace) {
return 11;
}

// Here I am catching the error if user input is not part of the defined values
if (!!cardNumber || !!faceCard || !!ace) {
return "Error: Invalid card value";
}
}
console.log(getCardValue("16♠"));

// the following are assertion test cases to check if the function is working correctly with different values
// Valid cases
console.assert(getCardValue("7♠") === 7, "Test Failed: 7♠ should return 7");
console.assert(getCardValue("10♣") === 10, "Test Failed: 10♣ should return 10");
console.assert(getCardValue("Q♦") === 10, "Test Failed: Q♦ should return 10");
console.assert(getCardValue("A♥") === 11, "Test Failed: A♥ should return 11");

// Edge cases
console.assert(getCardValue("2♠") === 2, "Test Failed: 2♠ should return 2");
console.assert(getCardValue("K♦") === 10, "Test Failed: K♦ should return 10");

// Invalid cases
console.assert(getCardValue("Q♦") === 11, "Test Failed: Q♦ should return 10"); // deliberately made it wrong to check if the functions is working
console.assert(getCardValue("A♥") === 11, "Test Failed: A♥ should return 11");
39 changes: 39 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,42 @@
// 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) {
if (numerator < denominator) {
return "True";
}

if (denominator === 0) {
return "Error (Denominator cannot be zero)";
}
return "False";
}

console.log(isProperFraction(2, 3));
console.log(isProperFraction(5, 2));
console.log(isProperFraction(3, 0));
console.log(isProperFraction(-4, 7));
console.log(isProperFraction(3, 3));

// Assertions
console.assert(
isProperFraction(2, 3) === "True",
"Test Case 1 Failed: Proper Fraction"
); // Proper fraction
console.assert(
isProperFraction(5, 2) === "False",
"Test Case 2 Failed: Improper Fraction"
); // Improper fraction
console.assert(
isProperFraction(3, 0) === "Error (Denominator cannot be zero)",
"Test Case 3 Failed: Zero Denominator"
); // Zero denominator
console.assert(
isProperFraction(-4, 7) === "True",
"Test Case 4 Failed: Negative Proper Fraction"
); // Negative proper fraction
console.assert(
isProperFraction(3, 3) === "False",
"Test Case 5 Failed: Equal Numerator and Denominator"
); // Equal numerator and denominator
44 changes: 44 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,47 @@
// 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) {
const sides = [a, b, c];
let i = 0;

while (i < sides.length) {
if (sides[i] === 0) {
return "False";
}
i++;
}

if (a + b > c && a + c > b && b + c > a) {
return "True";
}

return "False";
}

console.log(isValidTriangle(0, 3, 4)); // False (one side is zero)
console.log(isValidTriangle(3, 4, 5)); // True (valid triangle)
console.log(isValidTriangle(1, 10, 12)); // False (violates triangle inequality)
console.log(isValidTriangle(5, 5, 5)); // True (equilateral triangle)
console.log(isValidTriangle(2, 2, 4)); // False (degenerate case)

console.assert(
isValidTriangle(0, 3, 4) === "False",
"Test Case 1 Failed: One side is zero"
); // Zero side
console.assert(
isValidTriangle(3, 4, 5) === "True",
"Test Case 2 Failed: Valid triangle"
); // Valid triangle (3-4-5 triangle)
console.assert(
isValidTriangle(1, 10, 12) === "False",
"Test Case 3 Failed: Triangle inequality violated"
); // Inequality violation
console.assert(
isValidTriangle(5, 5, 5) === "True",
"Test Case 4 Failed: Equilateral triangle"
); // Valid equilateral triangle
console.assert(
isValidTriangle(2, 2, 4) === "False",
"Test Case 5 Failed: Degenerate case"
); // Degenerate triangle
14 changes: 14 additions & 0 deletions Sprint-3/implement/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "my-project",
"version": "1.0.0",
"description": "A project that uses Jest for testing",
"main": "index.js",
"scripts": {
"test": "jest"
},
"author": "",
"license": "ISC",
"devDependencies": {
"jest": "^28.0.0"
}
}
36 changes: 29 additions & 7 deletions Sprint-3/implement/rotate-char.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,50 @@
// 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.
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.
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.
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'),
// 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("Y", 2)); // Output: "A" (preserves case, but wraps around)

function rotateChar(char, shift) {
let alphabet = "";

if (char >= "a" && char <= "z") {
alphabet = "abcdefghijklmnopqrstuvwxyz";
}

if (char >= "A" && char <= "Z") {
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
}

if (alphabet === "") {
return char;
}

let index = alphabet.indexOf(char);
let newIndex = (index + shift) % alphabet.length;

return alphabet[newIndex];
}

console.log(rotateChar("a", 3)); // Output: "d"
console.log(rotateChar("e", 2)); // Output: "g"
console.log(rotateChar("z", 1)); // Output: "a" (preserves case, but wraps around)
console.log(rotateChar("Y", 2)); // Output: "A" (preserves case, but wraps around)
console.log(rotateChar("7", 5)); // Output: "7" (unchanged, not a letter)
console.log(rotateChar("A", 3)); // Output: "D"
console.log(rotateChar("F", 1)); // Output: "G"
36 changes: 36 additions & 0 deletions Sprint-3/revise/implement/count.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,39 @@
// 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(str, char) {
let count = 0;

for (let i = 0; i < str.length; i++) {
if (str[i] === char) {
count++;
}
}
return `${char} appears ${count} times in "${str}"`;
}

console.assert(
countChar("laka", "a") === 2,
'Test failed: "a" should appear 2 times in "laka"'
);
console.assert(
countChar("luke", "u") === 1,
'Test failed: "u" should appear 1 time in "luke"'
);
console.assert(
countChar("heLLo", "l") === 0,
'Test failed: "l" should appear 0 times in "heLLo" (case-sensitive)'
);
console.assert(
countChar("banana", "a") === 3,
'Test failed: "a" should appear 3 times in "banana"'
);
console.assert(
countChar("abc", "z") === 0,
'Test failed: "z" should appear 0 times in "abc"'
);
console.assert(
countChar("Mississippi", "s") === 4,
'Test failed: "s" should appear 4 times in "Mississippi"'
);
39 changes: 39 additions & 0 deletions Sprint-3/revise/implement/is-prime.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,42 @@
// Given a positive integer num,
// When the isPrime function is called with num as input,
// Then it should check if the num is prime

function isPrime(num) {
if (num < 2) {
return `${num} is not a prime number`;
}

for (let i = 2; i * i <= num; i++) {
if (num % i === 0) {
return `${num} is not a prime number`;
}
}

return `${num} is a prime number`;
}

console.assert(
isPrime(1) === "1 is not a prime number",
"Test failed: 1 should not be a prime number"
);
console.assert(
isPrime(2) === "2 is a prime number",
"Test failed: 2 should be a prime number"
);
console.assert(
isPrime(3) === "3 is a prime number",
"Test failed: 3 should be a prime number"
);
console.assert(
isPrime(4) === "4 is not a prime number",
"Test failed: 4 should not be a prime number"
);
console.assert(
isPrime(17) === "17 is a prime number",
"Test failed: 17 should be a prime number"
);
console.assert(
isPrime(18) === "18 is not a prime number",
"Test failed: 18 should not be a prime number"
);
Loading