Skip to content
This repository was archived by the owner on Apr 18, 2025. It is now read-only.

Feature/week 4 exercises #203

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

SyedArslanHaider
Copy link

Subject: Pull Request Review Request

Hi Sergey,

I hope you are doing well. I have submitted my pull request for review. I would appreciate it if you could take a look at it when you have the time.

I want to mention that I tried to solve the problem by myself first, and I did some research online for additional insights. This practice has been really helpful for me in deepening my understanding.

Thank you for your guidance and support!

@@ -0,0 +1,26 @@
function creditCard(value){
const cardPattern = /^\d{16}$/.test(value);
Copy link

Choose a reason for hiding this comment

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

Where did you take the rules from?
I mean 16 digits, at least 2 different digits, etc.
From what I see in Wikipedia the rules are different https://en.wikipedia.org/wiki/Payment_card_number.
Do you want to try to implement the rules? It would be an amazing excercice. Especially the the Luhn algorithm to check the last number.

Copy link
Author

Choose a reason for hiding this comment

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

I tried implementing a credit card feature, but my initial solution didn’t fully meet real-world requirements. I realized how much detail and accuracy are needed to make it work correctly. Even though my first attempt wasn’t perfect, I find it amazing to learn what’s involved in a proper implementation, and I’m excited to keep improving it to align with true real-world standards like implementing the Luhn algorithm to check the last character

@@ -0,0 +1,26 @@
function creditCard(value){
Copy link

Choose a reason for hiding this comment

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

It may be too much at once. And it's not a requirement for the task. But it would be good to pay attention already to function naming and interface.
What does your function do? The current name doesn't tell that. Something about credit cards.
What should the function return? Just printing is not that useful in production. You will need to call the function from another function.
Here are a couple of examples I see could be clearer.

  1. validateCredictCard. The function would raise an exception is the card number is invalid or would just finish not returning anything if all good.
  2. isCreditCardValid. The function would return either true or false depending if the number is valid or not.

Copy link
Author

Choose a reason for hiding this comment

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

Yes I understand but they give these instructions for credit card implementation

In this project you'll write a script that validates whether or not a credit card number is valid.

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.

For example, the following credit card numbers are valid:

9999777788880000
6666666666661666

And the following credit card numbers are invalid:

a92332119c011112 (invalid characters)
4444444444444444 (only one type of number)
1111111111111110 (sum less than 16)
6666666666666661 (odd final number)

It's a simple implementation, but I will try to improve it according to your suggestions. I’m truly happy to have your guidance as I work on improving my skills, and I appreciate the time you take to help me understand how to make my code better.

Copy link

Choose a reason for hiding this comment

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

Ah. Makes sense then. I didn't know you had precise requirements. All good then.
Did you have any questions about the function?

const sufix = (value > 10 && value <20) ? suffixes[0] : suffixes[Math.min(value%10,3)];
return value+sufix;
}
console.log(getOrdinal(1)); // "1st"
Copy link

Choose a reason for hiding this comment

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

You need more tests. Try 5 for example)

Copy link

Choose a reason for hiding this comment

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

Also the tests have to be written using Jest.
Here no better advice than just reading their docs.
Not an easy read but an important one. I think the biggest amount of time is spent in either writing tests or debugging. Not in writing code. So you should be very proficient and comfortable in tests.

Copy link
Author

Choose a reason for hiding this comment

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

"Yes my code isn’t working properly, but I’ll definitely read through the document you shared. If I run into any confusion, I’ll reach out to you for help. Thanks for your guidance!"

