forked from HackYourFuture/JavaScript1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPROJECT.js
More file actions
80 lines (59 loc) · 2.12 KB
/
PROJECT.js
File metadata and controls
80 lines (59 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
'use strict';
let num = 4444444444444444 ; // not valid
let num1 = 1000111111111110 ; //not valid
let num2 = 9999999999999991 ; //not valid
let num3 = 8899777755550000 ; //vaild
let num4 = 5554446669991666 ; //valid
checkCard(num);
checkCard(num1);
checkCard(num2);
checkCard(num3);
checkCard(num4)
function checkCard(creditNum){
// it must be 16 numbers
let length ;
// change the number to a string so it has length property
let creditNumString = creditNum.toString();
//change the number to an array that so it has one string of the number
let creditNumArr = creditNumString.split(',');
// check if the number has 16 digits
if (creditNumString.length< 16 || creditNumString.length > 16) {
length = false;
} else {
length = true;
}
// The final digit must be even
let finalIsEven;
if (creditNumString[15] % 2 === 0) {
finalIsEven = true;
} else {
finalIsEven = false;
}
// The sum of all the digits must be greater than 16
let sumHigherThan16
// to be able to sum the numbers, must to make it an arry
let creditNumArrOfStrings = [];
for (var i = 0; i < creditNumString.length; i++) {
creditNumArrOfStrings.push(parseFloat(creditNumString[i]));
}
// this calculates the numbers
const reducer = (accumulator, currentValue) => accumulator + currentValue;
const sum = creditNumArrOfStrings.reduce(reducer);
if (sum <= 16) {
sumHigherThan16 = false;
} else {
sumHigherThan16 = true;}
// the digits cannot be the same
let notTheSameNum ;
// make it an arry
const allEqual = creditNumArrOfStrings => creditNumArrOfStrings.every( v => v === creditNumArrOfStrings[0] );
if (allEqual(creditNumArrOfStrings)) {
notTheSameNum = false;
} else {
notTheSameNum = true;
}
if (length && notTheSameNum && finalIsEven && sumHigherThan16) {
console.log('Valid credit card Number');
} else { console.log(`${creditNum} is not valid. Please enter another number`);
}
}