-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
149 lines (132 loc) · 4.68 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// 1. Your calculator is going to contain functions for all of the basic math operators you typically find on simple calculators,
// so start by creating functions for the following items and
// testing them in your browser’s console.
// console.log(addition(9, 7))
// console.log(subtraction(9, 8))
// console.log(multiplication(7, 6))
// console.log(division(3, 4))
// Sum opretion
function addition(a, b) {
return a + b;
};
// Subtraction opretion
function subtraction(a, b) {
return a - b;
};
// Multiply opretion
function multiplication(a, b) {
return a * b;
};
// Divide opretion
function division(a, b) {
return a / b;
};
// percentage opretion
function percentage(a){
return a % a;
}
// 2 . Create a new function operate() that takes an operator and 2 numbers and then calls one of the above functions on the numbers.
function operateFun(num1, num2, operatorValue) {
num1 = Number(num1)
num2 = Number(num2)
switch (operatorValue) {
case "+":
return addition(num1, num2);
case "-":
return subtraction(num1, num2);
case "*":
return multiplication(num1, num2);
case "/":
return division(num1, num2);
case "%":
return percentage(num1)
}
};
// 4 . Create the functions that populate the display when you click the number buttons.
let inputBox = document.querySelector('input')
let numberButtons = document.querySelector('.numberButton')
let opretionButtons = document.querySelectorAll('.opretion')
let clearButton = document.querySelector('.acClearAll')
let quealButton = document.querySelector('.opretionButton')
let dotButton = document.querySelector('.dotButton')
let inputBoxstring = "";
let arrayFromInputBox = [];
let num1 = "";
let num2 = "";
let operatorValue = "";
let found_Operator = false;
let dotAdd = false;
let result;
let nu;
let valueCheckerArray = ["1","2","3","4","5","6","7","8","9","0"]
// this function is scan the array values and return the num1 num2 and opreater
function scanArray(arrayFromInputBox){
for(let i = 0; i < arrayFromInputBox.length; i++){
if(arrayFromInputBox[i] in valueCheckerArray && found_Operator == false){
num1 += arrayFromInputBox[i];
}
// else if (arrayFromInputBox[i] =='+'||arrayFromInputBox[i] =='%'||arrayFromInputBox[i] =='-'||arrayFromInputBox[i] =='*'||arrayFromInputBox[i] =='/'
// && found_Operator == false){
// num1 += arrayFromInputBox[i];
// }
else if (arrayFromInputBox[i] === "." && dotAdd == false && found_Operator == false){
num1 += arrayFromInputBox[i];
dotAdd = true;
}
else if (arrayFromInputBox[i] =='+'||arrayFromInputBox[i] =='%'||arrayFromInputBox[i] =='-'||arrayFromInputBox[i] =='*'||arrayFromInputBox[i] =='/'){
if (found_Operator == true) {
operatorValue = arrayFromInputBox[i];
dotAdd = false;
}
else{
found_Operator = true;
dotAdd = false;
operatorValue = arrayFromInputBox[i];
}
}
else if (arrayFromInputBox[i] === "." && dotAdd == false && found_Operator == true){
num2 += arrayFromInputBox[i];
dotAdd = true;
}
else if (arrayFromInputBox[i] in valueCheckerArray && found_Operator == false){
num1 += arrayFromInputBox[i];
}
else if (arrayFromInputBox[i] in valueCheckerArray && found_Operator == true){
num2 += arrayFromInputBox[i];
}
}
}
// this function when return value undefined ex: 0*0
unfind(inputBox.value);
function unfind(){
if (inputBox.value == undefined){
inputBox.value = 0;
}
}
//this function perfectly enter the value in input box
let dotDisplay = false;
function display(value){
let found_module = false;
if (value == "." && dotDisplay == false){
document.getElementById("inputvalue").value += value;
dotDisplay = true;
}
else if (value == "%" && inputBox.value == "" && found_module == false){
document.getElementById("inputvalue").value += value;
found_module = true;
}
// Extra Credit
// Users can get floating point numbers if they do the math required to get one, but they can’t type them in yet. Add a . button
// and let users input decimals! Make sure you don’t let them type more than one though: 12.3.56.5. It is hard to do math on these
// numbers. (disable the decimal button if there’s already one in the display)
else if(value == "." && dotDisplay == true){
dotDisplay = true
}
else if (value == "/" || value == "*" ||value == "-" ||value == "+" ||dotDisplay == true){
dotDisplay = false;
document.getElementById("inputvalue").value += value;
}
else {
document.getElementById("inputvalue").value += value;
}
}