PRV-Website/calculator.js

167 lines
4.6 KiB
JavaScript

let operator;
let operatorSelected = false;
let displayMem;
let equalsPerformed = false;
let fresh = true;
const display = document.getElementById("display");
const operatorDivision = document.getElementById("division");
const operatorMultiplication = document.getElementById("multiplication");
const operatorMinus = document.getElementById("minus");
const operatorPlus = document.getElementById("plus");
function appendToDisplay(input){
if(input=='.' && display.value.includes('.')){
return;
}
if(operatorSelected){
if(operator == '+'){
operatorPlus.style.backgroundColor = "#ff9f0a";
operatorPlus.style.color = "white";
}else if(operator == '-'){
operatorMinus.style.backgroundColor = "#ff9f0a";
operatorMinus.style.color = "white";
}else if(operator == '*'){
operatorMultiplication.style.backgroundColor = "#ff9f0a";
operatorMultiplication.style.color = "white";
}else if(operator == '/'){
operatorDivision.style.backgroundColor = "#ff9f0a";
operatorDivision.style.color = "white";
}
displayMem = display.value;
display.value = "";
operatorSelected = false;
}
operatorSelected = false;
display.value += input;
equalsPerformed = false;
fresh=false;
}
function setOperator(input){
if(operatorSelected){
operatorPlus.style.backgroundColor = "#ff9f0a";
operatorPlus.style.color = "white";
operatorMinus.style.backgroundColor = "#ff9f0a";
operatorMinus.style.color = "white";
operatorMultiplication.style.backgroundColor = "#ff9f0a";
operatorMultiplication.style.color = "white";
operatorDivision.style.backgroundColor = "#ff9f0a";
operatorDivision.style.color = "white";
}
if(!fresh){
if(input == '+'){
operatorPlus.style.backgroundColor = "white";
operatorPlus.style.color = "#ff9f0a";
}else if(input == '-'){
operatorMinus.style.backgroundColor = "white";
operatorMinus.style.color = "#ff9f0a";
}else if(input == '*'){
operatorMultiplication.style.backgroundColor = "white";
operatorMultiplication.style.color = "#ff9f0a";
}else if(input == '/'){
operatorDivision.style.backgroundColor = "white";
operatorDivision.style.color = "#ff9f0a";
}
}
if(displayMem != null && !operatorSelected && !equalsPerformed){
if(operator == '+'){
display.value = Number(displayMem) + Number(display.value);
}else if(operator == '-'){
display.value = Number(displayMem) - Number(display.value);
}else if(operator == '*'){
display.value = Number(displayMem) * Number(display.value);
}else if(operator == '/'){
display.value = Number(displayMem) / Number(display.value);
}
}
equalsPerformed = false;
if(display.value != ""){
operator = input;
operatorSelected = true;
}
}
function getPercentage(){
if(display.value != ""){
display.value = Number(display.value)/100;
equalsPerformed = false;
}
}
function clearDisplay(){
operatorPlus.style.backgroundColor = "#ff9f0a";
operatorPlus.style.color = "white";
operatorMinus.style.backgroundColor = "#ff9f0a";
operatorMinus.style.color = "white";
operatorMultiplication.style.backgroundColor = "#ff9f0a";
operatorMultiplication.style.color = "white";
operatorDivision.style.backgroundColor = "#ff9f0a";
operatorDivision.style.color = "white";
operatorSelected = false;
operator = "";
display.value = "";
displayMem = "";
equalsPerformed = false;
fresh = true;
}
function changeKeySignature(){
if(display.value != ""){
display.value = -1 * Number(display.value);
equalsPerformed = false;
}
}
function calculate(){
if(displayMem != null && !operatorSelected){
if(operator == '+'){
display.value = Number(displayMem) + Number(display.value);
}else if(operator == '-'){
display.value = Number(displayMem) - Number(display.value);
}else if(operator == '*'){
display.value = Number(displayMem) * Number(display.value);
}else if(operator == '/'){
display.value = Number(displayMem) / Number(display.value);
}
}
operatorSelected = false;
equalsPerformed = true;
}