-
-
Notifications
You must be signed in to change notification settings - Fork 116
LONDON_JAN25 | KHALIL ALI | STRUCTURING_AND_TESTING_DATA | SPRINT 3 | WEEK 6 #388
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
base: main
Are you sure you want to change the base?
Changes from 13 commits
81e3b58
b0d6861
d554fe0
4b83ace
619125a
7452251
f770750
2fdc4e3
a67b5a0
4ea450e
44a38f4
df074c1
9360469
2dd6e9c
8c20644
d1baa4b
d6da377
8579bc7
5d66d44
fa01d56
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,6 +1,9 @@ | ||
function isProperFraction(numerator, denominator) { | ||
if (numerator < denominator) return true; | ||
// add your completed function from key-implement here | ||
if (denominator === 0) return "Error: Denominator cannot be 0"; | ||
if (Math.abs(numerator) < denominator) return true; | ||
if (Math.abs(numerator) >= denominator) return false; | ||
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. We can also design the function to return Denominator can also be negative. Mathematically speaking, 2/-3 and -2/-3 are proper fractions. For any value that is not a proper fraction, we can just return 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 are right, |
||
} | ||
|
||
module.exports = isProperFraction; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,10 @@ | ||
function getCardValue(card) { | ||
// replace with your code from key-implement | ||
return 11; | ||
} | ||
let rank = card.slice(0,-1); | ||
if (rank === "A") return 11; | ||
if (rank === "10" || rank === "j" || rank === "Q" || rank === "K") return 10; | ||
if (rank >2 && rank <=9) return Number(rank); | ||
else throw new Error ("Invalid card rank"); | ||
} | ||
|
||
module.exports = getCardValue; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,15 @@ test("should return 11 for Ace of Spades", () => { | |
// Case 3: Handle Face Cards (J, Q, K): | ||
// Case 4: Handle Ace (A): | ||
// Case 5: Handle Invalid Cards: | ||
test("should return 5 for 5♥", () => { | ||
expect(getCardValue("5♥")).toEqual(5); | ||
}); | ||
test("should return 10 for Q♦", () => { | ||
expect(getCardValue("Q♦")).toEqual(10); | ||
}); | ||
test("should return 11 for A♣", () => { | ||
expect(getCardValue("A♣")).toEqual(11); | ||
}); | ||
test('should throw "Invalid card rank" for X♠', () => { | ||
expect(() => getCardValue("X♠")).toThrow("Invalid 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. Does your function return the value you expected from each of the following function calls?
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 fixed the bug. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,11 @@ | ||
function countChar(stringOfCharacters, findCharacter) { | ||
return 5 | ||
let count = 0; | ||
for (let i = 0; i < stringOfCharacters.length; i++) { | ||
if (stringOfCharacters[i] === findCharacter) { | ||
count++; | ||
} | ||
} | ||
return count; | ||
} | ||
|
||
module.exports = countChar; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,3 +22,9 @@ test("should count multiple occurrences of a character", () => { | |
// 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. | ||
test("should count multiple occurrences of a 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. This description does not seem to match the test specified. 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 fixed the description |
||
const str = "aaaaa"; | ||
const char = "b"; | ||
const count = countChar(str, char); | ||
expect(count).toEqual(0); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,18 @@ | ||
function getOrdinalNumber(num) { | ||
return "1st"; | ||
} | ||
if (num === 11 || num === 12 || num === 13) { | ||
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 ordinal numbers of 111, 312, 9913 are "111th", "312th", "9913th". 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 fixed the bug. |
||
return `${num}th`; | ||
} | ||
const lastDigit = num % 10; | ||
if (lastDigit === 1) { | ||
return `${num}st`; | ||
} | ||
if (lastDigit === 2) { | ||
return `${num}nd`; | ||
} | ||
if (lastDigit === 3) { | ||
return `${num}rd`; | ||
} | ||
return `${num}th`; | ||
} | ||
|
||
module.exports = getOrdinalNumber; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,3 +11,15 @@ const getOrdinalNumber = require("./get-ordinal-number"); | |
test("should return '1st' for 1", () => { | ||
expect(getOrdinalNumber(1)).toEqual("1st"); | ||
}); | ||
test("should return '2nd' for 2", () => { | ||
expect(getOrdinalNumber(2)).toEqual("2nd"); | ||
}); | ||
test("should return '3rd' for 3", () => { | ||
expect(getOrdinalNumber(3)).toEqual("3rd"); | ||
}); | ||
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. We can also generalise the test to include all numbers that ends with 3 but not with 13 as
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 added more examples to the test function. |
||
test("should return '4th' for 4", () => { | ||
expect(getOrdinalNumber(4)).toEqual("4th"); | ||
}); | ||
test("should return '11th' for 11", () => { | ||
expect(getOrdinalNumber(11)).toEqual("11th"); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
function repeat() { | ||
return "hellohellohello"; | ||
function repeat(str,count) { | ||
if(count < 0 ) throw new Error ("Negative counts are not valid"); | ||
if(count === 0) return ""; | ||
if(count === 1) return str; | ||
return str.repeat(count); | ||
} | ||
|
||
module.exports = repeat; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
const isValidNumber = (cardNum) => { | ||
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. Because of the limited precision of number type in JavaScript, I would assume 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 fixed it. |
||
const StrCardNum = cardNum.toString(); | ||
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. Common convention is to start a variable name with a lowercase letter. Uppercase camel case is usually applied to Class or Type names. 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 changed it. |
||
if (StrCardNum.length !== 16 || !/^\d+$/.test(StrCardNum)) { | ||
return false; | ||
} | ||
let allSame = true; | ||
for (let i = 1; i < 16; i++) { | ||
if (StrCardNum[0] !== StrCardNum[i]) { | ||
allSame = false; | ||
break; | ||
} | ||
} | ||
if (allSame) { | ||
return false; | ||
} | ||
if (cardNum % 2 !== 0) { | ||
return false; | ||
} | ||
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 approach is not safe. For example, Can you replace this check so that it will work on any 16 digit credit card number (regardless if the number is represented as a string or a value of type number)? 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. to check the last digit whether even or odd |
||
let sum = 0; | ||
digitsArray = StrCardNum.split(''); | ||
for (let i = 0; i < 16; i++) { | ||
sum += digitsArray[i]; | ||
} | ||
if (sum <= 16) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
// 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). | ||
// - The final digit must be even. | ||
// - The sum of all the digits must be greater than 16. | ||
|
||
module.exports = isValidNumber; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
const isValidNumber = require("./card-validator"); | ||
|
||
test('Should return false if the digits are not 16', () => { | ||
expect(isValidNumber(222222222222222)).toBe(false); | ||
}); | ||
|
||
test('Should return false if the 16 digits are not all numbers', () => { | ||
expect(isValidNumber(222222222222222.)).toBe(false); | ||
}); | ||
|
||
test('Should return false if the 16 digits are the same', () => { | ||
expect(isValidNumber(2222222222222222)).toBe(false); | ||
}); | ||
|
||
test('Should return false if the final digit is not even', () => { | ||
expect(isValidNumber(2222222222222221)).toBe(false); | ||
}); | ||
|
||
test('Should return false if the sum < 16', () => { | ||
expect(isValidNumber(1011111111111111)).toBe(false); | ||
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.
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 fixed the bug. |
||
}); | ||
|
||
|
||
|
||
|
||
// - 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). | ||
// - The final digit must be even. | ||
// - The sum of all the digits must be greater than 16. |
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.
We can specify multiple
expect(...)
statements within eachtest()
to cover multiple values that belong to the same case. For example,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.
thank you for your comment.
I added more examples to make a more comprehensive test.