PRV-Website/calculator.js

175 lines
5.5 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){
if(operatorSelected){
if(operator == '+'){
operatorPlus.style.backgroundColor = "#ff9f0a";
operatorPlus.style.color = "white";
/*operatorPlus.addEventListener('mouseover', function(e) {
e.target.style.backgroundColor = "#feb84e";});
operatorPlus.addEventListener('mouseleave', function(e) {
e.target.style.backgroundColor = "#ff9f0a";});*/
}else if(operator == '-'){
operatorMinus.style.backgroundColor = "#ff9f0a";
operatorMinus.style.color = "white";
/*operatorMinus.addEventListener('mouseover', function(e) {
e.target.style.backgroundColor = "#feb84e";});
operatorMinus.addEventListener('mouseleave', function(e) {
e.target.style.backgroundColor = "#ff9f0a";});*/
}else if(operator == '*'){
operatorMultiplication.style.backgroundColor = "#ff9f0a";
operatorMultiplication.style.color = "white";
/*operatorMultiplication.addEventListener('mouseover', function(e) {
e.target.style.backgroundColor = "#feb84e";});
operatorMultiplication.addEventListener('mouseleave', function(e) {
e.target.style.backgroundColor = "#ff9f0a";});*/
}else if(operator == '/'){
operatorDivision.style.backgroundColor = "#ff9f0a";
operatorDivision.style.color = "white";
/*operatorDivision.addEventListener('mouseover', function(e) {
e.target.style.backgroundColor = "#feb84e";});
operatorDivision.addEventListener('mouseleave', function(e) {
e.target.style.backgroundColor = "#ff9f0a";});*/
}
displayMem = display.value;
display.value = "";
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;
}