-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
42 lines (33 loc) · 1.11 KB
/
script.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
"use strict";
let submit = document.getElementById('submit');
let barcode = document.getElementById('value');
let result = document.getElementById('result');
submit.addEventListener('click', function() {
let toCheck = barcode.value;
let split = [];
let sumodd = 0;
let sumeven = 0;
let sum = 0;
if (toCheck.length != 12) {
result.innerHTML = "Incorrect length";
} else {
result.innerHTML = '';
split = String(toCheck).split('');
for (let i = 0; i < split.length; i++) {
if (i % 2 == 0 || i == 0) {
sumeven += Number(split[i]);
} else if (i % 2 != 0 || i != 0) {
sumodd += Number(split[i]);
}
}
sumodd *= 3;
sum = sumeven + sumodd;
if (sum % 10 == 0) {
result.innerHTML = `Your check digit is 0 making the full barcode ${toCheck}0`;
} else {
let mod = sum % 10;
let modtwo = 10 - mod;
result.innerHTML = `Your check digit is ${modtwo} making the full barcode ${toCheck}${modtwo}`;
}
}
});