-
-
Notifications
You must be signed in to change notification settings - Fork 50
Feature/week 4 exercises #203
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 commentThe reason will be displayed to describe this comment to others. Learn more. Where did you take the rules from?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| if(!cardPattern){ | ||
| return "Invalid: Must be 16 digits and all numbers"; | ||
| } | ||
| const uniqueDigits = new Set(value.split('')).size; | ||
| if (uniqueDigits < 2) { | ||
| return "Invalid: Must contain at least two different digits"; | ||
| } | ||
| if(parseInt(value.slice(-1)) % 2 !== 0){ | ||
| return "Invalid: Final digit must be even"; | ||
| } | ||
| const sum = Array.from(value).reduce((a,b) => a+parseInt(b),0); | ||
| if(sum <=16){ | ||
| return "Invalid: Sum of all digits must be greater than 16"; | ||
| } | ||
| return "Valid credit card number"; | ||
|
|
||
| } | ||
| // Example tests | ||
| console.log(creditCard("9999777788880000")); // "Valid credit card number" | ||
| console.log(creditCard("6666666666661666")); // "Valid credit card number" | ||
| console.log(creditCard("a92332119c011112")); // "Invalid: Must be 16 digits and all numbers" | ||
| console.log(creditCard("4444444444444444")); // "Invalid: Must contain at least two different digits" | ||
| console.log(creditCard("1111111111111110")); // "Invalid: Sum of all digits must be greater than 16" | ||
| console.log(creditCard("6666666666666661")); // "Invalid: Final digit must be even" | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,3 +2,18 @@ | |
|
|
||
| // continue testing and implementing getOrdinalNumber for additional cases | ||
| // Write your tests using Jest - remember to run your tests often for continual feedback | ||
| function getOrdinal(number){ | ||
| const suffixes = ["th","st","nd","rd"]; | ||
| const value = number % 100; | ||
| const sufix = (value > 10 && value <20) ? suffixes[0] : suffixes[Math.min(value%10,3)]; | ||
| return value+sufix; | ||
| } | ||
| console.log(getOrdinal(1)); // "1st" | ||
|
||
| console.log(getOrdinal(2)); // "2nd" | ||
| console.log(getOrdinal(3)); // "3rd" | ||
| console.log(getOrdinal(4)); // "4th" | ||
| console.log(getOrdinal(11)); // "11th" | ||
| console.log(getOrdinal(21)); // "21st" | ||
| console.log(getOrdinal(42)); // "42nd" | ||
| console.log(getOrdinal(103)); // "103rd" | ||
| console.log(getOrdinal(112)); // "112th" | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 commentThe reason will be displayed to describe this comment to others. Learn more. Nice. How did you come up with this solution?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| if(num <=1){ | ||
| return false; | ||
| } | ||
| if(num === 2){ | ||
| return true; | ||
| } | ||
| if(num % 2 === 0){ | ||
| return false; | ||
| } | ||
| for(let i=3; i<=Math.sqrt(num); i++){ | ||
| if(num % i === 0){ | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
| console.log(isPrime(2)); // true | ||
| console.log(isPrime(11)); // true | ||
| console.log(isPrime(15)); // false | ||
| console.log(isPrime(1)); // false | ||
| console.log(isPrime(17)); // true | ||
| console.log(isPrime(25)); // false | ||
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:
For example, the following credit card numbers are valid:
And the following credit card numbers are invalid:
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?