-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
36 lines (32 loc) · 1.05 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
function appendToDisplay(value) {
document.getElementById('display').value += value;
}
function clearDisplay() {
document.getElementById('display').value = '';
}
let random
function calculate() {
try {
let expression = document.getElementById('display').value;
random=Math.random()
// Faulty operation swap 10% of the time
if (random < 0.1) {
expression = expression.replace(/\+/g, 'tempPlus').replace(/\*/g, '-').replace(/\//g, '+').replace(/-/g, '/').replace(/tempPlus/g, '*');
}
let result = eval(expression);
document.getElementById('display').value = result;
} catch (error) {
document.getElementById('display').value = 'Error';
}
finally{
displayMsg(random)
}
}
function displayMsg(valRandom){
if(valRandom < 0.1){
document.getElementById("Message").innerHTML= "Result: OOPS! Wrong Answer! Try Again"
}
else{
document.getElementById("Message").innerHTML = "Result: You Got It Right!"
}
}