-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hemming.html
76 lines (76 loc) · 3.61 KB
/
Hemming.html
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
<html>
<head>
<title>hemmeing code</title>
<script>
function code(){
let mes = document.getElementById('inpMessage').value;
mes = mes.split('');
for(let i = 0; i < mes.length; i++)
mes[i] *= 1;
let control = new Array();
control.push((mes[0] + mes[1] + mes[3]) % 2);
control.push((mes[1] + mes[2] + mes[3]) % 2);
control.push((mes[0] + mes[2] + mes[3]) % 2);
document.getElementById('codedMessage').value = mes.join('') + control.join('');
}
function decode()
{
let codedMessage = document.getElementById('codedMessage').value;
let firstNewControl = (codedMessage[0] * 1 + codedMessage[1] * 1 + codedMessage[3] * 1) % 2
let secondNewControl = (codedMessage[1] * 1 + codedMessage[2] * 1 + codedMessage[3] * 1) % 2
let thirdNewControl = (codedMessage[0] * 1 + codedMessage[2] * 1 + codedMessage[3] * 1) % 2
let error = "";
if (firstNewControl != codedMessage[4] && secondNewControl == codedMessage[5] && thirdNewControl != codedMessage[6])
{
error = "Error in 1";
document.getElementById('decodedMessage').value = (codedMessage[0] * 1 + 1) % 2 + codedMessage.substr(1,3);
}
else if (firstNewControl != codedMessage[4] && secondNewControl != codedMessage[5] && thirdNewControl == codedMessage[6])
{
error = "Error in 2";
document.getElementById('decodedMessage').value = codedMessage[0] + (codedMessage[1] * 1 + 1) % 2 + codedMessage.substr(2,2);
}
else if (firstNewControl == codedMessage[4] && secondNewControl != codedMessage[5] && thirdNewControl != codedMessage[6])
{
error = "Error in 3";
document.getElementById('decodedMessage').value =codedMessage.substr(0,2) + (codedMessage[2] * 1 + 1) % 2 + codedMessage.substr(3,1);
}
else if (firstNewControl != codedMessage[4] && secondNewControl != codedMessage[5] && thirdNewControl != codedMessage[6])
{
error = "Error in 4";
document.getElementById('decodedMessage').value =codedMessage.substr(0,3) + (codedMessage[3] * 1 + 1) % 2;
}
else if (firstNewControl != codedMessage[4] )
{
error = "Error in 5";
document.getElementById('decodedMessage').value = codedMessage.substr(0,4);
}
else if (secondNewControl != codedMessage[5] )
{
error = "Error in 6";
document.getElementById('decodedMessage').value = codedMessage.substr(0,4);
}
else if (thirdNewControl != codedMessage[6] )
{
error = "Error in 7";
document.getElementById('decodedMessage').value = codedMessage.substr(0,4);
}
else
{
error = "No errors"
document.getElementById('decodedMessage').value = codedMessage.substr(0,4);
}
document.getElementById('placeOfError').textContent = error;
}
</script>
</head>
<body>
<h1>Hemming code</h1>
<p> <input type='text' id='inpMessage'> Type the message </p>
<p> <input type='button' value='code' onClick='code()'> </p>
<p> <input type='text' id='codedMessage'> Coded message </p>
<p> <input type='button' value='decode' onClick='decode()'> </p>
<p> <input type='text' id='decodedMessage'> Decoded message </p>
<p> <span id='placeOfError'> </span> </p>
</body>
</html>