|
2 | 2 |
|
3 | 3 | /* |
4 | 4 | description PROJECT: Validate the number of a credit card based on specified ruleset |
| 5 | + code revision #6 |
5 | 6 | */ |
6 | 7 |
|
7 | 8 | function runProjectccNumberValidator() { |
| 9 | + |
| 10 | + // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = |
| 11 | + // original submitted solution |
| 12 | + // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = |
| 13 | + // console.log("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = ="); |
| 14 | + // console.log("JS-1 - week 3 - PROJECT: Credit card number Validation"); |
| 15 | + // console.log("Validate the number of a credit card based on specified ruleset"); |
| 16 | + // console.log("- - - - - - - - - - - - - - - - - - - -"); |
| 17 | + // // |
| 18 | + // // define the Credit card number validation routine |
| 19 | + // // |
| 20 | + // function isValidccNumber(ccNumber){ |
| 21 | + // // transform to string and remove non-numeric characters |
| 22 | + // let ccNumStr=String(ccNumber).replace(/[^0-9]/g,''); |
| 23 | + // // put each character into an array element |
| 24 | + // let ccDigits=ccNumStr.split(''); |
| 25 | + // // decide validity based on specified ruleset |
| 26 | + // return (ccNumStr.length==16) |
| 27 | + // && (ccNumStr.replace((new RegExp(ccNumStr[0],'g')),'').length>0) |
| 28 | + // && ((ccNumStr[15]%2)==0) |
| 29 | + // && (ccDigits.reduce((a,b)=>Number(a)+Number(b))>16); |
| 30 | + // } |
| 31 | + // let ccNumberList=[ '9999777788880000','6666666666661666',' 1234 - 5678 - 9012 - 3456 ', |
| 32 | + // 'a92332119c011112','4444444444444444', |
| 33 | + // '1111111111111110','6666666666666661',' 6543 - 2109 - 8765 - 4321 ' ]; |
| 34 | + // for (let i=0; i<ccNumberList.length; i++){ |
| 35 | + // console.log(`Based on the provided rulset, the Credit card number [${ |
| 36 | + // ccNumberList[i]}] ${(isValidccNumber(ccNumberList[i])?'IS':'is NOT')} valid.`); |
| 37 | + // } |
| 38 | + // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = |
| 39 | + |
| 40 | + |
| 41 | + // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = |
| 42 | + // alternate solution with detailed reporting and complete ruleset evaluation |
| 43 | + // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = |
| 44 | + // function isValidccNumber(ccNumber){ |
| 45 | + // // transform into string and remove all spaces |
| 46 | + // let ccNumStr=String(ccNumber).replace(/\s+/g,''); |
| 47 | + // // define validity ruleset based on specifications |
| 48 | + // let ruleset={ |
| 49 | + // is_exactly_16_digits_long :(ccNumStr.length==16), |
| 50 | + // contains_only_numeric_digits :(ccNumStr===String(ccNumber).replace(/[^0-9]/g,'')), |
| 51 | + // not_all_the_digits_are_same :(ccNumStr.replace((new RegExp(ccNumStr[0],'g')),'').length>0), |
| 52 | + // last_digit_is_even :(ccNumStr[ccNumStr.length-1]%2==0), |
| 53 | + // digit_checksum_greater_than_16:(ccNumStr.split('').reduce((a,b)=>Number(a)+Number(b))>16), |
| 54 | + // }; |
| 55 | + // console.log(" "); |
| 56 | + // console.log(`Checking the validity of Credit card number [${ccNumber}] -> [${ccNumStr}]`); |
| 57 | + // let fResult=true; |
| 58 | + // Object.keys(ruleset).forEach(keyName=>{ |
| 59 | + // // detailed log of functionality |
| 60 | + // console.log(` ${keyName.replace(/_+/g,' ')}${' '.repeat( |
| 61 | + // 35-keyName.length)}[${ruleset[keyName]}]`); |
| 62 | + // // decide validity based on specified ruleset |
| 63 | + // fResult=fResult&&ruleset[keyName]; |
| 64 | + // }); |
| 65 | + // return fResult; |
| 66 | + // } |
| 67 | + // [ '9999777788880000',6666666666661666,' 1234 - 5678 - 9012 - 3456 ',' 1234 5678 9012 3456 ', |
| 68 | + // 'a92332119c011112','4444444444444444',' a6543 b2109 c8765 d1234 ', |
| 69 | + // '1111111111111110',6666666666666661,' 6543 - 2109 - 8765 - 4321 ' ] |
| 70 | + // .forEach(lmnt=>console.log(`Based on the provided rulset, the Credit card number [${ |
| 71 | + // lmnt}] ${(isValidccNumber(lmnt)?'IS':'is NOT')} valid.`)); |
| 72 | + // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = |
| 73 | + |
| 74 | + |
| 75 | + // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = |
| 76 | + // alternate solution with detailed reporting and partial ruleset evaluation |
| 77 | + // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = |
| 78 | + // function isValidccNumber(ccNumber){ |
| 79 | + // // transform into string and remove all spaces |
| 80 | + // let ccNumStr=String(ccNumber).replace(/\s+/g,''); |
| 81 | + // // define validity ruleset based on specifications |
| 82 | + // let ruleset={ |
| 83 | + // is_exactly_16_digits_long :(ccNumStr.length==16), |
| 84 | + // contains_only_numeric_digits :(ccNumStr===String(ccNumber).replace(/[^0-9]/g,'')), |
| 85 | + // not_all_the_digits_are_same :(ccNumStr.replace((new RegExp(ccNumStr[0],'g')),'').length>0), |
| 86 | + // last_digit_is_even :(ccNumStr[ccNumStr.length-1]%2==0), |
| 87 | + // digit_checksum_greater_than_16:(ccNumStr.split('').reduce((a,b)=>Number(a)+Number(b))>16), |
| 88 | + // }; |
| 89 | + // console.log(" "); |
| 90 | + // console.log(`Checking the validity of Credit card number [${ccNumber}] -> [${ccNumStr}]`); |
| 91 | + // return Object.keys(ruleset).every(keyName=>{ |
| 92 | + // // detailed log of functionality |
| 93 | + // console.log(` ${keyName.replace(/_+/g,' ')}${' '.repeat( |
| 94 | + // 35-keyName.length)}[${ruleset[keyName]}]`); |
| 95 | + // // decide validity based on specified ruleset |
| 96 | + // return ruleset[keyName]; |
| 97 | + // }); |
| 98 | + // } |
| 99 | + // [ '9999777788880000',6666666666661666,' 1234 - 5678 - 9012 - 3456 ',' 1234 5678 9012 3456 ', |
| 100 | + // 'a92332119c011112','4444444444444444',' a6543 b2109 c8765 d1234 ', |
| 101 | + // '1111111111111110',6666666666666661,' 6543 - 2109 - 8765 - 4321 ' ] |
| 102 | + // .forEach(lmnt=>console.log(`Based on the provided rulset, the Credit card number [${ |
| 103 | + // lmnt}] ${(isValidccNumber(lmnt)?'IS':'is NOT')} valid.`)); |
| 104 | + // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = |
| 105 | + |
| 106 | + |
| 107 | + // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = |
| 108 | + // replaced with following alternative validation method + alternative data setup & calling |
| 109 | + // evaluation is still partial, meaning at the 1st invalid step the number is ruled invalid |
| 110 | + // this is code revision #4 |
| 111 | + // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = |
| 112 | + // |
| 113 | + // console.log("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = ="); |
| 114 | + // console.log("JS-1 - week 3 - PROJECT: Credit card number Validation"); |
| 115 | + // console.log("Validate the number of a credit card based on specified ruleset"); |
| 116 | + // console.log("- - - - - - - - - - - - - - - - - - - -"); |
| 117 | + // // |
| 118 | + // // define the Credit card number validation routine |
| 119 | + // // |
| 120 | + // function isValidccNumber(ccNumber){ |
| 121 | + // // transform into string and remove all spaces |
| 122 | + // let ccNumStr=String(ccNumber).replace(/\s+/g,''); |
| 123 | + // // define validity ruleset - based on specifications |
| 124 | + // let ruleset={ |
| 125 | + // ccnumber_is_exactly_16_digits_long |
| 126 | + // :(ccNumStr.length==16), |
| 127 | + // contains_only_numeric_digits |
| 128 | + // :(ccNumStr===String(ccNumber).replace(/[^0-9]/g,'')), |
| 129 | + // not_all_the_digits_are_same |
| 130 | + // :(ccNumStr.replace((new RegExp(ccNumStr[0],'g')),'').length>0), |
| 131 | + // the_last_digit_is_even |
| 132 | + // :(ccNumStr[ccNumStr.length-1]%2==0), |
| 133 | + // has_checksum_greater_than_16 |
| 134 | + // :(ccNumStr.split('').reduce((a,b)=>Number(a)+Number(b))>16), |
| 135 | + // }; |
| 136 | + // // decide and return validity - based on specified ruleset |
| 137 | + // return Object.keys(ruleset).every(keyName=>ruleset[keyName]}); |
| 138 | + // } |
| 139 | + // // |
| 140 | + // // call the function to validate each credit card number from the following array |
| 141 | + // // |
| 142 | + // [ '9999777788880000', 6666666666661666, ' 1234 5678 9012 3456 ', |
| 143 | + // ' 1234 - 5678 - 9012 - 3456 ', 'a92332119c011112', '4444444444444444', |
| 144 | + // ' a6543 b2109 c8765 d1234 ', '1111111111111110', 6666666666666661, |
| 145 | + // ].forEach(lmnt=>console.log(`Based on the provided rulset, the Credit card number [${ |
| 146 | + // lmnt}] ${(isValidccNumber(lmnt)?'IS':'is NOT')} valid.`)); |
| 147 | + // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = |
| 148 | + |
| 149 | + |
| 150 | + // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = |
| 151 | + // missing code revision #5 |
| 152 | + // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = |
| 153 | + |
| 154 | + // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = |
| 155 | + // current code revision ( #6 ) |
| 156 | + // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = |
8 | 157 | console.log("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = ="); |
9 | 158 | console.log("JS-1 - week 3 - PROJECT: Credit card number Validation"); |
10 | | - console.log("Validate the number of a credit card based on specified ruleset"); |
| 159 | + console.log("Validate the number of a credit card based on specified ruleset (code revision #6)"); |
11 | 160 | console.log("- - - - - - - - - - - - - - - - - - - -"); |
12 | | - // |
13 | | - // define the Credit card number validation routine |
14 | | - // |
| 161 | + // define the Credit card number validation routine |
15 | 162 | function isValidccNumber(ccNumber){ |
16 | | - // transform to string and remove non-numeric characters |
17 | | - let ccNumStr=String(ccNumber).replace(/[^0-9]/g,''); |
18 | | - // put each character into an array element |
19 | | - let ccDigits=ccNumStr.split(''); |
20 | | - // decide validity based on specified ruleset |
21 | | - return (ccNumStr.length==16) |
22 | | - && (ccNumStr.replace((new RegExp(ccNumStr[0],'g')),'').length>0) |
23 | | - && ((ccNumStr[15]%2)==0) |
24 | | - && (ccDigits.reduce((a,b)=>Number(a)+Number(b))>16); |
25 | | - } |
26 | | - let ccNumberList=[ '9999777788880000','6666666666661666',' 1234 - 5678 - 9012 - 3456 ', |
27 | | - 'a92332119c011112','4444444444444444', |
28 | | - '1111111111111110','6666666666666661',' 6543 - 2109 - 8765 - 4321 ' ]; |
29 | | - for (let i=0; i<ccNumberList.length; i++){ |
30 | | - console.log(`Based on the provided rulset, the Credit card number [${ |
31 | | - ccNumberList[i]}] ${(isValidccNumber(ccNumberList[i])?'IS':'is NOT')} valid.`); |
| 163 | + // define number validation ruleset - based on specifications |
| 164 | + let ccNumberValidationRules={ |
| 165 | + The_number_should_be_Exactly_16_digits_long |
| 166 | + :()=>ccNumberString.length==16, |
| 167 | + The_number_should_consist_of_16_Numeric_digits |
| 168 | + :()=>ccNumberString===String(ccNumber).replace(/[^0-9]/g,''), |
| 169 | + The_number_should_have_at_least_2_Diferent_digits |
| 170 | + :()=>ccNumberString.replace((new RegExp(ccNumberString[0],'g')),'').length>0, |
| 171 | + The_last_digit_of_the_number_should_be_Even |
| 172 | + :()=>ccNumberString[ccNumberString.length-1]%2==0, |
| 173 | + The_number_should_have_digit_Checksum_greater_than_16 |
| 174 | + :()=>ccNumberString.split('').reduce((a,b)=>Number(a)+Number(b))>16, |
| 175 | + }; |
| 176 | + // transform the parameter into new string and remove blank spaces |
| 177 | + let ccNumberString=String(ccNumber).replace(/\s+/g,''); |
| 178 | + console.log(' '); |
| 179 | + // logout the credit card number under inspection |
| 180 | + console.log(`Validation of credit card number [${ccNumber |
| 181 | + }] - converted to [${ccNumberString}]`); |
| 182 | + // decide number validity - check against every rule in sequece |
| 183 | + let validationResult=Object.keys(ccNumberValidationRules).every(currentRule=>{ |
| 184 | + let currentResult=ccNumberValidationRules[currentRule](); |
| 185 | + // on false: report reason the number is not passing this rule |
| 186 | + // since every() will terminate (partial boolean evaluation) |
| 187 | + if (!currentResult) {console.log('Number is INVALID due to rule conflict: ' |
| 188 | + +currentRule.replace(/_+/g,' ')+'.')}; |
| 189 | + return currentResult; |
| 190 | + }); |
| 191 | + // finally, if card is valid, report so before leaving routine |
| 192 | + if (validationResult) {console.log(`This is a VALID credit card number.`)}; |
| 193 | + // return number validation result |
| 194 | + return validationResult; |
32 | 195 | } |
| 196 | + // validate each credit card number from the following array |
| 197 | + [ ' 1234 - 5678 - 9012 - 3456 ', ' 1234 5678 9012 3456 ', |
| 198 | + 'a92332119c011112', '0000777788880000', |
| 199 | + 4444444444444444, 6666666666661666, |
| 200 | + ' 006 543 210 987 651 234 ', ' 0012 3456 7890 1234 ', |
| 201 | + '1111111111111110', 3111111111111110, |
| 202 | + 6666666666666661, 1111111111111116 |
| 203 | + ].forEach(lmnt=>isValidccNumber(lmnt)); |
| 204 | + // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = |
| 205 | + |
33 | 206 | console.log("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = ="); |
34 | 207 | console.log(" "); |
35 | 208 | }; |
|
0 commit comments