Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,19 @@
// execute the code to ensure all tests pass.

function getAngleType(angle) {
// TODO: Implement this function
if (angle > 0 && angle < 90) {
return "Acute angle";
} else if (angle === 90) {
return "Right 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 "Invalid angle";
}
}

// The line below allows us to load the getAngleType function into tests in other files.
Expand All @@ -35,3 +47,18 @@ function assertEquals(actualOutput, targetOutput) {
// Example: Identify Right Angles
const right = getAngleType(90);
assertEquals(right, "Right angle");

const acute = getAngleType(45);
assertEquals(acute, "Acute angle");

const obtuse = getAngleType(120);
assertEquals(obtuse, "Obtuse angle");

const straight = getAngleType(180);
assertEquals(straight, "Straight angle");

const reflex = getAngleType(270);
assertEquals(reflex, "Reflex angle");

const invalid = getAngleType(400);
assertEquals(invalid, "Invalid angle");
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
// execute the code to ensure all tests pass.

function isProperFraction(numerator, denominator) {
// TODO: Implement this function
if ( numerator < denominator && numerator > 0 && denominator > 0) {
return true;
}
return false;
}

// The line below allows us to load the isProperFraction function into tests in other files.
Expand All @@ -31,3 +34,8 @@ function assertEquals(actualOutput, targetOutput) {

// Example: 1/2 is a proper fraction
assertEquals(isProperFraction(1, 2), true);
assertEquals(isProperFraction(3, 4), true);
assertEquals(isProperFraction(5, 5), false);
assertEquals(isProperFraction(7, 3), false);
assertEquals(isProperFraction(0, 5), false);
assertEquals(isProperFraction(5, 0), false);
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,26 @@
// execute the code to ensure all tests pass.

function getCardValue(card) {
// TODO: Implement this function
if (card === "A♠" || card === "A♥" || card === "A♦" || card === "A♣") {
return 11;
}
if (card === "J♠" || card === "J♥" || card === "J♦" || card === "J♣") {
return 10;
}
if (card === "Q♠" || card === "Q♥" || card === "Q♦" || card === "Q♣") {
return 10;
}
if (card === "K♠" || card === "K♥" || card === "K♦" || card === "K♣") {
return 10;
}
// Check for number cards
const rank = card.slice(0, -1);
const value = parseInt(rank);
if (!isNaN(value) && value >= 2 && value <= 10) {
return value;
}
// If we reach here, the card is invalid
throw new Error("Invalid card format");
}

// The line below allows us to load the getCardValue function into tests in other files.
Expand All @@ -40,6 +59,10 @@ function assertEquals(actualOutput, targetOutput) {
// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
// Examples:
assertEquals(getCardValue("9♠"), 9);
assertEquals(getCardValue("A♠"), 11);
assertEquals(getCardValue("J♠"), 10);
assertEquals(getCardValue("Q♠"), 10);
assertEquals(getCardValue("K♠"), 10);

// Handling invalid cards
try {
Expand Down
2 changes: 1 addition & 1 deletion Sprint-3/2-practice-tdd/count.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function countChar(stringOfCharacters, findCharacter) {
return 5
return stringOfCharacters.split(findCharacter).length - 1;
}

module.exports = countChar;
7 changes: 7 additions & 0 deletions Sprint-3/2-practice-tdd/count.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ test("should count multiple occurrences of a character", () => {
expect(count).toEqual(5);
});

test("should count zero occurrences of a character", () => {
const str = "aaaaa";
const char = "b";
const count = countChar(str, char);
expect(count).toEqual(0);
});

// Scenario: No Occurrences
// Given the input string `str`,
// And a character `char` that does not exist within `str`.
Expand Down
4 changes: 3 additions & 1 deletion Sprint-3/2-practice-tdd/get-ordinal-number.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
function getOrdinalNumber(num) {
return "1st";
if (num % 10 === 1 && num % 100 !== 11) {
return num + "st";
}
}

module.exports = getOrdinalNumber;
33 changes: 28 additions & 5 deletions Sprint-3/2-practice-tdd/repeat-str.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Implement a function repeatStr
const repeatStr = require("./repeat-str");

// Given a target string `str` and a positive integer `count`,
// When the repeatStr function is called with these inputs,
// Then it should:
Expand All @@ -9,24 +9,47 @@ const repeatStr = require("./repeat-str");
// When the repeatStr function is called with these inputs,
// Then it should return a string that contains the original `str` repeated `count` times.

function repeatStr(str, count) {
if (count < 0) {
throw new Error("Count must be a non-negative integer");
} else if (count === 0) {
return "";
} else {
return str.repeat(count);
}
}

test("should repeat the string count times", () => {
const str = "hello";
const count = 3;
const repeatedStr = repeatStr(str, count);
expect(repeatedStr).toEqual("hellohellohello");
});

// Case: handle count of 1:
// Given a target string `str` and a `count` equal to 1,
// When the repeatStr function is called with these inputs,
// Then it should return the original `str` without repetition.
test("should return the original string when count is 1", () => {
const str = "hello";
const count = 1;
const repeatedStr = repeatStr(str, count);
expect(repeatedStr).toEqual("hello");
});


// Case: Handle count of 0:
// Given a target string `str` and a `count` equal to 0,
// When the repeatStr function is called with these inputs,
// Then it should return an empty string.
test("should return an empty string when count is 0", () => {
const str = "hello";
const count = 0;
const repeatedStr = repeatStr(str, count);
expect(repeatedStr).toEqual("");
});

// Case: Handle negative count:
// Given a target string `str` and a negative integer `count`,
// When the repeatStr function is called with these inputs,
// Then it should throw an error, as negative counts are not valid.
test("should throw an error for a negative count", () => {
expect(() => repeatStr("hello", -1)).toThrow();

});
4 changes: 2 additions & 2 deletions Sprint-3/3-dead-code/exercise-1.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ let testName = "Jerry";
const greeting = "hello";

function sayHello(greeting, name) {
const greetingStr = greeting + ", " + name + "!";

return `${greeting}, ${name}!`;
console.log(greetingStr);

}

testName = "Aman";
Expand Down
6 changes: 2 additions & 4 deletions Sprint-3/3-dead-code/exercise-2.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@
// The countAndCapitalisePets function should continue to work for any reasonable input it's given, and you shouldn't modify the pets variable.

const pets = ["parrot", "hamster", "horse", "dog", "hamster", "cat", "hamster"];
const capitalisedPets = pets.map((pet) => pet.toUpperCase());

const petsStartingWithH = pets.filter((pet) => pet[0] === "h");

function logPets(petsArr) {
petsArr.forEach((pet) => console.log(pet));
}


function countAndCapitalisePets(petsArr) {
const petCount = {};
Expand Down
6 changes: 4 additions & 2 deletions Sprint-3/4-stretch/card-validator.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ In this project you'll write a script that validates whether or not a credit car

Here are the rules for a valid number:

- Number must be 16 digits, all of them must be numbers.
- You must have at least two different digits represented (all of the digits cannot be the same).
- Number must be 16 digits, all of them must be numbers. checked
- You must have at least two different digits represented (all of the digits cannot be the same).
- The final digit must be even.
- The sum of all the digits must be greater than 16.

Expand Down Expand Up @@ -33,3 +33,5 @@ These are the requirements your project needs to fulfill:
- Return a boolean from the function to indicate whether the credit card number is valid.

Good luck!

function
39 changes: 39 additions & 0 deletions Sprint-3/4-stretch/credit-card-validator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
function isValidCreditCard(cardNumber) {
// Rule 1: Must be exactly 16 characters long
if (cardNumber.length !== 16) {
return false;
}

// Rule 2: Must contain only digits
if (!cardNumber.match(/^\d{16}$/)) {
return false;
}

// Rule 3: Cannot be made up of only one repeated digit
if (cardNumber.match(/^(.)\1+$/)) {
return false;
}

// Rule 4: Final digit must be even
const lastDigit = Number(cardNumber[cardNumber.length - 1]);

if (lastDigit % 2 !== 0) {
return false;
}

// Rule 5: Sum of all digits must be greater than 16
let sum = 0;

for (const digit of cardNumber) {
sum += Number(digit);
}

if (sum <= 16) {
return false;
}

// Passed every rule
return true;
}

module.exports = isValidCreditCard;
3 changes: 3 additions & 0 deletions Sprint-3/4-stretch/find.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,8 @@ console.log(find("code your future", "z"));

// a) How the index variable updates during the call to find
// b) What is the if statement used to check
// the if statement checks characters in the string
// c) Why is index++ being used?
// index++ is used to move to the next character in the string for the next iteration of the loop
// d) What is the condition index < str.length used for?
// index < str.length keeps the index within the string's valid range. If no match is found by the end, the function returns -1.
20 changes: 18 additions & 2 deletions Sprint-3/4-stretch/password-validator.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
function passwordValidator(password) {
return password.length < 5 ? false : true
if (password.length < 5) {
return false;
} else if (!/[A-Z]/.test(password)) {
return false;
} else if (!/[a-z]/.test(password)) {
return false;
} else if (!/[0-9]/.test(password)) {
return false;
} else if (!/[!#$%.*&]/.test(password)) {
return false;
} else {
return true;
}


}


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

// ("!", "#", "$", "%", ".", "*", "&")
10 changes: 8 additions & 2 deletions Sprint-3/4-stretch/password-validator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,16 @@ You must breakdown this problem in order to solve it. Find one test case first a
const isValidPassword = require("./password-validator");
test("password has at least 5 characters", () => {
// Arrange
const password = "12345";
const password = "2A4a#";
// Act
const result = isValidPassword(password);
// Assert
expect(result).toEqual(true);
}
);
);

test("password must have an uppercase letter", () => {
const password = "2a4a#";
const result = isValidPassword(password);
expect(result).toEqual(false);
});
Loading