-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
128 lines (110 loc) · 4.88 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
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
let inputWordOne = document.getElementById('wordOne');
let inputWordTwo = document.getElementById('wordTwo');
let inputWordSum = document.getElementById('wordSum');
let inputWordOneTransmited = document.getElementById('wordOneTransmited');
let inputWordTwoTransmited = document.getElementById('wordTwoTransmited');
let inputWordSumInverted = document.getElementById('wordSumInverted');
let inputWordSumInverted2 = document.getElementById('wordSumInverted2');
let inputWordSumReceived = document.getElementById('wordSumReceived');
let inputWordResult = document.getElementById('wordResult');
let spanResultTransmission = document.querySelector('.resultTransmission');
let optionsTransmissionStatus = document.querySelectorAll('.optionStatus');
let buttonToTransmit = document.getElementById('toTransmit');
let transmissionCorrect = false;
inputWordOne.addEventListener('keyup', function () {
inputWordOne.value = inputWordOne.value.replace(/[^0-1]/g,'');
})
inputWordTwo.addEventListener('keyup', function () {
inputWordTwo.value = inputWordTwo.value.replace(/[^0-1]/g,'');
})
inputWordOne.addEventListener('blur', function () {
inputWordSum.value = sumWords(parseInt(inputWordOne.value, 2), parseInt(inputWordTwo.value, 2));
inputWordOne.value = defineNumberWithHeightBits(inputWordOne.value);
})
inputWordTwo.addEventListener('blur', function () {
inputWordSum.value = sumWords(parseInt(inputWordOne.value, 2), parseInt(inputWordTwo.value, 2));
inputWordTwo.value = defineNumberWithHeightBits(inputWordTwo.value);
})
buttonToTransmit.addEventListener('click', function () {
let whatWordIncorret;
let numOfError = Math.floor(Math.random() * 7) + 1;
inputWordSumInverted.value = invertSum(document.getElementById('wordSum').value);
inputWordSumInverted2.value = inputWordSumInverted.value;
if (transmissionCorrect) {
inputWordOneTransmited.value = inputWordOne.value;
inputWordTwoTransmited.value = inputWordTwo.value;
} else if (!transmissionCorrect) {
whatWordIncorret = Math.floor(Math.random()*3);
if (!whatWordIncorret) {
inputWordSumInverted.value = sumWords(parseInt(inputWordSumInverted.value, 2), numOfError);
inputWordSumInverted2.value = inputWordSumInverted.value;
} else if (whatWordIncorret === 1) {
inputWordOneTransmited.value = sumWords(parseInt(inputWordOne.value, 2), numOfError);
inputWordTwoTransmited.value = inputWordTwo.value;
} else if (whatWordIncorret === 2) {
inputWordOneTransmited.value = inputWordOne.value;
inputWordTwoTransmited.value = sumWords(parseInt(inputWordTwo.value, 2), numOfError);
}
}
inputWordSumReceived.value = sumWords(parseInt(inputWordOneTransmited.value, 2), parseInt(inputWordTwoTransmited.value, 2));
inputWordResult.value = sumWords(parseInt(inputWordSumInverted2.value, 2), parseInt(inputWordSumReceived.value, 2));
verifyResult();
setTransmissionStatus();
})
function generateRandomNumbers() {
let num1 = Math.floor(Math.random() * 255);
let num2 = Math.floor(Math.random() * 255);
inputWordSum.value = sumWords(num1, num2);
num1 = defineNumberWithHeightBits(num1.toString(2));
num2 = defineNumberWithHeightBits(num2.toString(2));
inputWordOne.value = num1;
inputWordTwo.value = num2;
}
generateRandomNumbers();
setTransmissionStatus();
function defineNumberWithHeightBits(num) {
while (num.length < 8) {
num = '0' + num;
}
return num;
}
function sumWords(numOne, numTwo) {
return defineNumberWithHeightBits((numOne + numTwo).toString(2));
}
function setTransmissionStatus() {
if (optionsTransmissionStatus[0].checked) {
transmissionCorrect = true;
} else if (optionsTransmissionStatus[1].checked) {
transmissionCorrect = false;
} else {
transmissionCorrect = Math.round(Math.random());
}
}
function invertSum(wordSum) {
let wordInverted = '';
for (let i = 0; i < wordSum.length; i++) {
if (wordSum[i] === '0') wordInverted += '1';
else if (wordSum[i] === '1') wordInverted += '0';
}
return wordInverted;
}
function verifyResult() {
let thisWord = inputWordResult.value;
let transmissionIsValid = true;
for (let i = 0; i < thisWord.length; i++) {
if (thisWord[i] === '0') {
transmissionIsValid = false;
}
}
if (transmissionIsValid) {
inputWordResult.classList.remove('resultIncorrect');
inputWordResult.classList.add('resultCorrect');
spanResultTransmission.style.color = '#00FF00';
spanResultTransmission.textContent = 'Transmissão CORRETA!';
} else {
inputWordResult.classList.remove('resultCorrect');
inputWordResult.classList.add('resultIncorrect');
spanResultTransmission.style.color = '#FF0000';
spanResultTransmission.textContent = 'Transmissão INCORRETA!';
}
}