-
-
Notifications
You must be signed in to change notification settings - Fork 391
London| 26-ITP-May | Martin Mwaka | Sprint 3 | Stretch #1464
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Temceo
wants to merge
7
commits into
CodeYourFuture:main
Choose a base branch
from
Temceo:stretch
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
08c83d6
create card-validator.js file
Temceo a043ba8
Complete 4-stretch/card-validator.js
Temceo 5bc7e2b
Add tests to 4-stretch/card-validator.test.js
Temceo 7cd1603
Complete 4-stretch/find.js
Temceo 012c657
Write tests for 4-stretch/password-validator.test.js
Temceo 3e569fa
Complete password-validator.js
Temceo 48629e0
Update 4-stretch/password-validator.test.js - all tests pass
Temceo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| /* | ||
| 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: | ||
|
|
||
| ```markdown | ||
| 9999777788880000 | ||
| 6666666666661666 | ||
| ``` | ||
|
|
||
| And the following credit card numbers are invalid: | ||
|
|
||
| ```markdown | ||
| a92332119c011112 (invalid characters) | ||
| 4444444444444444 (only one type of number) | ||
| 1111111111111110 (sum less than 16) | ||
| 6666666666666661 (odd final number) | ||
| ``` | ||
|
|
||
| These are the requirements your project needs to fulfill: | ||
|
|
||
| - Make a JavaScript file with a name that describes its contents. | ||
| - Create a function with a descriptive name which makes it clear what the function does. The function should take one argument, the credit card number to validate. | ||
| - Write at least 2 comments that explain to others what a line of code is meant to do. | ||
| - Return a boolean from the function to indicate whether the credit card number is valid. | ||
|
|
||
| Good luck! | ||
| */ | ||
|
|
||
| const validateCard = (card) => { | ||
| const cardStr = String(card); | ||
| // Must have 16 digits | ||
| const has16Digits = cardStr.length === 16; | ||
| // Must be all digits | ||
| const isAllDigits = /^\d+$/.test(cardStr); | ||
| // Must have at least 2 numbers | ||
| const hasMoreThanOneDigit = cardStr.length > 1; | ||
| // Final digit must be even | ||
| const hasEvenFinalDigit = isAllDigits && (Number(cardStr) % 10) % 2 === 0; | ||
| // Sum of numbers must be at least 16 | ||
| const sumOfNumbers = isAllDigits | ||
| ? cardStr.split("").reduce((acc, num) => acc + Number(num), 0) | ||
| : 0; | ||
|
|
||
| const hasSumOver15 = sumOfNumbers > 15; | ||
|
|
||
| // Must have more than one type of number | ||
| const hasMultipleDistinctDigits = new Set(cardStr.split("")).size > 1; | ||
|
|
||
| return has16Digits && | ||
| hasMoreThanOneDigit && | ||
| hasEvenFinalDigit && | ||
| hasSumOver15 && | ||
| hasMultipleDistinctDigits | ||
| ? "Valid card" | ||
| : "Invalid card"; | ||
| }; | ||
|
|
||
| console.log(validateCard(9999777788880000)); // valid | ||
| console.log(validateCard(6666666666661666)); // valid | ||
| console.log(validateCard("a92332119c011112")); // Invalid | ||
| console.log(validateCard(4444444444444444)); // Invalid | ||
| console.log(validateCard(1111111111111110)); // Invalid | ||
| console.log(validateCard(6666666666666661)); // Invalid | ||
|
|
||
| module.exports = validateCard; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| const validateCard = require("./card-validator"); | ||
|
|
||
| test("should have 16 digits, all numbers", () => { | ||
| const cardNumber = 9999777788880000; | ||
| const result = validateCard(cardNumber); | ||
| expect(result).toBe("Valid card"); | ||
| }); | ||
|
|
||
| test("should not have less than 16 digits", () => { | ||
| const cardNumber = 99997777; | ||
| const result = validateCard(cardNumber); | ||
| expect(result).toBe("Invalid card"); | ||
| }); | ||
|
|
||
| test("should have at least two different distinct digits", () => { | ||
| const cardNumber = 9999777788880000; | ||
| const result = validateCard(cardNumber); | ||
| expect(result).toBe("Valid card"); | ||
| }); | ||
|
|
||
| test("should have even final digit", () => { | ||
| const cardNumber = 6666666666661666; | ||
| const result = validateCard(cardNumber); | ||
| expect(result).toBe("Valid card"); | ||
| }); | ||
|
|
||
| test("total sum of numbers should be greater than 15", () => { | ||
| const cardNumber = 6666666666661666; | ||
| const result = validateCard(cardNumber); | ||
| expect(result).toBe("Valid card"); | ||
| }); | ||
|
|
||
| test("single repeating digit is invalid", () => { | ||
| const cardNumber = 4444444444444444; | ||
| const result = validateCard(cardNumber); | ||
| expect(result).toBe("Invalid card"); | ||
| }); | ||
|
|
||
| test("digits cannot include non numerals", () => { | ||
| const cardNumber = "a92332119c011112"; | ||
| const result = validateCard(cardNumber); | ||
| expect(result).toBe("Invalid card"); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,10 @@ console.log(find("code your future", "z")); | |
| // Pay particular attention to the following: | ||
|
|
||
| // a) How the index variable updates during the call to find | ||
| // [MM] - The index variable increments by one each time the letter (str[index]) at the current index does not match the target letter (char) | ||
|
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. Tip: You don't need to include your name in comments as git keeps track of who made what changes. |
||
| // b) What is the if statement used to check | ||
| // [MM] - The if statement is used to check if the current letter matches the target letter (char). If a match is found (str[index] === char) it will cause an early exit from the while loop at the current index | ||
| // c) Why is index++ being used? | ||
| // [MM] - The index++ increments the index variable by one each time the current letter (str[index]) does not match the target letter (char). It only runs if a match is not found | ||
| // d) What is the condition index < str.length used for? | ||
| // [MM] - The condition index < str.length is used to set the upper bound of the loop (the loop will run while index number is less than str.length) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,16 @@ | ||
| function passwordValidator(password) { | ||
| return password.length < 5 ? false : true | ||
| } | ||
| const previousPasswords = ["heLlo5.", "Su1ma#"]; | ||
|
|
||
| const checks = { | ||
| minLength: password.length > 4, | ||
| hasUpperCase: /[A-Z]/.test(password), | ||
|
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. good use of regular expressions here |
||
| hasLowerCase: /[a-z]/.test(password), | ||
| hasNumber: /[0-9]/.test(password), | ||
| hasSymbol: /[!#$%.*&]/.test(password), | ||
| notPreviousPassword: !previousPasswords.includes(password), | ||
| }; | ||
|
|
||
| return Object.values(checks).every(Boolean); | ||
| } | ||
|
|
||
| module.exports = passwordValidator; | ||
| module.exports = passwordValidator; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This looks correct, but can you read the task specification again? See what the return type of the function is meant to be.