Skip to content

CYF - London | Anna Fedyna | Module-Structuring-and-Testing-Data | Week 3 #91

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 4 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
29 changes: 29 additions & 0 deletions Sprint-3/implement/get-angle-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,35 @@

// Acceptance criteria:

function getAngleType(angle) {
if (typeof angle !== "number" || isNaN(angle)) {
return "Enter a valid number";
}
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";
} else {
return "Angle out of range";
}
}

test("check for Right angle", function () {
expect(getAngleType()).toEqual("Enter a valid input");
expect(getAngleType("")).toEqual("Enter a valid input");
expect(getAngleType(900)).toEqual("Angle out of range");
expect(getAngleType(90)).toEqual("Right angle");
expect(getAngleType(9)).toEqual("Acute angle");
expect(getAngleType(119)).toEqual("Obtuse angle");
expect(getAngleType(180)).toEqual("Straight angle");
});

// Given an angle in degrees,
// When the function getAngleType is called with this angle,
// Then it should:
Expand Down
24 changes: 23 additions & 1 deletion Sprint-3/implement/get-card-value.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,29 @@
// This problem involves playing cards: https://en.wikipedia.org/wiki/Standard_52-card_deck

// You will need to implement a function getCardValue

function getCardValue(card) {
if (card.length < 2 && card.length > 3) {
return "Enter proper input";
} else if (card.length === 2) {
let rank = card[0];
if (!isNaN(+rank) && +rank >= 2 && +rank <= 9) {
return 5;
} else if (rank === "J" || rank === "Q" || rank === "K") {
return 10;
} else if (rank === "A") {
return 11;
} else {
return "Invalid card rank.";
}
} else {
return 10;
}
}

console.assert(getCardValue("A♠") === 11, "1");
console.assert(getCardValue("K♠") === 10, "2");
console.assert(getCardValue("7♠") === 5, "3");
console.log(getCardValue("10♠") === 10, "4");
// You need to write assertions for your function to check it works in different cases

// Acceptance criteria:
Expand Down
25 changes: 25 additions & 0 deletions Sprint-3/implement/is-proper-fraction.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,31 @@
// You wil need to implement a function isProperFraction
// You need to write assertions for your function to check it works in different cases

function isProperFraction(numerator, denominator) {
if (
typeof numerator !== "number" ||
typeof denominator !== "number" ||
isNaN(numerator) ||
isNaN(denominator)
) {
return "Wrong input";
} else {
if (denominator === 0) {
return "Error";
} else if (denominator == numerator) {
return false;
} else {
return Math.abs(numerator) < Math.abs(denominator) ? true : false;
}
}
}

console.assert(isProperFraction(2, 3) === true, "test1");
console.assert(isProperFraction(-2, 3) === true, "test2");
console.assert(isProperFraction(-2, 0) === "Error", "test3");
console.assert(isProperFraction(-5, 3) === false, "test4");
console.assert(isProperFraction(3, 3) === false, "test5");

// Terms:
// Fractions: https://www.bbc.co.uk/bitesize/topics/zt9n6g8/articles/zjxpp4j
// Written here like this: 1/2 == Numerator/Denominator
Expand Down
37 changes: 37 additions & 0 deletions Sprint-3/implement/is-valid-triangle.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,41 @@
// Implement a function isValidTriangle

function isValidTriangle(a, b, c) {
if (
typeof a !== "number" ||
typeof b !== "number" ||
typeof c !== "number" ||
isNaN(a) ||
isNaN(b) ||
isNaN(c)
) {
return "Enter valid input";
} else if (a <= 0 || b <= 0 || c <= 0) {
return "Enter valid positive numbers for the sides of the triangle";
} else {
if (a + b > c && b + c > a && a + c > b) {
return "This is a valid triangle";
} else {
return "This is not a valid triangle";
}
}
}

console.assert(
isValidTriangle(1, 2, 3) === "This is not a valid triangle",
"Not valid"
);
console.assert(
isValidTriangle(-1, 3, 4) ===
"Enter valid positive numbers for the sides of the triangle",
"Negative sides"
);
console.assert(
isValidTriangle(3, 3, 3) === "This is a valid triangle",
"Valid"
);
console.assert(isValidTriangle(3, 3) === "Enter valid input", "Invalid input");

// Terms
// the Triangle Inequality says: the sum of any two sides is always greater than the third side.
// practical examples:
Expand Down
28 changes: 28 additions & 0 deletions Sprint-3/implement/rotate-char.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,33 @@
// This function is commonly used for text encryption and decryption,
// where shifting characters by a certain value can obscure their meaning or reveal hidden messages.



function rotateCharacter(char, shift) {
if (
typeof +char === "number" &&
!isNaN(char) &&
(+char !== 0 || char === "0")
) {
return char;
} else if (typeof char === "string" && char.length === 1) {
const acsiiCode = char.charCodeAt();
if (acsiiCode >= 65 && acsiiCode <= 90) {
return acsiiCode + shift <= 90
? String.fromCharCode(acsiiCode + shift)
: String.fromCharCode(acsiiCode + shift - 26);
} else if (acsiiCode >= 97 && acsiiCode <= 122) {
return acsiiCode + shift <= 122
? String.fromCharCode(acsiiCode + shift)
: String.fromCharCode(acsiiCode + shift - 26);
} else {
return "Wrong input, enter a letter"; // space case
}
} else {
return "Wrong input, enter a letter";
}
}

