Skip to content

Sheffield| May 2025| Mayowa Fadare| Structure testing sprint 3 #633

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 11 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion Sprint-1/3-mandatory-interpret/1-percentage-change.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ let carPrice = "10,000";
let priceAfterOneYear = "8,543";

carPrice = Number(carPrice.replaceAll(",", ""));
priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));
priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ,""));

const priceDifference = carPrice - priceAfterOneYear;
const percentageChange = (priceDifference / carPrice) * 100;
Expand Down
23 changes: 15 additions & 8 deletions Sprint-3/2-mandatory-rewrite/1-get-angle-type.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
function getAngleType(angle) {
// Ensure angle is a number and within valid bounds
if (typeof angle !== "number" || angle <= 0 || angle >= 360) {
return "Invalid angle";
}
if (angle < 90) {
return "Acute angle";
}
if (angle === 90) {
return "Right angle";
// replace with your completed function from key-implement

} else if (angle < 90) {
return "Acute angle";
} else if (angle > 90 && angle < 180) {
}
if (angle < 180) {
return "Obtuse angle";
} else if (angle === 180) {
}
if (angle === 180) {
return "Straight angle";
} else if (angle > 180 && angle < 360) {
return "Reflex angle";
}
// Any remaining angle must be between 180 and 360 (exclusive)
return "Reflex angle";
}



// Don't get bogged down in this detail
// Jest uses CommonJS module syntax by default as it's quite old
// We will upgrade our approach to ES6 modules in the next course module, so for now
Expand Down
21 changes: 9 additions & 12 deletions Sprint-3/2-mandatory-rewrite/2-is-proper-fraction.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@

function isProperFraction(numerator, denominator) {
if (numerator < denominator) return true;}
if (Math.abs(numerator) < Math.abs(denominator)) {
return true;
} { return false; }
if (numerator < denominator) {
return true;
} if (numerator === denominator) {
return false;
} if (denominator === 0) {
return false;
}
// Check for invalid fraction (denominator cannot be zero)
if (denominator === 0) {
return false;
}
// Check if the absolute value of the numerator is less than the absolute value of the denominator
return Math.abs(numerator) < Math.abs(denominator);
}

