-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
54 lines (54 loc) · 1.33 KB
/
Copy pathmain.js
File metadata and controls
54 lines (54 loc) · 1.33 KB
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
$(document).ready(function(){
var inputs = [""];
var operators1 = ["+", "-", "*", "/"];
var operators2 = ["."];
var nums = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
var totalString;
function getValue(input){
//To prevent duplicate decimal points
if(operators2.includes(inputs[inputs.length - 1])===true && input==="."){
console.log("Duplicate '.'");
}
//To prevent entry of first operator except "-" operator
else if(inputs.length === 1 && input === "-"){
inputs.push(input);
}
//To prevent double operators
else if(operators1.includes(inputs[inputs.length - 1]) === false && inputs.length > 1){
inputs.push(input);
}
//To enter a number
else if(nums.includes(Number(input))){
inputs.push(input);
}
}
function update(){
totalString = inputs.join("");
$("#output").html(totalString);
}
function getTotal(){
totalString = inputs.join("");
$("#output").html(eval(totalString));
}
$("button").on("click", function(){
if(this.id === "deleteAll"){
inputs = [""];
update();
}
else if(this.id === "backOne"){
inputs.pop();
update();
}
else if(this.id === "="){
getTotal();
}
else if(inputs[inputs.length - 1].indexOf("+", "-", "*", "/")===-1){
getValue(this.id);
update();
}
else{
getValue(this.id);
update();
}
});
});