Skip to content

Commit 907ed60

Browse files
authored
Merge pull request #6 from Armigerousmonk/week3-AmirHossein
AmirHossein-Javascript1-Week3-creditCard Project
2 parents d25ecb6 + e4849c8 commit 907ed60

1 file changed

Lines changed: 62 additions & 0 deletions

File tree

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
'use strict'
2+
function validateCreditNumber(num) {
3+
const array = [];
4+
for(let i = 0; i < num.length; i++){
5+
array.push(num[i]);
6+
}
7+
8+
//At least two diffrent numbers should be represented
9+
function similarity(array){
10+
let firstItem = array[0]
11+
let x = array.filter(elements => elements == firstItem).length != array.length ? false : true;
12+
return x
13+
}
14+
15+
//The Sum of all the numbers must be greater than 16:
16+
let sum = 0
17+
const lastNum = array[array.length -1];
18+
for (let i = 0; i < array.length; i++){
19+
let arrayInt = parseInt(array[i])
20+
sum += arrayInt
21+
}
22+
23+
switch(true){
24+
25+
//The length of the number
26+
case num.length !== 16:
27+
console.log(`Invalid! The input ${num} should be 16 characters!`);
28+
break;
29+
30+
//All characters must be numbers
31+
case num.match(/^[0-9]+$/) == null:
32+
console.log(`Invalid! The input ${num} should contain only numbers!`);
33+
break;
34+
35+
//At least two diffrent numbers should be represented
36+
case similarity(array) == true:
37+
console.log(`Invalid! The input ${num} should contain at least 2 different types of numbers!`);
38+
break;
39+
40+
//The last number must be even
41+
case lastNum % 2 !== 0:
42+
console.log(`Invalid! The last number of the input ${num} should be even`);
43+
44+
break;
45+
46+
//The Sum of all the numbers must be greater than 16:
47+
case sum < 16:
48+
console.log(`Invalid! The sum of the input ${num} should be more than 16`);
49+
break;
50+
51+
default:
52+
console.log(`Success! The input ${num} is a valid credit card number!`)
53+
54+
};
55+
56+
};
57+
58+
59+
validateCreditNumber('66666666666561666');
60+
validateCreditNumber('a92332119c011112');
61+
validateCreditNumber('4444444444444441');
62+
validateCreditNumber('1111111111111110');

0 commit comments

Comments
 (0)