-
-
Notifications
You must be signed in to change notification settings - Fork 51
Feature/week 4 exercises #203
base: main
Are you sure you want to change the base?
Feature/week 4 exercises #203
Conversation
@@ -0,0 +1,26 @@ | |||
function creditCard(value){ | |||
const cardPattern = /^\d{16}$/.test(value); |
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.
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.
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 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){ |
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.
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.
validateCredictCard
. The function would raise an exception is the card number is invalid or would just finish not returning anything if all good.isCreditCardValid
. The function would return either true or false depending if the number is valid or not.
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.
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.
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.
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" |
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.
You need more tests. Try 5 for example)
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.
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.
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.
"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){ |
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.
Nice. How did you come up with this solution?
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 conditions are written by my own and I search Google for loop code but this is very helpful
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.
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); |
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 test looks good. Nice.
For future you can use test.each. Shorter this way and allows to add more tests easily.
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 I will search about test.each
// creditCard.test.js | ||
const creditCard = require('./credit-card-vslidator'); | ||
|
||
describe('creditCard function', () => { |
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.
Nice tests
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.
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
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.
Which tests failed?
describe('getOrdinal', () => { | ||
test('returns "1st" for 1', () => { |
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.each would be very handy in here.
Would be good not to miss numbers before 10 at least.
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.
Yes I try to learn test.each also if feels any difficulty I will let you know
@@ -0,0 +1,26 @@ | |||
function creditCard(value){ |
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.
Ah. Makes sense then. I didn't know you had precise requirements. All good then.
Did you have any questions about the function?
Credit card validator but I fix this now some test of the password
validator tests should fails
…On Fri, 8 Nov 2024, 4:35 pm Sergey Novikov, ***@***.***> wrote:
***@***.**** commented on this pull request.
------------------------------
In week-4/implement/credit-card-vslidator.test.js
<#203 (comment)>
:
> @@ -0,0 +1,30 @@
+// creditCard.test.js
+const creditCard = require('./credit-card-vslidator');
+
+describe('creditCard function', () => {
Which tests failed?
—
Reply to this email directly, view it on GitHub
<#203 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AHWOUANEENNKZFNEIEMOHVLZ7TK5VAVCNFSM6AAAAABRC2RDDKVHI2DSMVQWIX3LMV43YUDVNRWFEZLROVSXG5CSMV3GSZLXHMZDIMRUGEZDCNRYGI>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
Ok. Happy to help with the failing test if needed. But I need to know which one is failing. |
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: else if(previousPasswords.includes(pass)){ 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)" "Error: Password must contain at least one lowercase letter (a-z)" |
Hey hey, First about the undefined variable.
In the example function child will see all the variables. |
Next an example when a function won't see a variable:
Here function2 is not defined within function1 and can't see its variables. |
There are couple ways to make this work:
|
Try this article about variables scope in Javascript https://www.w3schools.com/js/js_scope.asp |
Please ask what's not clear. |
See how your function works? It first checks lowercase. If all good, then it checks numbers. |
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!