// Acceptance criteria:

// Given a character and a shift value,
Expand Down Expand Up @@ -41,3 +68,4 @@ console.log(rotateCharacter("7", 5)); // Output: "7" (unchanged, not a letter)
// 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)

14 changes: 14 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,17 @@
// 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 c of str) {
if (c === char) {
count++;
}
}
return count;
}
test("Count the number of times a character occurs in a string", function () {
expect(countChar("fndscn", "n")).toEqual(2);
expect(countChar("", "g")).toEqual(0);
});
24 changes: 24 additions & 0 deletions Sprint-3/revise/implement/credit-card-validator.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
function creditcardValidator(str) {
if (typeof str !== "string" || str.length !== 16) {
return false;
}
let sum = 0;
let obj = {};
for (let i = 0; i < 16; i++) {
if (typeof +str[i] !== "number" || isNaN(+str[i])) {
return false;
} else {
sum = sum + +str[i];
obj[+str[i]] = 1;
}
}
return (
sum > 16 && str[str.length - 1] % 2 == 0 && Object.keys(obj).length > 2
);
}

test("Check if card number valid", function () {
expect(creditcardValidator("1234567812345678")).toEqual(true);
expect(creditcardValidator("123456781234567g")).toEqual(false);
expect(creditcardValidator("1000000000000000")).toEqual(false);
});
22 changes: 22 additions & 0 deletions Sprint-3/revise/implement/get-ordinal-number.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,25 @@

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

function getOrdinalNumber(num) {
if (typeof num !== "number" && isNaN(num)) {
return "Enter valid input";
}
if (num === 1) {
return "1st";
} else if (num === 2) {
return "2nd";
} else if (num === 3) {
return "3rd";
} else {
return `${num}th`;
}
}

test("The ordinal number", function () {
expect(getOrdinalNumber(1)).toEqual("1st");
expect(getOrdinalNumber(2)).toEqual("2nd");
expect(getOrdinalNumber(3)).toEqual("3rd");
expect(getOrdinalNumber(13)).toEqual("13th");
});
16 changes: 16 additions & 0 deletions Sprint-3/revise/implement/is-prime.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
// 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) {
for (let i = 2; i < Math.sqrt(num); i++) {
if (num % i === 0) {
return false;
}
}
return true;
}

test("The ordinal number", function () {
expect(isPrime(1)).toEqual(true);
expect(isPrime(22)).toEqual(false);
expect(isPrime(33)).toEqual(false);
expect(isPrime(13)).toEqual(true);
});
37 changes: 37 additions & 0 deletions Sprint-3/revise/implement/password-validator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,40 @@ To be valid, a password must:

You must breakdown this problem in order to solve it. Find one test case first and get that working
*/

function passwordValidator(str, passwords) {
if (
typeof str != "string" ||
str.length < 5 ||
passwords.indexOf(str) != -1
) {
return false;
}
let upperCaseLetter = false;
let lowerCaseLetter = false;
let oneNumber = false;
let nonAlphanumericSymbol = false;
const symbols = ["!", "#", "$", "%", ".", "*", "&"];

for (let i = 0; i < str.length; i++) {
const char = str[i];

if (char >= "A" && char <= "Z") {
upperCaseLetter = true;
} else if (char >= "a" && char <= "z") {
lowerCaseLetter = true;
} else if (char >= "0" && char <= "9") {
oneNumber = true;
} else if (symbols.includes(char)) {
nonAlphanumericSymbol = true;
}
}
return (
upperCaseLetter && lowerCaseLetter && oneNumber && nonAlphanumericSymbol
);
}

test("Check for valid password", function () {
expect(passwordValidator("Asd123#", [])), toEqual(true);
expect(passwordValidator("Asd123#", ["Asd123#"])), toEqual(false);
});
20 changes: 20 additions & 0 deletions Sprint-3/revise/implement/repeat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,23 @@
// 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.

function repeat(str, count) {
if (count < 0 || typeof count !== "number") {
return "Error";
} else if (count === 0) {
return "";
} else {
let res = "";
for (let i = 0; i < count; i++) {
res += str;
}
return res;
}
}

test("Repeat the str count times", function () {
expect(repeat("hello", 0)).toEqual("");
expect(repeat("hello", 2)).toEqual("hellohello");
expect(repeat("hello", -5)).toEqual("Error");
});
4 changes: 4 additions & 0 deletions Sprint-3/revise/investigate/find.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ console.log(find("code your future", "z"));
// Pay particular attention to the following:

// a) How the index variable updates during the call to find
/* The index variable increase by 1 in each iteration of the loop until the target character is found or the end of the string */
// b) What is the if statement used to check
/* Checks whether the character at the current position indexis equal to the target character char */
// c) Why is index++ being used?
/* Increment the index variable */
// d) What is the condition index < str.length used for?
/* To ensure that the loop continues iterating through the string */