-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebstuff.js
46 lines (42 loc) · 1.64 KB
/
webstuff.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
window.onload = function() {
document.getElementById("calculate").onclick = function() {
displayResults();
};
document.getElementById("input_box").onkeypress = function() {
if (window.event.keyCode == 13) displayResults();
};
createNumbers();
["+", "-", "*", "/", "^"].forEach(createOperators);
};
function displayResults() {
var expression = document.getElementById("input_box").value;
var maths_results = maths(expression);
if (isNaN(maths_results)) {
document.getElementById("result").innerHTML = "Uh oh! Error!";
document.getElementById("result_text").innerHTML = "";
}
else {
document.getElementById("result").innerHTML = maths_results;
document.getElementById("result_text").innerHTML = "" + expression + " = ";
}
}
function createNumbers() {
for (var i = 0; i < 10; i++) {
var newNumber = document.createElement("div");
newNumber.innerHTML = i;
var br = document.createElement("br");
newNumber.onclick = function() {
document.getElementById("input_box").value = document.getElementById("input_box").value + this.innerHTML;
};
document.getElementById("numbers").appendChild(newNumber);
if (i === 4) document.getElementById("numbers").appendChild(br);
}
}
function createOperators(element, index, array) {
var newOp = document.createElement("div");
newOp.innerHTML = element;
newOp.onclick = function() {
document.getElementById("input_box").value = document.getElementById("input_box").value + this.innerHTML;
};
document.getElementById("operators").appendChild(newOp);
}