Skip to content

CYF London | Fatma Arslantas | Module-Structuring-and-Testing-Data | Week 3#209

Closed
AFatmaa wants to merge 13 commits intoCodeYourFuture:mainfrom
AFatmaa:sprint3
Closed

CYF London | Fatma Arslantas | Module-Structuring-and-Testing-Data | Week 3#209
AFatmaa wants to merge 13 commits intoCodeYourFuture:mainfrom
AFatmaa:sprint3

Conversation

@AFatmaa
Copy link

@AFatmaa AFatmaa commented Nov 29, 2024

Learners, PR Template

Self checklist

  • I have committed my files one by one, on purpose, and for a reason
  • I have titled my PR with COHORT_NAME | FIRST_NAME LAST_NAME | REPO_NAME | WEEK
  • I have tested my changes
  • My changes follow the style guide
  • My changes meet the requirements of this task

Changelist

Briefly explain your PR.

Questions

Ask any questions you have for your reviewer.

@AFatmaa AFatmaa added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Nov 29, 2024
@cjyuan cjyuan self-requested a review December 2, 2024 13:01
Copy link
Contributor

@cjyuan cjyuan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left my comments and suggestions in the code.


// Example assertions to test the function
try {
console.assert(getCardValue("3♠") === 3, "Test failed for '3♠'");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you expect from the following function calls?
Does your function returns the value you expected?

getCardValue("20Q♠");
getCardValue("02♠");
getCardValue("AA♠");
getCardValue("2.1♠")


console.log(isProperFraction(2, 3));
console.log(isProperFraction(5, 2));
console.log(isProperFraction(-4, 7));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suppose the notation |X| denotes the absolute value of X.
If you are unfamiliar with the definition of absolute value, you should look the term up.

To test your function more comprehensively, you should consider testing all combinations of positive and negative parameters. For examples,

  • For cases where |numerator| < |denominator|, test
    isProperFraction(4, 7), isProperFraction(-4, -7), isProperFraction(-4, 7), isProperFraction(4, -7).
  • Do the same for cases where |numerator| > |denominator|.

Hint: If you compute the absolute value of both parameters inside the function first, the code can become much simpler.

return false
}
// Check for non-positive side length
if (a <= 0 || b <= 0 || c <= 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it necessary to include the if-statement at lines 23-25?
Can you find any values for a, b, and c, such that the function will fail after you removed the if-statement at lines 23-25?

To check if three sides can form a valid triangle, a lot of people added an if-statement in their code to ensure the three sides are positive numbers. But have you ever wondered why they did that? The answer is related to how the value of a, b, and c are represented in a particular programming language, and you may not have the necessary knowledge yet to understand why it is necessary to ensure a, b, and c are positives in some programming languages.

The main point I would like to raise 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.

expect(getAngleType(200)).toBe("Reflex angle");
});

test('Get Invalid angle ', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not test also angles that are < 0 and angles that are >= 360? Especially the boundary cases like 0 and 360.


// Return the number with the appropriate suffix.
// Use modulus to determine the correct suffix or fall back to "th".
return `${num}${suffixes[(value - 20) % 10] || suffixes[value] || suffixes[0]}`;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you elaborate how this expression yield the suffix value when value is 11?
suffixes[(value - 20) % 10] || suffixes[value] || suffixes[0]

// Check if the input is less than 2, which is not prime
if (num < 2) return false;

// Loop from 2 to the square root of the number
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can possibly improve the performance of the code in the following manners:

  • Check if num is 2, and check only odd numbers >= 3 in the loop
  • Avoid calling Math.sqrt(num) repeatedly by assigning the value of Math.sqrt(num) to a variable once, and then refer to the variable in the condition of the loop.
    • Note: The condition is checked at the start of every iteration.

});

test('should handle larger non-prime numbers', () => {
expect(isPrime(100)).toBe(false);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could also test some odd composite numbers, and specify the parameter as 101 * 13 (a composite number as a product of two prime numbers).

});

test('should return false for a password that is too short', () => {
expect(isPasswordValid("sh1*", previousPasswords)).toBe(false);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Sh1*" would be a better test value because when the function return false, it would imply "Sh1*" passes all other checks except the password length check.

@cjyuan cjyuan added Reviewed Volunteer to add when completing a review with trainee action still to take. and removed Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. labels Dec 2, 2024
@AFatmaa
Copy link
Author

AFatmaa commented Dec 2, 2024

Hi @cjyuan,

Thank you for your review. I'll check and fix them! 🙌😊

@AFatmaa
Copy link
Author

AFatmaa commented Dec 2, 2024

Hi @cjyuan,

Thank you for taking the time to review my PR and for your detailed comments and suggestions. I've carefully examined each of them, made the necessary adjustments, and committed the changes. Your feedback was helpful and I truly appreciate your guidance and feedback!

Please let me know if there’s anything else that needs improvement. 🙌

@cjyuan
Copy link
Contributor

cjyuan commented Dec 5, 2024

Code is correct.

Can you explain how this expression work when value is 15?

    const suffixes = ["th", "st", "nd", "rd"];
    return `${num}${suffixes[value % 10] || suffixes[0]}`;

@AFatmaa
Copy link
Author

AFatmaa commented Dec 5, 2024

Hi @cjyuan,
If I understand your question correctly, here’s how I would explain it:

Can you explain how this expression work when value is 15?

const suffixes = ["th", "st", "nd", "rd"];
return `${num}${suffixes[value % 10] || suffixes[0]}`;
  1. value % 10
  • This expression calculates the last digit of the number value.
  • For example: If value = 15, then 15 % 10 equals 5.
  1. suffixes[value % 10]
  • The suffixes array is defined as: const suffixes = ["th", "st", "nd", "rd"];
  • The elements in this array are:
    suffixes[0] → "th"
    suffixes[1] → "st"
    suffixes[2] → "nd"
    suffixes[3] → "rd"
  • The expression suffixes[value % 10] attempts to access the array at the index of the last digit of value.
  • If value % 10 is outside the array’s bounds (e.g., there is no suffixes[5]), the result is undefined.
  1. || suffixes[0]
  • The || operator in JavaScript works as a fallback mechanism.
  • If the left-hand side is a "falsy" value (like undefined), it evaluates the right-hand side instead.
  • Here, if suffixes[value % 10] is undefined, the code will use suffixes[0], which is "th".

Example: value = 15

  1. value % 10: 15 % 10 → 5
  2. suffixes[5]: Since suffixes do not have an element at index 5, this evaluates to undefined.
  3. || suffixes[0]: undefined || suffixes[0] → "th"
  4. Final result: The function returns "15th".

The || suffixes[0] part ensures that the default "th" suffix is used if there’s no valid suffix at the index value % 10. For example, when the last digit is 5 or 6, "th" is returned as a fallback.

Thank you for taking the time to ask detailed questions and help me learn. I really appreciate it! 😊

@AFatmaa AFatmaa added the Complete Volunteer to add when work is complete and all review comments have been addressed. label Dec 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Complete Volunteer to add when work is complete and all review comments have been addressed. Reviewed Volunteer to add when completing a review with trainee action still to take.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants