Skip to content

London | 26-ITP-Sep | Bartosz Kawiak | Sprint 3 | Stretch Exercises - #1743

Open
bartoszkawiak wants to merge 1 commit into
CodeYourFuture:mainfrom
bartoszkawiak:sprint3/stretch
Open

bartoszkawiak wants to merge 1 commit into
CodeYourFuture:mainfrom
bartoszkawiak:sprint3/stretch

Conversation

@bartoszkawiak

Copy link
Copy Markdown

Learners, PR Template

Self checklist

  • I have titled my PR with Region | Cohort | FirstName LastName | Sprint | Assignment Title
  • My changes meet the requirements of the task
  • I have tested my changes
  • My changes follow the style guide

Task code

CYF-1065

Changelist

Stretch exercises solved, implemented tests and finished function. Added and finished required files for card-validation.

Questions

I did change the password in first example test in password-validation.test.js as I did not find a way to make it pass alongside other requirements. Is there a way to complete the exercise with the example test passing?

@bartoszkawiak bartoszkawiak added 📅 Sprint 3 Assigned during Sprint 3 of this module Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. Module-Structuring-And-Testing-Data The name of the module. labels Oct 21, 2026
Comment on lines +1 to +4
export function isCardValid(cardNum) {
if (typeof cardNum !== "number") {
return false;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Because of the way JavaScript represents numbers, isCardValid(9999999999999997) would evaluate to true.

Could you use AI to find out why, and then update your code (both function implementation and tests) to use other data type to represent card number?

Comment on lines +13 to +16
const regex = /^\d$/;
if (!regex.test(num)) {
return false;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Note: Since you are using regex, you could also just use regex to check if the whole card number (as a string) contain 16 digits (outside the loop) instead of checking each digit one by one.

Comment on lines +8 to +12
test("card number should have 16 digits", () => {
const cardNumber = 1234567890987654;
const result = isCardValid(cardNumber);
expect(result).toEqual(true);
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

In addition to checking if the function can correctly return true when the specified condition is met, this test should also check if the function can correctly return false when the specified condition is not met.

The same also applies to other tests in this script.

hasSpecialChars &&
!passwords.includes(password)
) {
passwords.push(password);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is "adding the password being checked to a global variable" an expected behavior of this function?

Comment on lines +1 to 2
const passwords = [];
export function isValidPassword(password) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

For higher flexibility, we could design the function to accept passwords as an optional second parameter (with a default value).

const specialChars = "!#$%.*&";

for (const char of password) {
hasUpper = upperLetters.includes(char) ? true : hasUpper;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Note:

  hasUpper = upperLetters.includes(char) ? true : hasUpper;

is equivalent to

 hasUpper = hasUpper && upperLetters.includes(char);

or

  if (upperLetters.includes(char)) hasUpper = true;

Alternatively, you could use regex to check if password contains an uppercase letter in one statement.

for (const char of password) {
hasUpper = upperLetters.includes(char) ? true : hasUpper;
hasLower = lowerLetters.includes(char) ? true : hasLower;
hasNumbers = numbers.includes(char) ? true : hasNumbers;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Note: The term digit refers to a number in the range 0-9. A number may contain any number of digits.

Comment on lines +39 to +43
test("password have at least one number(0-9)", () => {
const password = "RRRqqq";
const result = isValidPassword(password);
expect(result).toEqual(false);
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The function can return false for multiple reasons.
To test a specific reason, choose an input that satisfies all other conditions except the one you're targeting. This way, if the function returns false, you can confidently attribute it to that specific condition.

For example, the function might return false for "RRRqqq" not only because it lacks a digit, but also because it lacks a special symbol. As a result, we can't be certain that the function correctly handles the case of passwords have at least one digit, since multiple conditions are being violated simultaneously.

@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 Oct 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Module-Structuring-And-Testing-Data The name of the module. Reviewed Volunteer to add when completing a review with trainee action still to take. 📅 Sprint 3 Assigned during Sprint 3 of this module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants