-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
71 lines (65 loc) · 2.33 KB
/
index.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
document.querySelector("#equal").addEventListener('click', calculate);
var tocalculate = document.querySelector("#tocalculate");
var result_show = document.querySelector("#result_show");
var buttons = document.querySelectorAll('#first-line button,#left-content button,#right-content button');
for (i = 5; i < buttons.length - 1; i++) {
buttons[i].addEventListener('click', writing);
}
buttons[0].addEventListener('click', empty_result);
buttons[1].addEventListener('click', addtomemory);
buttons[2].addEventListener('click', clear_memory);
buttons[3].addEventListener('click', delete_last);
function empty_result() {
tocalculate.innerHTML = "";
result_show.innerHTML = "";
}
function clear_memory (){
document.querySelector('#memory').innerHTML=null;
}
function add_event_listener() {
var memo = document.querySelectorAll('.memo');
for (i = 0; i < memo.length; i++) {
memo[i].addEventListener('click', printmemo);
}
}
function printmemo() {
tocalculate.innerHTML = this.innerHTML;
}
function addtomemory() {
if (result_show.innerHTML != "") {
var paragraph = document.createElement('p');
paragraph.innerHTML = result_show.innerHTML.slice(1);
paragraph.classList.add("memo");
document.querySelector('#memory').appendChild(paragraph);
add_event_listener();
}
}
function calculate() {
var tangent = "Math.tan(";
var tangent_input = "tan(";
var cosine = "Math.cos(";
var cosine_input = "cos(";
var sine = "Math.sin(";
var sine_input = "sin(";
var sqrt_input = "sqrt(";
var sqrt = "Math.sqrt(";
var log_input = "log(";
var log = "Math.log(";
var LN2_input = "LN2(";
var LN2 = "Math.LN2(";
var final_string = ((((((tocalculate.innerHTML.replace(/x/g, "*")).replace(tangent_input, tangent)).replace(cosine_input, cosine)).replace(sine_input, sine)).replace(sqrt_input, sqrt)).replace(log_input, log)).replace(LN2_input, LN2);
var result_test=eval(final_string);
if(result_test=="Infinity"){
result_test="black hole"
result_show.innerHTML = "=" + result_test ;
}
if(typeof result_test!='undefined'){
result_show.innerHTML = "=" + result_test ;
}
}
function writing() {
tocalculate.innerHTML += this.value;
}
function delete_last() {
tocalculate.innerHTML = tocalculate.innerHTML.slice(0, -1);
}