Skip to content

Commit 1500345

Browse files
authored
Merge pull request #1 from grozevg/layout
Adding layout.
2 parents 77005ad + 42c68e6 commit 1500345

File tree

4 files changed

+146
-41
lines changed

4 files changed

+146
-41
lines changed

index.html

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
<link rel="stylesheet" href="style.css">
6+
</head>
7+
8+
<body>
9+
<h1>Grozev's Calendar</h1>
10+
11+
<div id="myCalculator">
12+
13+
</div>
14+
<script src="script.js"></script>
15+
</body>
16+
17+
</html>

init.js

-41
This file was deleted.

script.js

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
"use strict";
2+
3+
var Calculator = function (container) {
4+
this.container = container;
5+
this.init();
6+
};
7+
8+
Calculator.prototype = {
9+
init: function () {
10+
document.myCalculators = document.myCalculators || [];
11+
document.myCalculators.push(this);
12+
this.createLayout();
13+
this.str = 0;
14+
this.updateDisplay();
15+
},
16+
17+
input: function (input) {
18+
if (this.str)
19+
this.str += input;
20+
else
21+
this.str = input;
22+
23+
this.updateDisplay();
24+
},
25+
26+
output: function (input) {
27+
return this.validate() || eval(this.str);
28+
},
29+
30+
validate: function () {
31+
var str = this.str;
32+
var reg = /[^1-9,0,*,/,\-,+,%, ,(,)]/g;
33+
var result = str.match(reg);
34+
// return ;
35+
if (result || str.endsWith("+") || str.endsWith("-") || str.endsWith("*") || str.endsWith("/")) {
36+
return "Invalid: " + (result || "End of character.");
37+
}
38+
39+
},
40+
41+
updateDisplay: function (result) {
42+
document.getElementById("c-header").innerHTML = result || this.str;
43+
},
44+
45+
sum() {
46+
this.updateDisplay(this.output());
47+
},
48+
49+
clear: function () {
50+
this.str = 0;
51+
this.updateDisplay();
52+
// console.log(this.output());
53+
},
54+
55+
createLayout: function () {
56+
var cont = document.getElementById(this.container);
57+
cont.appendChild(this.createHeader());
58+
cont.appendChild(this.createBody());
59+
},
60+
61+
createHeader: function () {
62+
var cHeader = document.createElement("div");
63+
cHeader.innerHTML = `<div id="c-header">
64+
<span id="c-display">Display</span>
65+
</div>`;
66+
return cHeader;
67+
68+
},
69+
70+
//no function
71+
createButton(text, i) {
72+
var button = document.createElement("button");
73+
button.innerHTML = text;
74+
var buttonObserver = this.buttonObserver;
75+
button.addEventListener("click", function () { buttonObserver(this.innerHTML) });
76+
return button;
77+
78+
},
79+
80+
buttonObserver(a) {
81+
var currentCalc = document.myCalculators[0];
82+
if (a == "C") {
83+
currentCalc.clear();
84+
}
85+
else if (a == "=") {
86+
currentCalc.sum();
87+
}
88+
else {
89+
currentCalc.input(a);
90+
}
91+
},
92+
93+
createBody() {
94+
var cBody = document.createElement("div");
95+
for (var i = 0; i < 10; i++) {
96+
cBody.appendChild(this.createButton(i));
97+
}
98+
99+
cBody.appendChild(this.createButton("+"));
100+
cBody.appendChild(this.createButton("-"));
101+
cBody.appendChild(this.createButton("*"));
102+
cBody.appendChild(this.createButton("/"));
103+
cBody.appendChild(this.createButton("="));
104+
cBody.appendChild(this.createButton("("));
105+
cBody.appendChild(this.createButton(")"));
106+
cBody.appendChild(this.createButton("C"));
107+
108+
cBody.style.background = "green";
109+
return cBody;
110+
}
111+
112+
};
113+
114+
var a = new Calculator("myCalculator");
115+
//sum("51 + (5 / 2 * 85 + 1)");//Invalid: End of character.
116+
//sum("5 / 2 * 85 + 1asd"); // Invalid: a,s,d
117+
//sum("5 + (5 / 2 * 85 + 1)") // 218.5
118+
119+
120+
//a.clear();
121+
122+
function sum(string) { a.input(string) }
123+
124+

style.css

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#c-header {
2+
height: 20px;
3+
border: 1px solid #000;
4+
background-color: #ccc;
5+
}

0 commit comments

Comments
 (0)