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

NW-6 | AREEB-SATTAR | JS1| [TECH ED] Complete week 4 exercises | WEEK-4 #184

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
3,617 changes: 3,617 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "week-4-exercises",
"description": "Created this package for the JS1 week 4 exercises",
"scripts": {
"test":"jest"
},
"devDependencies": {
"jest": "^29.7.0"
}
}
23 changes: 0 additions & 23 deletions week-3/implement/get-angle-type.js

This file was deleted.

48 changes: 48 additions & 0 deletions week-3/implement/get-angle-type.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Implement a function getAngleType, and tests for each of the acceptance criteria.

const { default: expect } = require("expect");
const { get } = require("http");

// Acceptance criteria:

// Identify Right Angles:
// When the angle is exactly 90 degrees,
// Then the function should return "Right angle"

// Identify Acute Angles:
// When the angle is less than 90 degrees,
// Then the function should return "Acute angle"

// Identify Obtuse Angles:
// When the angle is greater than 90 degrees and less than 180 degrees,
// Then the function should return "Obtuse angle"

// Identify Straight Angles:
// When the angle is exactly 180 degrees,
// Then the function should return "Straight angle"

// 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 < 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 cannot be greater than 360 or less than 0";
}

test("This is to check if our function is returning the correct type of triangle based on given angle", function () {
expect(getAngleType(90)).toBe("Right Angle");
expect(getAngleType(180)).toBe("Straight Angle");
expect(getAngleType(45)).toBe("Acute Angle");
expect(getAngleType(120)).toBe("Obtuse Angle");
expect(getAngleType(190)).toBe("Reflex Angle");
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// This problem involves playing cards: https://en.wikipedia.org/wiki/Standard_52-card_deck

const { default: expect } = require("expect");

// You will need to implement a function getCardValue

// You need to write assertions for your function to check it works in different cases
Expand Down Expand Up @@ -29,3 +31,22 @@
// 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(str) {
if (str === "A") {
return 11;
} else if (str === "J" || str === "Q" || str === "K") {
return 10;
} else if (str > 1 && str < 11) {
return +str;
} else return "Invalid card rank";
}

test("Check if function is returning right value for BlackJack game", function () {

expect(getCardValue("A")).toBe(11);
expect(getCardValue("J")).toBe(10);
expect(getCardValue("K")).toBe(10);
expect(getCardValue("2")).toBe(2);
expect(getCardValue("5")).toBe(5);
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// You wil need to implement a function isProperFraction
// You need to write assertions for your function to check it works in different cases

const { log } = require("console");
const { default: expect } = require("expect");

// Terms:
// Fractions: https://www.bbc.co.uk/bitesize/topics/zt9n6g8/articles/zjxpp4j
// Written here like this: 1/2 == Numerator/Denominator
Expand Down Expand Up @@ -33,3 +36,31 @@
// 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(a, b) {
if (b === 0) {
return "error";
}
if (a === b) {
return false;
}
if (a < b) {
return true;
}
if (a > b) {
return false;
}
if (a < 0) {
return true;
}
}

test("Check if the fraction is a proper fraction or not ", function () {

expect(isProperFraction(4, 7)).toBe(true);
expect(isProperFraction(7, 7)).toBe(false);
expect(isProperFraction(-4, 7)).toBe(true);
expect(isProperFraction(4, 0)).toBe("error");
expect(isProperFraction(5, 2)).toBe(false);
});

Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Implement a function isValidTriangle

const { default: expect } = require("expect");

// 🗝️ Terms
// the Triangle Inequality says: the sum of any two sides is always greater than the third side.
// practical examples:
Expand Down Expand Up @@ -38,3 +40,22 @@
// 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){
if (a+b<=c || a+c<=b || b+c<=a){
return false;
}
else if (a<=0 || b<=0 || c<=0){
return false;
}
else if(a+b>c && a+c>b && b+c>a){
return true;
}
}

test("Check if the triangle is valid or not", function () {

expect(isValidTriangle(3, 3, 3)).toBe(true);
expect(isValidTriangle(3, -3, 3)).toBe(false);
expect(isValidTriangle(1, 2, 5)).toBe(false);
});
21 changes: 20 additions & 1 deletion week-4/implement/count.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
// implement a function countChar that counts the number of times a character occurs in a string

function countChar(str, char) {
let index = 0;
Copy link
Member

Choose a reason for hiding this comment

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

Can you think of a more descriptive name for this variable? We already have i which keeps track of the index

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

Choose a reason for hiding this comment

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

This is a well written code however can you explain this a bit? It seems a bit confusing to me, maybe if I understand what you have done here, it would make sense to me.

index++;
}
while (str[i + 1] === char) {
Copy link
Member

Choose a reason for hiding this comment

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

From my understanding, this loop will keep counting up and moving through the string if the next element in the string is the character we're counting. However if we just let the for loop run, it will already check every character in the string. So this while loop isn't doing much for us, just using the for loop will be easier to understand 🙂

i++;
index++;
}
}
return index;
}
test("This will check consecutive occurrences or no occurrences of a given character", function () {
expect(countChar("asad", "s")).toBe(1);
expect(countChar("assa", "s")).toBe(2);
expect(countChar("aaad", "s")).toBe(0);
expect(countChar("asssssd", "s")).toBe(5);
});
// 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 @@ -14,4 +33,4 @@
// Given the input string str,
// 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.
// Then it should return 0, indicating that no occurrences of the char were found in the case-sensitive str
14 changes: 14 additions & 0 deletions week-4/implement/get-ordinal-number.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
// In this week's prep, we started implementing getOrdinalNumber
function getOrdinalNumber(num) {
if (num % 10 === 1) {
Copy link
Member

Choose a reason for hiding this comment

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

Great use of the modulo % operator Areeb!

if (num === 11) {
return "11th";

Choose a reason for hiding this comment

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

I did mine a bit differently. Mine is a bit more concise, but this makes sense as well. Well done!

}
return `${num}st`;
}
}

