-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJavascript.js
62 lines (61 loc) · 1.35 KB
/
Javascript.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
const N = parseInt(readline());
var invalid = 0;
var invalidA = [];
for (let i = 0; i < N; i++) {
const ISBN = readline();
checkISBN(ISBN);
}
function checkISBN(isbn) {
if (isbn.length == 10) {
checkISBN10(isbn);
} else if (isbn.length == 13) {
checkISBN13(isbn);
} else {
invalid++;
invalidA.push(isbn);
}
}
function checkISBN10(isbn) {
var digits = isbn.split("");
var sum = 0;
for (var i = 0; i < 9; i++) {
sum += parseInt(digits[i]) * (10 - i);
}
checkDigits = sum % 11;
if (checkDigits != 0) {
checkDigits = 11 - checkDigits;
}
if (digits[9] == "X" && checkDigits != "10") {
invalid++;
invalidA.push(isbn);
} else if (digits[9] != "X" && parseInt(digits[9]) != checkDigits) {
invalid++;
invalidA.push(isbn);
}
}
function checkISBN13(isbn) {
var digits = isbn.split("");
var sum = 0;
for (var i = 0; i < 12; i++) {
if (i % 2 == 0) {
sum += parseInt(digits[i]);
} else {
sum += parseInt(digits[i]) * 3;
}
}
checkDigits = sum % 10;
if (checkDigits != 0) {
checkDigits = 10 - checkDigits;
}
if (digits[12] == "X") {
invalid++;
invalidA.push(isbn);
} else if (parseInt(digits[12]) != checkDigits) {
invalid++;
invalidA.push(isbn);
}
}
console.log(invalid + " invalid:");
for (var i = 0; i < invalidA.length; i++) {
console.log(invalidA[i]);
}