-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathluhnChecksum.js
64 lines (60 loc) · 1.68 KB
/
luhnChecksum.js
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
//Luhn Checksum code
var luhnChecksumValidation = function() {
//document.getElementById('checksum_validation').addEventListener('click', readIndividualDigits);
};
var handleDoubleDigits = function(digit) {
var doubleDigit = digit * 2;
var sum;
if (doubleDigit >= 10) {
sum = 1 + doubleDigit % 10;
} else {
sum = doubleDigit;
}
return sum;
};
var readIndividualDigits = function() {
var digit = prompt('Enter your number for validation: ');
var checksum = 0;
for (var i = 0; i < digit.length; i++) {
//handle odd or even length numbers
if (digit.length % 2 === 0) {
if (i % 2 === 0) {
checksum += parseInt(digit[i]);
} else {
var double = handleDoubleDigits(parseInt(digit[i]));
checksum += double;
}
} else {
if (i % 2 === 1) {
checksum += parseInt(digit[i]);
} else {
var double = handleDoubleDigits(parseInt(digit[i]));
checksum += double;
}
}
};
if (checksum % 10 === 0) {
alert('Checksum is divisible by 10. Valid.');
} else {
alert('Checksum is not divisible by 10. Invalid');
}
};
//positive or negative code
var positiveOrNegative = function() {
var positiveCount = 0;
var negativeCount = 0;
for (var i = 0; i < 10; i++) {
var number = prompt('Please enter a positive or negative number: ');
if (parseInt(number) > 0) {
positiveCount++;
} else {
negativeCount++;
}
}
var response = prompt('Do you want the (p)ositive or (n)egative count?');
if (response.toLowerCase() === 'p') {
alert('The number of positive numbers is: ' + positiveCount);
} else {
alert('The number of negative number is: ' + negativeCount);
}
};