test("works for any number ending in 1", function () {
expect(getOrdinalNumber(1)).toBe("1st");
expect(getOrdinalNumber(11)).toBe("11th");
expect(getOrdinalNumber(21)).toBe("21st");
});

// continue testing and implementing getOrdinalNumber for additional cases
// Write your tests using Jest - remember to run your tests often for continual feedback
23 changes: 23 additions & 0 deletions week-4/implement/is-prime.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
// Given a positive integer num,
// When the isPrime function is called with num as input,
// Then it should return a boolean representing whether the num is prime

function isPrime(num) {
if (num < 2) {
return false;
}
if (num != Math.round(num)) {
return false;
}
for (var i = 2; i < num; i++){
Copy link
Member

Choose a reason for hiding this comment

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

Great spot that you can begin this loop at 2! I would just replace var with let, as this helps to keep the variable scope as small as possible, in this case, only inside the for loop. Otherwise you would be able to access i outside of the loop which could cause bugs.

if(num%i===0)
return false;
}
return true;
}

test("Check if the number is a positive prime number", function () {

expect(isPrime(3)).toBe(true);
expect(isPrime(-7)).toBe(false);
expect(isPrime(0)).toBe(false);
expect(isPrime(17)).toBe(true);
expect(isPrime(3.5)).toBe(false);
});
23 changes: 23 additions & 0 deletions week-4/implement/repeat.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Implement a function repeat

const { default: expect } = require("expect");

// Given a target string str and a positive integer count,
// When the repeat function is called with these inputs,
// Then it should:
Expand All @@ -23,3 +25,24 @@
// 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 repeatString(str, num) {
if (num < 0) {
return "negative count is not valid";
}
let repeatedString = str.repeat(num);
if (num === 0) {
return "";
}
if (num === 1) {
return str;
} else return repeatedString;
Comment on lines +34 to +39
Copy link
Member

@JayMayer JayMayer Dec 11, 2023

Choose a reason for hiding this comment

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

Check what happens when you give str.repeat(..) the number 0 or 1. You might find that you can remove a few if statements from your function 😉

As a general rule of thumb, even if you don't need to repeat the string (if the number is 1, for example), as long as the built-in function still behaves as expected, it's fine to still use its output.

}

test("Check if the string is being repeated correctly", function () {
expect(repeatString("Ali", 2)).toBe("AliAli");
expect(repeatString("Ali", 5)).toBe("AliAliAliAliAli");
expect(repeatString("Ali", 0)).toBe("");
expect(repeatString("Ali", 1)).toBe("Ali");
expect(repeatString("Ali", -2)).toBe("negative count is not valid");
});

Choose a reason for hiding this comment

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

This is a well written code.

4 changes: 4 additions & 0 deletions week-4/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
//Ans. It adds 1 everytime the loop runs as index keep adding up until the input character has been reached
// b) What is the if statement used to check
//Ans. it's checking the value of certain index or string against the input character
// c) Why is index++ being used?
// Ans. It is being used to just add 1 to the value of index each time it has not reached the input character
// d) What is the condition index < str.length used for?
//Ans. As long as number of index is less than the length of the string.