@@ -4,60 +4,56 @@ function validateCreditNumber(num) {
44 for ( let i = 0 ; i < num . length ; i ++ ) {
55 array . push ( num [ i ] ) ;
66 }
7-
8-
9-
10- //Soloution 1
11-
12- const items = num . split ( '' )
13-
14- if ( num . length <= 16 ) {
15- console . log ( "True" ) ;
16- } else {
17- console . log ( `Invalid! The input ${ num } should be 16 characters!` ) ;
18- } ;
19-
20- //All characters must be numbers
21-
22- if ( num . match ( / ^ [ 0 - 9 ] + $ / ) != null ) {
23- console . log ( "true" )
24- } else {
25- console . log ( `Invalid! The input ${ num } should contain only numbers!` )
26- }
27-
7+
288 //At least two diffrent numbers should be represented
299 function similarity ( array ) {
30- let firstItem = array [ 0 ]
31- let x = array . filter ( elements => elements == firstItem ) . length
32- return x != array . length ? false : true ;
33- }
34- console . log ( similarity ( array ) )
35-
36- //The last number must be even
37- if ( array . pop ( ) % 2 == 0 ) {
38- console . log ( "the last is even" ) ;
39- } else {
40- console . log ( "the last is odd" )
41- }
10+ let firstItem = array [ 0 ]
11+ let x = array . filter ( elements => elements == firstItem ) . length != array . length ? true : false ;
12+ return x
13+ }
14+
15+ //The Sum of all the numbers must be greater than 16:
16+ let sum = 0
17+ for ( let i = 0 ; i < array . length ; i ++ ) {
18+ let arrayInt = parseInt ( array [ i ] )
19+ sum += arrayInt
20+ }
21+
22+ switch ( false ) {
23+
24+ //The length of the number
25+ case num . length == 16 :
26+ console . log ( `Invalid! The input ${ num } should be 16 characters!` ) ;
27+ break ;
28+
29+ //All characters must be numbers
30+ case num . match ( / ^ [ 0 - 9 ] + $ / ) != null :
31+ console . log ( `Invalid! The input ${ num } should contain only numbers!` ) ;
32+ break ;
33+
34+ //At least two diffrent numbers should be represented
35+ case similarity ( array ) == true :
36+ console . log ( `Invalid! The input ${ num } should contain at least 2 different types of numbers!` ) ;
37+ break ;
38+
39+ //The last number must be even
40+ case array . pop ( ) % 2 == 0 :
41+ console . log ( `Invalid! The last number of the input ${ num } should be even` ) ;
42+ break ;
4243
44+ //The Sum of all the numbers must be greater than 16:
45+ case sum > 16 :
46+ console . log ( `Invalid! The sum of the input ${ num } should be more than 16` ) ;
47+ break ;
4348
44- //Soloution2
45-
46- // switch(false){
47- // //The length of the number
48- // case num.length <= 16:
49- // console.log(`Invalid! The input ${num} should be 16 characters!`)
50- // break;
51- // //All characters must be numbers
52- // case num.match(/^[0-9]+$/) != null:
53- // console.log(`Invalid! The input ${num} should contain only numbers!`)
54- // break;
55- // //At least two diffrent numbers should be represented
56-
57-
58- // };
59- } ;
49+ default :
50+ console . log ( `Success! The input ${ num } is a valid credit card number!` )
6051
52+ } ;
53+
54+ } ;
6155
6256
63- validateCreditNumber ( '6666666666666661' ) ;
57+ validateCreditNumber ( '6666666666661666' ) ;
58+ validateCreditNumber ( 'a92332119c011112' ) ;
59+ validateCreditNumber ( '4444444444444444' ) ;
0 commit comments