-
-
Notifications
You must be signed in to change notification settings - Fork 76
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 | Dawud Vermeulen | Module-Structuring-and-Testing-Data | Week 3 #216
base: main
Are you sure you want to change the base?
CYF ITP South Africa - Cape Town | Dawud Vermeulen | Module-Structuring-and-Testing-Data | Week 3 #216
Changes from all commits
577475a
99b4369
57f8a4c
38bcd17
8b70aaa
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module.exports = function getAngleType(angle) { | ||
if (angle === 90) return 'Right angle'; | ||
if (angle < 90) return 'Acute 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. Bear in mind that the code is executed line-by-line, so the function will return 'acute angle' for anything less than or equal to 0. How would you remedy this? |
||
if (angle > 90 && angle < 180) return 'Obtuse 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. While this is not wrong, note that code often doesn't need everything written in explicit detail and can interpret things based on implications, so it's best to avoid redundancy to keep the code as neat and succinct as possible. Do you see which boolean in the above line of code and one of the lines below may be redundant? |
||
if (angle === 180) return 'Straight angle'; | ||
if (angle > 180 && angle < 360) return 'Reflex angle'; | ||
if (angle <= 0 || angle >= 360) return 'Who you trying to fool Flat-Earther?!' | ||
} | ||
|
||
//this was the first time i followed the TDD approach and started with tests then the function. the approach works for me cause its like i know what the function must do, its more clear. | ||
// i needed Babel to help me with the es6 as jest only uses commonJS and i ran into alot of problems getting my tests to run but in the end i discovered Babel and learnt alot of extras. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
const getAngleType = require('./angle'); | ||
//its still js so i should be able to comment in this file. i wrote specific tests and followed the boiler plate from the site. | ||
describe('getAngleType', () => { | ||
it('identifies right angles', () => { | ||
expect(getAngleType(90)).toBe('Right angle'); | ||
}); | ||
it('identifies acute angles', () => { | ||
expect(getAngleType(45)).toBe('Acute angle'); | ||
}); | ||
it('identifies obtuse angles', () => { | ||
expect(getAngleType(120)).toBe('Obtuse angle'); | ||
}); | ||
it('identifies straight angles', () => { | ||
expect(getAngleType(180)).toBe('Straight angle'); // Fixed the expected value | ||
}); | ||
it('identifies reflex angles', () => { | ||
expect(getAngleType(270)).toBe('Reflex angle'); | ||
}); | ||
it('identifies invalid angles', () => { | ||
expect(getAngleType(360)).toBe('Who you trying to fool Flat-Earther?!'); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module.exports = { | ||
presets: [['@babel/preset-env', {targets: {node: 'current'}}]], | ||
}; | ||
|
||
//my testing was not working cause my code was in es6 and jest speaks commonJS. i added babel to help solve this miscommunication. this code imports the function then tests. i started by writing this first to try the TDD approach. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* write a function called getCardValue(that takes in a string){ | ||
the function then matches that string to a object in the array of cards and returns the numeric value of the match. | ||
create a array of a deck of cards. cards 2-10 of any suite have the numeric value respective numeric values 2-10. face cards of any suite have the value 10. the Ace card of any suite has the numeric value 11. | ||
return the the int value of the card that the input string matched to. | ||
the input must have 1 numeric value of int 2 - 10 or a face card value of K,Q,J,A. the input string must also contain an emoji for the suit, valid emojis are: ♠️,♦️,♣️,♥️ | ||
} */ | ||
|
||
module.exports = function getCardValue(card) { | ||
const cardValues = { | ||
'2': 2, | ||
'3': 3, | ||
'4': 4, | ||
'5': 5, | ||
'6': 6, | ||
'7': 7, | ||
'8': 8, | ||
'9': 9, | ||
'10': 10, | ||
'J': 10, | ||
'Q': 10, | ||
'K': 10, | ||
'A': 11 | ||
}; | ||
|
||
// input validation | ||
if (typeof card !== 'string') { | ||
throw new Error('Invalid input: card must be a string'); | ||
} | ||
|
||
// Extract character until we hit one thats not a number | ||
const value = card.match(/^([0-9]+|[JQKA])/)?.[0]; | ||
// note the .match method works as expected with the string for the 1st test case. update with more test cases. | ||
if (!value || !(value in cardValues)) { | ||
throw new Error('Invalid input: invalid card value'); | ||
} | ||
|
||
// If we got here, the value is valid, so return its numeric value | ||
return cardValues[value]; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// write tests for testing the user inputs | ||
// input K♥️ expect return of int 10 | ||
// input Q expect return of a error cause no emoji for suit entered | ||
// input J♣️ expect return of int 10 | ||
//input 4♠️ expect return of int 4 | ||
// input A♦️ expect return of int 11 | ||
// input 1♣️ expect return of error message "Invalid entry" | ||
// input ♥️♥️ expect a return of error message "Invalid entry" | ||
|
||
const getCardValue = require('./card'); | ||
|
||
describe('getCardValue', () => { | ||
// write 1 test case just to check jest works. it is expected to fail. | ||
it('should return 10 for King of any suit', () => { | ||
expect(getCardValue('K♥️')).toBe(10); | ||
}); | ||
// Test Ace's | ||
test('should return 11 for Ace of any suit', () => { | ||
expect(getCardValue('A♠️')).toBe(11); | ||
expect(getCardValue('A♥️')).toBe(11); | ||
expect(getCardValue('A♦️')).toBe(11); | ||
expect(getCardValue('A♣️')).toBe(11); | ||
}); | ||
|
||
// Test face's | ||
test.each([ | ||
['J♠️', 10], | ||
['Q♥️', 10], | ||
['K♦️', 10], | ||
])('should return 10 for face card %s', (input, expected) => { | ||
expect(getCardValue(input)).toBe(expected); | ||
}); | ||
|
||
// Tests number cards | ||
test.each([ | ||
['2♠️', 2], | ||
['3♥️', 3], | ||
['4♦️', 4], | ||
['5♣️', 5], | ||
['6♠️', 6], | ||
['7♥️', 7], | ||
['8♦️', 8], | ||
['9♣️', 9], | ||
['10♠️', 10], | ||
])('should return %i for card %s', (input, expected) => { | ||
expect(getCardValue(input)).toBe(expected); | ||
}); | ||
|
||
// Test error cases | ||
|
||
test('should throw error for non-string input', () => { | ||
expect(() => getCardValue(42)).toThrow('Invalid input: card must be a string'); | ||
expect(() => getCardValue(null)).toThrow('Invalid input: card must be a string'); | ||
//expect(() => getCardValue('A')).toThrow('Invalid input: card must be a string'); // test case does not work. error with .toThrow method | ||
}); | ||
|
||
|
||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// here is my 1st attempt a the solution: | ||
/* | ||
function isProperFraction(numerator, denominator) { | ||
if (numerator < denominator) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
*/ | ||
// lets call this one v2 of the function | ||
function isProperFraction(numerator, denominator) { | ||
if (denominator === 0) { | ||
throw new Error("Denominator cannot be zero"); | ||
} | ||
|
||
if (Math.abs(numerator) < Math.abs(denominator)) { // v2 fails a test here. it should consider the absolute value of the numerator and denominator and so i changed it but lets get the syntax right. | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
module.exports = isProperFraction; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
const isProperFraction = require('./isProperFraction'); | ||
|
||
// i think i can improve these maybe but lets run it against the 1st iteration of the function (i already got a better slicker approach in mind) | ||
describe('isProperFraction', () => { | ||
test('identifies a basic proper fraction', () => { | ||
expect(isProperFraction(2, 3)).toBe(true); | ||
}); | ||
|
||
test('identifies an improper fraction', () => { | ||
expect(isProperFraction(5, 2)).toBe(false); | ||
}); | ||
|
||
test('throws error for zero denominator', () => { | ||
expect(() => isProperFraction(3, 0)).toThrow('Denominator cannot be zero'); //BUG v1 does not handle the user input error of a zero denominator. test fails. | ||
}); | ||
|
||
test('handles negative proper fraction', () => { | ||
expect(isProperFraction(-4, 7)).toBe(true); | ||
}); | ||
|
||
test('returns false when numerator equals denominator', () => { | ||
expect(isProperFraction(3, 3)).toBe(false); | ||
}); | ||
|
||
test('handles zero numerator', () => { | ||
expect(isProperFraction(0, 5)).toBe(true); | ||
}); | ||
|
||
test('works with large numbers', () => { | ||
expect(isProperFraction(99, 100)).toBe(true); | ||
expect(isProperFraction(100, 99)).toBe(false); | ||
}); | ||
|
||
test('handles negative denominator', () => { | ||
expect(isProperFraction(2, -7)).toBe(true); //BUG for the test of v1 of the function this fails and returns 'false'. | ||
expect(isProperFraction(-2, -7)).toBe(true); //on v2 this fails but its !=BUG=! cause i got it wrong it should be changed to 'true' as the input would evaluate to a positive fraction of 2 over 7. | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// this one feels tricky so the code is simple and easy to understand. i will research a better way for v2 but following my initial breakdown and using the ternary is the best i can do for v1 | ||
|
||
function rotateCharacter(char, shift) { | ||
// If != letter, return the char as is | ||
if (!/[a-zA-Z]/.test(char)) { | ||
return char; | ||
} | ||
|
||
// uppercase or lowercase? | ||
const isUpperCase = char === char.toUpperCase(); | ||
|
||
// Sets starting point of the alphabet | ||
const alphabet = isUpperCase //refactored cause extra var not necessary | ||
? 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' | ||
: 'abcdefghijklmnopqrstuvwxyz'; | ||
|
||
// Find index of the char in the alphabet | ||
const currentIndex = alphabet.indexOf(char); | ||
|
||
// Calculate the new index | ||
const newIndex = (currentIndex + shift + 26) % 26; | ||
|
||
return alphabet[newIndex]; | ||
} | ||
|
||
module.exports = rotateCharacter; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
//just copied the test cases from the spec | ||
const rotateCharacter = require('./rotateCharacter'); | ||
|
||
describe('rotateCharacter', () => { | ||
// Lowercase | ||
test('rotates lowercase letters', () => { | ||
expect(rotateCharacter("a", 3)).toBe("d"); | ||
expect(rotateCharacter("f", 1)).toBe("g"); | ||
}); | ||
|
||
// Uppercase | ||
test('rotates uppercase letters', () => { | ||
expect(rotateCharacter("A", 3)).toBe("D"); | ||
expect(rotateCharacter("F", 1)).toBe("G"); | ||
}); | ||
|
||
// Wraparound alphabet. this is a issue. | ||
test('handles wraparound for lowercase', () => { | ||
expect(rotateCharacter("z", 1)).toBe("a"); | ||
expect(rotateCharacter("x", 5)).toBe("c"); | ||
}); | ||
|
||
test('handles wraparound for uppercase', () => { | ||
expect(rotateCharacter("Z", 1)).toBe("A"); | ||
expect(rotateCharacter("Y", 2)).toBe("A"); | ||
}); | ||
|
||
// Non-letter | ||
test('leaves non-letter characters unchanged', () => { | ||
expect(rotateCharacter("7", 5)).toBe("7"); | ||
expect(rotateCharacter("!", 3)).toBe("!"); | ||
}); | ||
|
||
// Negative shifts. another area that needs attention. | ||
test('handles negative shifts', () => { | ||
expect(rotateCharacter("d", -1)).toBe("c"); | ||
expect(rotateCharacter("A", -1)).toBe("Z"); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
function cardValidator(cardNumber){ | ||
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. Where are the contents of the function? |
||
// 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. | ||
// 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 | ||
} |
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.
Not wrong, but is it really best practice to use only "if" statements in this instance?