Skip to content

Commit bfae2f8

Browse files
committed
removing all space and - from the string input
1 parent 596bf65 commit bfae2f8

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed
Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,13 @@
1-
function creditCardValidator(cardNumber) {}
1+
function creditCardValidator(cardNumber) {
2+
// Remove all - and spaces from the input
3+
const sanitized = cardNumber.replace(/[-\s]/g, "");
4+
5+
//check if the length of the sanitized input is 16
6+
if (sanitized.length !== 16) {
7+
return false;
8+
} else {
9+
return true;
10+
}
11+
}
12+
13+
module.exports = creditCardValidator;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const creditCardValidator = require("./creditCard-validator");
2+
3+
describe("creditCardValidator", () => {
4+
test("should return true for a 16 digit long card number", () => {
5+
expect(creditCardValidator("1234-5678-9012-3456")).toBe(true);
6+
expect(creditCardValidator("1234 5678 9012 3456")).toBe(true);
7+
});
8+
});

0 commit comments

Comments
 (0)