module.exports = isProperFraction;
module.exports = isProperFraction;
6 changes: 2 additions & 4 deletions Sprint-3/2-mandatory-rewrite/3-get-card-value.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
function getCardValue(card) {
// replace with your code from key-implement
return 11;
}
function getCardValue(card) {
const rank = card.slice(0, -1); // strip off the suit emoji

Expand All @@ -15,3 +11,5 @@ function getCardValue(card) {

throw new Error("Invalid card rank");
}

module.exports = getCardValue;
35 changes: 15 additions & 20 deletions Sprint-3/2-mandatory-rewrite/3-get-card-value.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,18 @@ test("should return 11 for Ace of Spades", () => {
const aceofSpades = getCardValue("A♠");
expect(aceofSpades).toEqual(11);
});


// Case 2: Handle Number Cards (2-10)
test("should return 5 for 5 of Hearts", () => {
const five = getCardValue("5♥");
expect(five).toEqual(5);
test("should return the value of number cards (2-10)", () => {
const testCases = [
["2♣", 2],
["5♠", 5],
["10♦", 10]
];
testCases.forEach(([card, expected]) => {
expect(getCardValue(card)).toEqual(expected);
});
});

test("should return 10 for 10 of Diamonds", () => {
Expand All @@ -16,25 +24,12 @@ test("should return 10 for 10 of Diamonds", () => {
});

// Case 3: Handle Face Cards (J, Q, K)
test("should return 10 for Jack of Spades", () => {
const jack = getCardValue("J♠");
expect(jack).toEqual(10);
});

test("should return 10 for Queen of Clubs", () => {
const queen = getCardValue("Q♣");
expect(queen).toEqual(10);
test("should return 10 for face cards J, Q, K", () => {
expect(getCardValue("J♠")).toEqual(10);
expect(getCardValue("Q♣")).toEqual(10);
expect(getCardValue("K♥")).toEqual(10);
});

test("should return 10 for King of Hearts", () => {
const king = getCardValue("K♥");
expect(king).toEqual(10);
});

// Case 4: Handle Ace
const AceCard = getCardValue("A");
assertEquals(AceCard ("A"), 11);

// Case 5: Handle Invalid Cards
test("should throw error for invalid card Z♠", () => {
expect(() => getCardValue("Z♠")).toThrow("Invalid card rank");
Expand Down
6 changes: 0 additions & 6 deletions Sprint-3/3-mandatory-practice/implement/count.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
function countChar(stringOfCharacters, findCharacter) {
return 5
}

module.exports = countChar;

function countChar(stringOfCharacters, findCharacter) {
let count = 0;
for (let char of stringOfCharacters) {
Expand Down
3 changes: 2 additions & 1 deletion Sprint-3/3-mandatory-practice/implement/count.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// implement a function countChar that counts the number of times a character occurs in a string
const countChar = require("./count");

// Given a string str and a single character char to search for,
// When the countChar function is called with these inputs,
// Then it should:
Expand All @@ -9,6 +9,7 @@ const countChar = require("./count");
// 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').
const countChar = require("./count");

test("should count multiple occurrences of a character", () => {
const str = "aaaaa";
Expand Down
38 changes: 17 additions & 21 deletions Sprint-3/3-mandatory-practice/implement/get-ordinal-number.test.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,35 @@
const getOrdinalNumber = require("./get-ordinal-number");
// In this week's prep, we started implementing getOrdinalNumber

// continue testing and implementing getOrdinalNumber for additional cases
// Write your tests using Jest - remember to run your tests often for continual feedback

// Case 1: Identify the ordinal number for 1
// When the number is 1,
// Then the function should return "1st"

const getOrdinalNumber = require("./getOrdinalNumber");

test("should return 1st", () => {
expect(getOrdinalNumber(1)).toEqual("1st");
});

test("should return 2nd", () => {
expect(getOrdinalNumber(2)).toEqual("2nd");
test("returns 1st for 1", () => {
expect(getOrdinalNumber(1)).toBe("1st");
});

test("should return 3rd", () => {
expect(getOrdinalNumber(3)).toEqual("3rd");
test("returns 2nd for 2", () => {
expect(getOrdinalNumber(2)).toBe("2nd");
});

test("should return 8th", () => {
expect(getOrdinalNumber(8)).toEqual("8th");
test("returns 3rd for 3", () => {
expect(getOrdinalNumber(3)).toBe("3rd");
});

test("should return 19th", () => {
expect(getOrdinalNumber(19)).toEqual("19th");
test("returns 4th for 4", () => {
expect(getOrdinalNumber(4)).toBe("4th");
});

test("should return 81st", () => {
expect(getOrdinalNumber(81)).toEqual("81st");
test("Handle the special case for teens (11, 12, 13)", () => {
expect(getOrdinalNumber(11)).toBe("11th");
expect(getOrdinalNumber(12)).toBe("12th");
expect(getOrdinalNumber(13)).toBe("13th");
});

test("should return 125th", () => {
expect(getOrdinalNumber(125)).toEqual("125th");
test("Get the last digit to determine suffix", () => {
expect(getOrdinalNumber(21)).toBe("21st");
expect(getOrdinalNumber(22)).toBe("22nd");
expect(getOrdinalNumber(23)).toBe("23rd");
expect(getOrdinalNumber(101)).toBe("101st");
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
function getOrdinalNumber(num) {
return "1st";
}
function getOrdinalNumber(num) {
const lastDigit = num % 10;
const lastTwoDigits = num % 100;
Expand All @@ -20,5 +17,4 @@ function getOrdinalNumber(num) {
return num + "th";
}
}

module.exports = getOrdinalNumber;
12 changes: 5 additions & 7 deletions Sprint-3/3-mandatory-practice/implement/repeat.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
function repeat() {
return "hellohellohello";
}

module.exports = repeat;
function repeat(str = "", count = 1) {
if (count < 0) {
throw new Error("Invalid count");
}

function repeat(word = "hello", times = 3) {
return word.repeat(times);
return str.repeat(count);
}

module.exports = repeat;
3 changes: 1 addition & 2 deletions Sprint-3/3-mandatory-practice/implement/repeat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ test("should repeat the string count times", () => {
// Given a target string str and a negative integer count,
// When the repeat function is called with these inputs,
// Then it should throw an error or return an appropriate error message, as negative counts are not valid.
const repeat = require("./repeat");

// Case: Repeat string multiple times
test("should repeat the string count times", () => {
Expand Down Expand Up @@ -60,5 +59,5 @@ test("should return an empty string if count is 0", () => {
test("should throw an error if count is negative", () => {
const str = "hello";
const count = -2;
expect(() => repeat(str, count)).toThrow("Invalid ");
expect(() => repeat(str, count)).toThrow("Invalid count");
});
32 changes: 32 additions & 0 deletions Sprint-3/4-stretch-investigate/card-validator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
function validateCreditCard(cardNumber) {
// Check length and numeric characters
if (cardNumber.length !== 16 || !/^\d{16}$/.test(cardNumber)) {
return false;
}

// Check at least two different digits
const uniqueDigits = new Set(cardNumber);
if (uniqueDigits.size < 2) {
return false;
}

// Check last digit is even
const lastDigit = Number(cardNumber[cardNumber.length - 1]);
if (lastDigit % 2 !== 0) {
return false;
}

// Check sum of digits
let digitSum = 0;
for (let digit of cardNumber) {
digitSum += Number(digit);
}
if (digitSum <= 16) {
return false;
}

// If all checks passed, return true
return true;
}

module.exports = validateCreditCard;
31 changes: 0 additions & 31 deletions Sprint-3/4-stretch-investigate/card-validator.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,36 +34,5 @@ These are the requirements your project needs to fulfill:

Good luck!

function validateCreditCard(cardNumber) {
// cardNumber = cardNumber.replace(/\D/g, "");
if (cardNumber.length !== 16 || !/^\d{16}$/.test(cardNumber)) {
return false;
}

// Check: Must contain at least 2 different digits
const sameDigits = new Set(cardNumber);
if (sameDigits.size < 2) {
return false;
}

// Check: Last digit must be even
const lastDigit = Number(cardNumber[cardNumber.length - 1]);
if (lastDigit % 2 !== 0) {
return false;
}

// let digitSum = 0;
for (let digit of strNum) {
digitSum += Number(digit);
} // Convert each character to a number and add it to the sum
if (digitSum <= 16) {
return false;
}

return true; // All validation rules passed
}

// All checks passed; valid card
return true;

module.exports = validateCreditCard;
50 changes: 20 additions & 30 deletions Sprint-3/4-stretch-investigate/password-validator.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,29 @@
function passwordValidator(password) {
return password.length < 5 ? false : true
}
function passwordValidator(password) {
return password.length >= 5;
}
// Checks whether the password variable is a string
if (typeof password !== 'string') return false;
const previousPasswords = ["Password123!", "Welcome1$", "Admin#2023"]; // example previous passwords list

// Check length is at least a maximum of 5 characters long
if (password.length < 5) return true;

// Check for at least one uppercase letter A-Z
if (!/[A-Z]/.test(password)) return false;
function passwordValidator(password) {
// Check that password is a string
if (typeof password !== "string") return false;

// Check for at least one lowercase letter a-z
if (!/[a-z]/.test(password)) return false;
// Check minimum length
if (password.length < 5) return false;

// Check for at least one number 0-9
if (!/[0-9]/.test(password)) return false;
// Check for at least one uppercase letter
if (!/[A-Z]/.test(password)) return false;

// Check for at least one allowed special character: ! # $ % . * &
if (!/[!#$%.*&]/.test(password)) return false;
// Check for at least one lowercase letter
if (!/[a-z]/.test(password)) return false;

// Check that password is not in previousPasswords array
if (previousPasswords.includes(password)) return false;
// Check for at least one digit
if (!/[0-9]/.test(password)) return false;

// If all checks pass, return true
return true;
// Check for at least one special character from allowed set
if (!/[!#$%.*&]/.test(password)) return false;

// Check password not previously used
if (previousPasswords.includes(password)) return false;

// If all checks pass, password is valid
return true;
}

const hasUppercase = /[A-Z]/.test(password);
const hasLowercase = /[a-z]/.test(password);
const hasDigit = /[0-9]/.test(password);
const hasSpecialChar = /[!@#$%^&*(),.?":{}|<>]/.test(password);
const hasNoSpaces = !/\s/.test(password);

module.exports = passwordValidator;
module.exports = passwordValidator;
Loading