Skip to content
This repository was archived by the owner on May 14, 2024. It is now read-only.

Commit d0d20c3

Browse files
authored
made improvements according to Unmes's advice.
removed unnecessary parsInt, better explanation added as comments.
1 parent f00e8de commit d0d20c3

1 file changed

Lines changed: 9 additions & 7 deletions

File tree

‎Week3/homework/creditcardvalidator.js‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
/*main function is getting the info from other functions and returns the errors of the
33
invalid card number or returns the statement that says card number is valid.*/
44
function main(cardNum) {
5-
// let errors = [];
65
if (isValid(cardNum) === false) {
76
console.log(`(${cardNum})`)
87
if (checkLength(cardNum) === false) {
@@ -39,15 +38,17 @@ function checkLength(cardNum) {
3938
function checkSum(cardNum) {
4039
let sum = 0;
4140
for (let i = 0; i < cardNum.length; i++) {
42-
sum = parseInt(sum) + parseInt(cardNum[i]);
41+
sum = sum + parseInt(cardNum[i]);
4342
}
4443
if (sum > 16) {
4544
return true;
4645
}
4746
return false;
4847
}
4948

50-
// checking the card number if it has at least 2 different numbers
49+
/* checking the card number if it has at least 2 different numbers
50+
.split() method and every() method takes each character of the string into an array
51+
then the function checks all the characters if they match with the first one, returns true if all characters same*/
5152
function allEqualCharacters(cardNum) {
5253
let characterCheck = cardNum.split('').every(char => char === cardNum[0]);
5354
if (characterCheck == true) {
@@ -65,9 +66,11 @@ function isEndEven(cardNum) {
6566
return false;
6667
}
6768

68-
// checking if the card number is made of only numbers
69+
/* checking if the card number is made of only numbers
70+
^ does a match for the first character of the string
71+
\d searches for the digits between 0-9*/
6972
function onlyNumbers(cardNum) {
70-
let isNumOnly = /^\d+$/.test(cardNum); //this method is to check if the items of the variable is only made of numbers if so it returns 'true'.
73+
let isNumOnly = /^\d+$/.test(cardNum);
7174
if (isNumOnly == true) {
7275
return true;
7376
}
@@ -95,11 +98,10 @@ function isValid(cardNum) {
9598
}
9699

97100

98-
//main('1000000000006');
99101
main('9999777788880000'); //valid number example
100102
main('6666666666661666'); //valid number example
101103

102104
main('a92332119c011112'); //Invalid number example
103105
main('4444444444444444'); //Invalid number example
104106
main('1111111111111110'); //Invalid number example
105-
main('6666666666666661'); //Invalid number example
107+
main('6666666666666661'); //Invalid number example

0 commit comments

Comments
 (0)