@@ -1,3 +1,26 @@
// Given a positive integer num,
// When the isPrime function is called with num as input,
// Then it should return a boolean representing whether the num is prime
function isPrime(num){
Copy link

Choose a reason for hiding this comment

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

Nice. How did you come up with this solution?

Copy link
Author

@SyedArslanHaider SyedArslanHaider Nov 4, 2024

Choose a reason for hiding this comment

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

If conditions are written by my own and I search Google for loop code but this is very helpful

Copy link

@nsi88 nsi88 left a comment

Choose a reason for hiding this comment

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

Looks good. You learned jest really fast.
Any questions? Anything doesn't work?

console.log(countChar("banana", "a")); // Expected output: 3
describe('countChar',()=>{
test('should return correct count for a specific character',()=>{
expect(countChar('aaaaa','a')).toBe(5);
Copy link

Choose a reason for hiding this comment

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

The test looks good. Nice.
For future you can use test.each. Shorter this way and allows to add more tests easily.

Copy link
Author

Choose a reason for hiding this comment

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

ok I will search about test.each

// creditCard.test.js
const creditCard = require('./credit-card-vslidator');

describe('creditCard function', () => {
Copy link

Choose a reason for hiding this comment

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

Nice tests

Copy link
Author

Choose a reason for hiding this comment

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

yes It works good I learned how test is worked mostly tests are passed but I think 2 test in password validator and 2 in card validator are failed I think the logic of the code has an error that's why some tests looks failed

Copy link

Choose a reason for hiding this comment

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

Which tests failed?

describe('getOrdinal', () => {
test('returns "1st" for 1', () => {
Copy link

Choose a reason for hiding this comment

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

test.each would be very handy in here.
Would be good not to miss numbers before 10 at least.

Copy link
Author

Choose a reason for hiding this comment

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

Yes I try to learn test.each also if feels any difficulty I will let you know

@@ -0,0 +1,26 @@
function creditCard(value){
Copy link

Choose a reason for hiding this comment

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

Ah. Makes sense then. I didn't know you had precise requirements. All good then.
Did you have any questions about the function?

@SyedArslanHaider
Copy link
Author

SyedArslanHaider commented Nov 8, 2024 via email

@nsi88
Copy link

nsi88 commented Nov 9, 2024

Credit card validator but I fix this now some test of the password validator tests should fails

Ok. Happy to help with the failing test if needed. But I need to know which one is failing.

@SyedArslanHaider
Copy link
Author

Hi Sergey,

I ran Jest tests on the password validator, but I'm encountering a few issues. Here’s a summary of the errors:

Undefined Variable:
In multiple tests, previousPasswords is throwing a ReferenceError. Specifically, in lines like:

else if(previousPasswords.includes(pass)){
It looks like previousPasswords hasn't been defined or passed correctly to the validPassword function.
Mismatch in Expected vs. Received Error Message:

In the test case should return error if password does not contain at least one number, Jest expected this error message:

"Error: Password must contain at least one number(0-9)"
but instead received:

"Error: Password must contain at least one lowercase letter (a-z)"
This suggests there might be an error in the logic handling the number requirement.
Could you take a look with me? I’d appreciate any insights on handling the undefined variable or adjusting the logic for the number requirement error. Thanks!

@nsi88
Copy link

nsi88 commented Nov 12, 2024

Hey hey,

First about the undefined variable.
A popular problem and not an easy concept.
If shortly variables are visible if they are defined in the same function (including if passed as parameters) or in a parent function/context.
An example:

let global_variable = 1;
function parent() {
  let parent_variable = 2;
  function child(param) {
    let child_variable = 3;
  }
}

In the example function child will see all the variables.
Function parent will see parent_variable and global variable.

@nsi88
Copy link

nsi88 commented Nov 12, 2024

Next an example when a function won't see a variable:

function1() {
  let variable1 = 1;
}
function2() {
  let variable2 = 2;
}

Here function2 is not defined within function1 and can't see its variables.
This is example related to your test.
You define previousPasswords in your test function describe('validPassword',()=>{
But you're trying to access it in a different function validPassword

@nsi88
Copy link

nsi88 commented Nov 12, 2024

There are couple ways to make this work:

  1. Define the previousPasswords variable in the common context. For example on the top of the file. Then both validPassword and its test will see it.
  2. Pass previousPasswords as a parameter to validPassword.

@nsi88
Copy link

nsi88 commented Nov 12, 2024

Try this article about variables scope in Javascript https://www.w3schools.com/js/js_scope.asp
Or the book on Javascript you have should describe scopes as well

@nsi88
Copy link

nsi88 commented Nov 12, 2024

Please ask what's not clear.
Happy to discuss further on a call if you need.

@nsi88
Copy link

nsi88 commented Nov 12, 2024

"Error: Password must contain at least one number(0-9)" but instead received:

"Error: Password must contain at least one lowercase letter (a-z)"

See how your function works? It first checks lowercase. If all good, then it checks numbers.
The problem is with the test. The test password needs to have only one error at a time to have a pure test.
Does it make sense?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants