-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
59 lines (40 loc) · 1.32 KB
/
index.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
function start(){
document.getElementById("list_view").innerHTML = "";
document.getElementById("min_view").innerHTML = "";
var txt = document.getElementById('txt').value;
var bin_array = txt.split(",").map(function(item) {
return item.trim();
});
// console.log(bin_array);
let { min, print_element } = hd_finder(bin_array);
print_element.forEach(element => document.getElementById("list_view").innerHTML += element);
if(min == 1000){
document.getElementById("min_view").innerHTML = "<b> Something went wrong </b>";
}else{
document.getElementById("min_view").innerHTML = "<b> Minimum Hamming Distance: " + min + "</b>";
}
}
function hd_finder(bin_array){
let print_element = [];
let min = 1000;
for(let i=0; i<bin_array.length; i++){
for(let j=i+1; j<bin_array.length; j++){
hd = xor_one_count(bin_array[i], bin_array[j]);
min = (min>hd) ? hd : min;
print_element.push("<p>" + bin_array[i] + " ^ " + bin_array[j] + " -> " + hd + "<p>");
}
}
return {
'min': min,
'print_element': print_element
}
}
function xor_one_count(a, b){
let count = 0;
for(let i=0; i<a.length; i++){
if(a[i] != b[i]){
count++;
}
}
return count;
}