Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
removing all space and - from the string input
  • Loading branch information
TzeMingHo committed Sep 25, 2025
commit bfae2f8725bd7887cc51547a2967563207ef293b
14 changes: 13 additions & 1 deletion Sprint-3/3-stretch/creditCard-validator.js
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
function creditCardValidator(cardNumber) {}
function creditCardValidator(cardNumber) {
// Remove all - and spaces from the input
const sanitized = cardNumber.replace(/[-\s]/g, "");

//check if the length of the sanitized input is 16
if (sanitized.length !== 16) {
return false;
} else {
return true;
}
}

module.exports = creditCardValidator;
8 changes: 8 additions & 0 deletions Sprint-3/3-stretch/creditCard-validator.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const creditCardValidator = require("./creditCard-validator");

describe("creditCardValidator", () => {
test("should return true for a 16 digit long card number", () => {
expect(creditCardValidator("1234-5678-9012-3456")).toBe(true);
expect(creditCardValidator("1234 5678 9012 3456")).toBe(true);
Copy link
Member

Choose a reason for hiding this comment

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

Your tests all currently assume there are extra characters in the credit card number - is 123456789012345 valid? You may want to test that kind of thing too, to make sure your implementation doesn't assume there are separators.

Copy link
Author

Choose a reason for hiding this comment

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

You are right. I should have included tests for card numbers that are more or fewer than 16 digits. As you suggested, I created one test for the case of fewer and another for more.

Copy link
Member

Choose a reason for hiding this comment

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

These extra tests look good, but aren't quite getting at the problem I was pointing at - can you add a test for a 16-digit credit card number which is only numbers (i.e. no spaces or -s)?

Copy link
Author

Choose a reason for hiding this comment

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

Yeah. I included a test for only 16 digits without spaces or operators.

});
});