Repository navigation
Conversation
cjyuan
left a comment
There was a problem hiding this comment.
Why not also try the card-validator exercise?
| // b) What is the if statement used to check | ||
| // - It checks whether the character at position index of str is exactly equal to char. If so it returns that position. | ||
| // c) Why is index++ being used? | ||
| // - index++ checks the next character of str. Without index would stay at 0 and the loop would never stop. |
There was a problem hiding this comment.
index++ does not "check the next character str". Could you rephrase the statement?
There was a problem hiding this comment.
index++ increments by 1 each time the lope find a match?
There was a problem hiding this comment.
That still does not quite explain "why" it is being used in the code.
There was a problem hiding this comment.
It's used to move into the next character in str if a matching char is not found.
|
@cjyuan I didn't know there was another card-validator exercise. I will do it. |
|
@cjyuan card-validator exercise is complete now. Thanks for reminding me. |
| @@ -0,0 +1,29 @@ | |||
| export function creditCardValidator(cardNumber) { | |||
| // rejects the number if itn's exactly 16 characters or contain non-digits | |||
| if (cardNumber.length !== 16 || !Number.isInteger(Number(cardNumber))) { | |||
There was a problem hiding this comment.
Any string resembling a valid number literal in JS could be safely converted to a number.
For examples, all these cases could pass the check.
creditCardValidator("3.14159265358e16");
creditCardValidator("+123456789012344");
creditCardValidator(" 998");
creditCardValidator("0x1111aaaa000998");
There was a problem hiding this comment.
I deleted the !Number.isInteger(Number(cardNumber))) condition and replaced it with a condition that check cardNumber is between 0 and 9.
| test("password must not be any previous password in the passwords array", () => { | ||
| const password = "1Az9!"; | ||
| const previousPassword = ["1Az9!", "1HBz2!"]; | ||
| const result = isValidPassword(password, previousPassword); | ||
| expect(result).toEqual(false); | ||
| }); |
There was a problem hiding this comment.
In addition to testing if the function could return false when the given password match a previous password, we should also test if the function could return true when the given password does not match any previous password (when the array of previous passwords is provided).
We could include multiple expect() to test several cases within the same category.
To make the test even more comprehensive, we could also test if the function could work correctly when the matching previous password appears as the first/last element in the array.
There was a problem hiding this comment.
Test cases added when previous password doesn't match and when swapping previous password element position in the array.
|
Changes look good. Feel free to use AI to explore different ways to check if all 16 characters are digits in JS. |
Learners, PR Template
Self checklist
Task code
CYF-1065
Changelist
Password validator test written and function implemented. Questions answers and code explained