-
-
Notifications
You must be signed in to change notification settings - Fork 50
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
base: main
Are you sure you want to change the base?
Changes from all commits
05330b8
5102fc4
fe54403
d6ba89a
caa7cf7
5b1439f
838130e
6eb108e
0704828
d12fdd1
94ced20
0f182bf
128e7f1
0b4446e
f8c0200
d094243
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
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" | ||
} | ||
} |
This file was deleted.
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,24 @@ | ||
// implement a function countChar that counts the number of times a character occurs in a string | ||
|
||
function countChar(str, char) { | ||
let index = 0; | ||
for (let i = 0; i < str.length; i++) { | ||
if (str[i] === char) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
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: | ||
|
@@ -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 |
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great use of the modulo |
||
if (num === 11) { | ||
return "11th"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
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++){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
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); | ||
}); |
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: | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check what happens when you give 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"); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a well written code. |
There was a problem hiding this comment.
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