PRV-Website/calculator.js

167 lines
4.6 KiB
JavaScript
Raw Normal View History

2024-04-26 16:57:08 +02:00
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){
2024-04-26 20:35:51 +02:00
if(input=='.' && display.value.includes('.')){
return;
}
2024-04-26 16:57:08 +02:00
if(operatorSelected){
if(operator == '+'){
operatorPlus.style.backgroundColor = "#ff9f0a";
operatorPlus.style.color = "white";
2024-06-01 14:19:32 +02:00
2024-04-26 16:57:08 +02:00
}else if(operator == '-'){
operatorMinus.style.backgroundColor = "#ff9f0a";
operatorMinus.style.color = "white";
2024-06-01 14:19:32 +02:00
2024-04-26 16:57:08 +02:00
}else if(operator == '*'){
operatorMultiplication.style.backgroundColor = "#ff9f0a";
operatorMultiplication.style.color = "white";
2024-06-01 14:19:32 +02:00
2024-04-26 16:57:08 +02:00
}else if(operator == '/'){
operatorDivision.style.backgroundColor = "#ff9f0a";
operatorDivision.style.color = "white";
2024-06-01 14:19:32 +02:00
2024-04-26 16:57:08 +02:00
}
displayMem = display.value;
display.value = "";
operatorSelected = false;
}
2024-06-01 14:19:32 +02:00
operatorSelected = false;
2024-04-26 16:57:08 +02:00
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;
}