-
-
Notifications
You must be signed in to change notification settings - Fork 195
London | May-2025 | Fatima_Zohra_Belkedari | Module Structuring and Testing Data | Sprint. 3 #629
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?
Conversation
const number = Number(rank); | ||
if (number >= 2 && number <= 10) return number; |
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.
In JavaScript, strings that represent valid numeric literals in the language can be safely converted to equivalent numbers. For examples, "0x02", "2.1", or "00_02".
Does your function return the value you expected from each of the following function calls?
getCardValue("0x02♠");
getCardValue("2.1♠");
getCardValue("00_02♠");
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 tried and used chat gpt but I dont't want to copy and paste without understanding. I would appreciate if you could explain to me.
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 run the code and it did't return what was expected with the functions above.
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.
Why not update the code to throw error with the argument is one of these?
getCardValue("0x02♠");
getCardValue("2.1♠");
getCardValue("00_02♠");
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.
Done!
test("should return 10 for Jack of Diamonds", () => { | ||
expect(getCardValue ("J")).toEqual(10); | ||
}); | ||
|
||
test("should return 10 for Queen of Clubs", () => { | ||
expect(getCardValue("Q")).toEqual(10); | ||
}); | ||
|
||
test("should return 10 for King of Spades", () => { | ||
expect(getCardValue("K")).toEqual(10); | ||
}); | ||
|
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 could combine these tests under the category "should return 10 for face cards (J, Q, K)" and check all three ranks J, Q, K).
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.
Ok.
test("should return 10 for face cards J,Q,K", () => {
["J" ,"Q" ,"K"] .forEach(rank) => {
expect(getCardValue(rank)).toEqual (10);
});
});
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.
The approach you suggested in your previous comment works. Why not update the code as well?
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.
ok, noted.
const strNum= num.toString(); | ||
if(strNum.slice(-2)==="11") { | ||
return strNum + "th"; | ||
} | ||
if(strNum.slice(-2)==="12") { | ||
return strNum + "th"; | ||
} | ||
if(strNum.slice(-2)==="13") { | ||
return strNum + "th"; | ||
} | ||
if(strNum.slice(-1)==="1") { | ||
return strNum + "st"; | ||
} | ||
if(strNum.slice(-1)==="2") { | ||
return strNum + "nd"; | ||
} | ||
if(strNum.slice(-1)==="3") { | ||
return strNum + "rd"; | ||
} | ||
|
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.
-
If
num
is an integer, we could also use % to extract the last one or two digits from its value. -
What should the function return when
num
is 10, 15, or 124?
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.
If we use the number%10 we will get the last digit. In this case :
10 % 10 = 0
15 % 10 = 5
124 % 10 = 4
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.
What does your function return when num
is 10, 15, or 124?
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.
number will return the remainder
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.
10 % 10 = 0
15 % 10 = 5
124 % 10 = 4
test("should return '2nd' for 2", () => { | ||
expect(getOrdinalNumber(2)).toEqual("2nd"); | ||
}); |
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.
To ensure thorough testing, we need broad scenario coverage. Listing individual values, however, can quickly lead to an unmanageable number of test cases.
Instead of writing tests for individual numbers, consider grouping all possible input values into meaningful categories. Then, select representative samples from each category to test. This approach improves coverage and makes our tests easier to maintain.
For example, we can prepare a test for numbers 2, 22, 132, etc. as
test("append 'nd' to numbers ending in 2, except those ending in 12", () => {
expect( getOrdinalNumber(2) ).toEqual("2nd");
expect( getOrdinalNumber(22) ).toEqual("22nd");
expect( getOrdinalNumber(132) ).toEqual("132nd");
});
Can you update the tests in this script so that they can cover all valid integers?
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.
test("append 'st' to numbers ending in 1" , () => {
expect(getOrdinalNumber(1)).toEqual("1st");
expect(getOrdinalNumber(21)).toEqual("21st");
expect(getOrdinalNumber(121)).toEqual("121st");
});
test("append 'nd' to numbers ending in 2, except those ending in 12", () => {
expect(getOrdinalNumber(2)).toEqual("2nd");
expect(getOrdinalNumber(22)).toEqual("22nd");
expect(getOrdinalNumber(132)).toEqual("132nd");
});
test("append 'rd' to numbers ending in 3 , except those ending in 13", () => {
expect(getOrdinalNumber(23)).toEqual("23rd");
expect(getOrdinalNumber(13)).toEqual("13th");
expect(getOrdinalNumber(3)).toEqual("3rd");
});
test("should return '11th' for 11", () => {
expect(getOrdinalNumber(11)).toEqual("11th");
});
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.
Some of the code could still use some improvement.
expect(getOrdinalNumber(23)).toEqual("23rd"); | ||
expect(getOrdinalNumber(13)).toEqual("13th"); |
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.
"except those ending in 13".
Numbers ending in 12, and 13 could be placed together with numbers ending in 11 in the same category.
const strNum= num.toString(); | ||
if(strNum.slice(-2)==="11") { | ||
return strNum + "th"; | ||
} | ||
if(strNum.slice(-2)==="12") { | ||
return strNum + "th"; | ||
} | ||
if(strNum.slice(-2)==="13") { | ||
return strNum + "th"; | ||
} | ||
if(strNum.slice(-1)==="1") { | ||
return strNum + "st"; | ||
} | ||
if(strNum.slice(-1)==="2") { | ||
return strNum + "nd"; | ||
} | ||
if(strNum.slice(-1)==="3") { | ||
return strNum + "rd"; | ||
} | ||
|
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.
What does your function return when num
is 10, 15, or 124?
const number = Number(rank); | ||
if (number >= 2 && number <= 10) return number; |
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.
Why not update the code to throw error with the argument is one of these?
getCardValue("0x02♠");
getCardValue("2.1♠");
getCardValue("00_02♠");
test("should return 10 for Jack of Diamonds", () => { | ||
expect(getCardValue ("J")).toEqual(10); | ||
}); | ||
|
||
test("should return 10 for Queen of Clubs", () => { | ||
expect(getCardValue("Q")).toEqual(10); | ||
}); | ||
|
||
test("should return 10 for King of Spades", () => { | ||
expect(getCardValue("K")).toEqual(10); | ||
}); | ||
|
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.
The approach you suggested in your previous comment works. Why not update the code as well?
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.
get-ordinal-number.js
and get-ordinal-number.test.js
can still use some improvement.
All other changes look good.
|
||
test("should return '11th' for 11 , 12, 13", () => { | ||
expect(getOrdinalNumber(11)).toEqual("11th"); | ||
expect(getOrdinalNumber(12)).toEqual("12th"); | ||
expect(getOrdinalNumber(13)).toEqual("13th"); | ||
}); |
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 include this test to the script and then check if your function can pass this test:
// Replace _______ by the value you expected
test("should append '__' to number ending in 0, 4-9", () => {
expect(getOrdinalNumber(124)).toEqual( _______ );
expect(getOrdinalNumber(10)).toEqual( _______ );
expect(getOrdinalNumber(99)).toEqual( _______ );
});
Learners, PR Template
Self checklist
Changelist
Briefly explain your PR.
Questions
Ask any questions you have for your reviewer.