-
-
Notifications
You must be signed in to change notification settings - Fork 73
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
CYF ITP South Africa - Cape Town | Emmanuel Siziba | Module-Structuring-and-Testing-Data | Week 3 #217
base: main
Are you sure you want to change the base?
CYF ITP South Africa - Cape Town | Emmanuel Siziba | Module-Structuring-and-Testing-Data | Week 3 #217
Changes from all commits
0455567
a5805ac
49b57b0
23eb70b
6408987
ed96256
9cc7315
d8f1af0
babde67
7b610f9
aab6e2e
968ddf8
0537d6f
0e9c76e
699b87f
cc6526e
11783cb
4fbab41
099c1aa
2a4f69a
487fb5c
9e9ac0c
66706d0
909cc91
83d1fb8
bbac933
3da5a95
6cab33c
1e17560
de9797e
0547e6d
d79ad1c
25b1091
c5fbc33
7867ccf
2cf59b6
aeda383
da7f3cc
a5ff669
7a365d0
2e79cba
4d5e0a7
1c7e88b
9c2bed0
5618c98
e1fd233
8a32c3f
660da3e
2d225ca
dcf6ce9
3e307a3
5e6d56f
3603ef8
2f4f386
293e59f
9830f5b
e94ff41
7dc7129
5d6a07d
806bfcd
70d037e
8877358
5d38bd1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
// Implement a function getAngleType | ||
i// Implement a function getAngleType | ||
|
||
// Acceptance criteria: | ||
|
||
|
@@ -25,3 +25,31 @@ | |
// 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"; | ||
} | ||
|
||
if(angle < 90){ | ||
return "Acute angle"; | ||
} | ||
if (angle < 180){ | ||
return "Obtuse angle"; | ||
} | ||
|
||
if(angle === 180){ | ||
return "Straight angle" | ||
} | ||
|
||
if(angle > 180 && angle < 360){ | ||
return "Reflex angle" | ||
} | ||
|
||
else { | ||
return "Invalid angle" | ||
} | ||
} | ||
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. If it were up to you, what do think the function should return when angle is >= 360? 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. It will give us an angle number equal to or greater than 360. Honestly, I was confused here because I just couldn't figure a way to code this statement " angle is greater than 180 degrees and less than 360 degrees" so I thought I would just focus on the last part that says less than 360 degrees but I see that was only half of the answer please check I fixed my if statement. 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. Your previous code is correct because the previous if statements have eliminated all the cases where What I was asking is that, what if 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 have put in the else statement with the corresponding test case |
||
module.exports = getAngleType; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
const getAngleType = require('./get-angle-type') | ||
|
||
test('Returns "Right angle" when the angle is exactly 90 degrees', () => { | ||
expect(getAngleType(90)).toBe('Right angle'); | ||
}); | ||
|
||
test('Returns "Acute" when the angle is less than 90 degrees', () => { | ||
expect(getAngleType(45)).toBe('Acute angle'); | ||
}); | ||
|
||
test('Returns "Obtuse angle" when the angle is greater than 90 degrees but less than 180 degrees', () => { | ||
expect(getAngleType(120)).toBe('Obtuse angle'); | ||
}); | ||
|
||
|
||
test('Returns "Straight angle" when the angle is exactly 180 degrees', () => { | ||
expect(getAngleType(180)).toBe('Straight angle'); | ||
}); | ||
|
||
|
||
test('Returns "Reflex angle" when the angle is greater than 180 degrees but less than 360 degrees', () => { | ||
expect(getAngleType(220)).toBe('Reflex angle'); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
const getCardValue = require('./get-card-value') | ||
|
||
test('getCardValue returns 11 for Ace', () => { | ||
expect(getCardValue('A♠')).toBe(11); | ||
}); | ||
|
||
|
||
test('getCardValue returns 10 for J, Q or K', () => { | ||
expect(getCardValue('J♠')).toBe(10); | ||
expect(getCardValue('Q♠')).toBe(10); | ||
expect(getCardValue('K♠')).toBe(10); | ||
}); | ||
|
||
|
||
test('getCardValue returns parseInt(rank) for 2 & 9', () => { | ||
expect(getCardValue('2')).toBe(parseInt('2')); | ||
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. Need to always include a suite character (or any character) as the last character in the "card string". Also, "10" is a valid card rank. 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. is this what you are saying i must do : 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. The function only needs to check the rank value and can assume the last character is a valid suite character.
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 have fixed it |
||
expect(getCardValue('9')).toBe(parseInt('9')); | ||
}); | ||
|
||
test('getCardValue throws error for invalid rank', () => { | ||
expect(() => getCardValue('A♠♠')).toThrow('Invalid card rank.'); | ||
expect(() => getCardValue('Q$')).toThrow('Invalid card rank.'); | ||
expect(() => getCardValue('K&')).toThrow('Invalid card rank.'); | ||
|
||
|
||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
const isProperFraction = require('./is-proper-fraction'); | ||
|
||
test('return true for a proper function', () => { | ||
expect(isProperFraction(2, 3)).toBe(true); | ||
}); | ||
|
||
test('returns an error for an improper function', () => { | ||
expect(() => isProperFraction(3, 0)).toThrow('Denominator cannot be zero'); | ||
}); | ||
|
||
test('return false for a improper function', () => { | ||
expect(isProperFraction(5, 2)).toBe(false); | ||
}); | ||
|
||
test('return true for a proper function', () => { | ||
expect(isProperFraction(4, 7)).toBe(true); | ||
}); | ||
|
||
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. What do you expect from the following function calls?
Suggestion: Look up "Absolute Value" 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. isProperFraction(4, -7); I expect this one to be true So the issue with these function calls is that they did not use Absolute values is that right? Absolute value is the distance of a number from zero on a number line. Regardless of whether the number is positive or negative, 1 its absolute value is always positive. This is the definition i found. So this means i have to remove the nagative sign in my code and use an Absolute value. 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. Correct. You still need to make changes to your code. |
||
test('return false for a improper function', () => { | ||
expect(isProperFraction(3, 3)).toBe(false); | ||
}); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,3 +33,14 @@ | |
// 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 <= 0 || b <= 0 || c <= 0) { | ||
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. Is it necessary to include the if-statement at lines 37-39? I will not go into details why in some programming languages (but not JavaScript) we need also to check if a, b, c are positives. The main point I would like to make is, you should fully understand what you wrote in your code. An interviewer may ask you questions like what I am asking here, and it would reflect poorly on you if you cannot explain your code. 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. My thought process while approaching this was since they said we must validate and invalidate our triangle, the only way to do this was through an If statement. Are you saying there's another way of solving this without an If statement? 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. The main point I would like to make is, you should fully understand what you wrote in your code. An interviewer may ask you questions like what I am asking here, and it would reflect poorly on you if you cannot explain your code. Thank you for this. I am at the point now in this journey whereby I can read a problem and immediately my mind starts to generate solutions. I have solved problems on code wars, that I didn't grasp their concept. i just knew what I had to do So my problem is explaining what I am doing or just purely explaining my logic to someone else that's a real struggle but I am grateful for the comment you made. Instead of emphasizing writing the code, will place my emphasis on understanding what is needed, and what my code does and learn to explain my logic to others. |
||
return false; | ||
} | ||
else if ((a + b > c) && (a + c > b) && (b + c > a)) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
module.exports = isValidTriangle; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
const isValidTriangle = require('./is-valid-triangle'); | ||
|
||
test('validates a triangle where the sum of any two sides is always greater than the third side', () => { | ||
|
||
expect(isValidTriangle('a, b, c')).toBe(true); | ||
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. If you were to call your 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. heres how i would write it : function isValidTriangle(a, b, c) { const result = isValidTriangle(a, b, c); the mistake I made here was it the fact that I didn't plug in any values in the test case file submission. 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. But that's not how you call the function in 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. Okay so I would have to say : let result isValidTriangle(1, 1, 1); |
||
}); | ||
|
||
//test('returns false when the sum of any two side lengths is less than or equal to the length of the third side', () => { | ||
|
||
// expect(isValidTriangle('a, b, c')).toBe(false); | ||
//}); | ||
|
||
//test('returns false when the sides are less than or equal to zero', () => { | ||
|
||
// expect(isValidTriangle('a, b, c')).toBe(false); | ||
//}); | ||
|
||
//test('returns true when the sides are the sum of any two sides is greater than the third side', () => { | ||
|
||
// expect(isValidTriangle('a, b, c')).toBe(true); | ||
//}); | ||
// I can not seem to make this test pass |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,28 +16,41 @@ | |
// Given a lowercase letter character and a positive integer shift, | ||
// When the function is called with these inputs, | ||
// Then it should rotate the lowercase letter by shift positions within the lowercase alphabet, wrapping around if necessary, and return the rotated lowercase letter as a string. | ||
console.log(rotateCharacter("a", 3)); // Output: "d" | ||
console.log(rotateCharacter("f", 1)); // Output: "g" | ||
//console.log(rotateCharacter("a", 3)); // Output: "d" | ||
//console.log(rotateCharacter("f", 1)); // Output: "g" | ||
|
||
// Scenario: Rotate Uppercase Letters: | ||
// Given an uppercase letter character and a positive integer shift, | ||
// When the function is called with these inputs, | ||
// Then it should rotate the uppercase letter by shift positions within the uppercase alphabet, wrapping around if necessary, and return the rotated uppercase letter as a string. | ||
console.log(rotateCharacter("A", 3)); // Output: "D" | ||
console.log(rotateCharacter("F", 1)); // Output: "G" | ||
//console.log(rotateCharacter("A", 3)); // Output: "D" | ||
//console.log(rotateCharacter("F", 1)); // Output: "G" | ||
|
||
// Scenario: Leave Non-Letter Characters Unchanged: | ||
// Given a character that is not a letter (neither uppercase nor lowercase) and any positive or negative shift value, | ||
// When the function is called with these inputs, | ||
// Then it should return the character unchanged. | ||
// This specification outlines the behavior of the rotateCharacter function for different input scenarios, including valid and invalid characters, and defines the expected output or action for each case. | ||
console.log(rotateCharacter("7", 5)); // Output: "7" (unchanged, not a letter) | ||
//console.log(rotateCharacter("7", 5)); // Output: "7" (unchanged, not a letter) | ||
|
||
// Scenario: Shifting a Character with Wraparound | ||
// Given a character char within the lowercase alphabet range (e.g., 'z') or the uppercase alphabet range (e.g., 'Z'), | ||
// And a positive integer shift that causes the character to wrap around the alphabet when rotated (e.g., a shift of 3 for 'z' or 'Z'), | ||
// When the rotateCharacter function is called with char and shift as inputs, | ||
// Then it should correctly rotate the character by shift positions within the alphabet while handling the wraparound, | ||
// 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) | ||
//console.log(rotateCharacter("z", 1)); // Output: "a" (preserves case, but wraps around) | ||
//console.log(rotateCharacter("Y", 2)); // Output: "A" (preserves case, but wraps around) | ||
|
||
|
||
|
||
function rotateCharacter(char, shift) { | ||
const isLowercase = char.toLowerCase() === 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. Characters in other languages also have lowercase/uppercase alphabets. For example So you probably need a better way to ensure the character is one of the 26 English alphabets. 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 was able to fix it 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 was able to fix it 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 expression, |
||
const baseCode = isLowercase ? 'a'.charCodeAt(0) : 'A'.charCodeAt(0); | ||
const charCode = char.charCodeAt(0); | ||
|
||
let rotatedCode = ((charCode - baseCode + shift) % 26) + baseCode; | ||
return String.fromCharCode(rotatedCode); | ||
} | ||
|
||
module.exports = rotateCharacter; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
const rotateCharacter = require('./rotate-char'); | ||
|
||
test('rotates a lowercase letter by the given shift', () => { | ||
expect(rotateCharacter('a', 3)).toBe('d'); | ||
}); | ||
|
||
test('rotates a lowercase letter with negative shift', () => { | ||
expect(rotateCharacter('d', -3)).toBe('a'); | ||
}); | ||
|
||
test('rotates an uppercase letter by the given shift', () => { | ||
expect(rotateCharacter('A', 3)).toBe('D'); | ||
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. Can also try some huge shift value. For example,
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. okay i tried it and it passed |
||
}); | ||
|
||
test('rotates an uppercase letter with negative shift', () => { | ||
expect(rotateCharacter('D', -3)).toBe('A'); | ||
}); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,3 +33,20 @@ 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! | ||
|
||
|
||
//Answer: Breakdown | ||
touch command to create file cardValidator.js | ||
define a function cardValidator(cardNumber16digits){} | ||
// this function checks if the card number entered by a user is valid. | ||
// a valid card number has the following parameters: 16 digits, ends in even number so use %2 if ==0 then even, if cardNumber sum > 16 card is valid. | ||
function ends in return boolean if card is valid(true) or invalid(false) */ | ||
|
||
|
||
|
||
function cardValidator(cardNumber){ | ||
// this function checks if the card number entered by a user is 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 good start. Would you try to complete the implementation of this function and its tests? |
||
// a valid card number has the following parameters: 16 digits, ends in even number so use %2 if ==0 then even, if cardNumber sum > 16 card is valid. | ||
// a conditional statement to say if the cardNumber is true or false | ||
return validCard //this is a true or false to show if the card is valid | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
function countChar(str, char) { | ||
let count = 0; | ||
for (let i = 0; i < str.length; i++){ | ||
if (str[i] === char) { | ||
count++; | ||
} | ||
} | ||
return count; | ||
} | ||
|
||
module.exports = countChar; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
function getOrdinalNumber(number) { | ||
if (number === 1) { | ||
return number + "st"; | ||
} else if (number === 2) { | ||
return number + "nd"; | ||
} else if (number === 3) { | ||
return number + "rd"; | ||
} | ||
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. Ordinal number of 101 is "101st", and ordinal number of 11 is "11th". May I suggest using Google or ChatGPT to find out the rules for constructing an ordinal number from an integer? 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 function is still not yet implemented correctly. |
||
} | ||
|
||
module.exports = getOrdinalNumber; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,26 @@ | ||
// In this week's prep, we started implementing getOrdinalNumber | ||
|
||
// continue testing and implementing getOrdinalNumber for additional cases | ||
//Continue testing and implementing getOrdinalNumber for additional cases | ||
// Write your tests using Jest - remember to run your tests often for continual feedback | ||
|
||
const getOrdinalNumber = require('./get-ordinal-number'); | ||
|
||
test("Expect 'st' as suffix when the last digit is 1 but the last two digits are not 11, 12, or 13", () => { | ||
expect(getOrdinalNumber(1)).toBe("1st"); | ||
expect(getOrdinalNumber(21)).toBe("21st"); | ||
}); | ||
|
||
test("Expect 'nd' as suffix when the last digit is 2 but the last two digits are not 12", () => { | ||
expect(getOrdinalNumber(2)).toBe("2nd"); | ||
expect(getOrdinalNumber(22)).toBe("22nd"); | ||
}); | ||
|
||
test("Expect 'rd' as suffix when the last digit is 3 but the last two digits are not 13", () => { | ||
expect(getOrdinalNumber(3)).toBe("3rd"); | ||
expect(getOrdinalNumber(23)).toBe("23rd"); | ||
}); | ||
|
||
test("Expect 'th' as suffix for all other cases", () => { | ||
expect(getOrdinalNumber(4)).toBe("4th"); | ||
expect(getOrdinalNumber(11)).toBe("11th"); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
function isPrime(num) { | ||
|
||
if (num <= 1) return false; | ||
if (num === 2) return true; | ||
|
||
const sqrtNum = Math.sqrt(num); | ||
for (let i = 3; i <= sqrtNum; i += 2) { | ||
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 good. |
||
if (num % i === 0) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
module.exports = isPrime; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,17 @@ | ||
// Given a positive integer num, | ||
// When the isPrime function is called with num as input, | ||
// Then it should check if the num is prime | ||
|
||
const isPrime = require('./is-prime'); | ||
|
||
test('checks if 7 is a prime number', () => { | ||
expect(isPrime(7)).toBe(true); | ||
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. You should test more numbers (especially those special cases and boundary cases like 0, 1, 2) in this script to make the test comprehensive. 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. okay, i was able to add more test numbers |
||
expect(isPrime(0)).toBe(false); | ||
expect(isPrime(1)).toBe(false); | ||
expect(isPrime(2)).toBe(true); | ||
expect(isPrime(3)).toBe(true); | ||
expect(isPrime(5)).toBe(true); | ||
expect(isPrime(6)).toBe(false); | ||
expect(isPrime(23)).toBe(true); | ||
expect(isPrime(24)).toBe(false); | ||
}); |
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.
Technically, 0 < acute angle < 90. Since the spec didn't specify, we can assume angle is always a positive number.
You don't have to change your 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.
okay so you are saying since an acute angle is any angle that measures form 0 to 90, I should leave my solution as it is .
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.
I was saying you can leave your solution as it is because the spec didn't say if the function needs to deal with angles that are negatives or angles that are more than or equal to 360.
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.
I have put in the else statement with the corresponding test case