Okay, so the answer to my question might not be the problem but here’s a go anyway.
Basically this is one part of a calculator program. I need to know how I can take the var y on only the first execution of this function (assuming it will likely be executed repeatedly) and have the variable x be converted to that value and hold that value for each subsequent execution … while still allowing for output.original_value to be changed when another function is called that wants to change its value. This is the only part of my program that I have not been able to figure out.
Essentially I am trying to emulate a common calculator function where if the user presses 1+2 and then = the calculation will render 3 and then will increase by the value of the second number (2 in this case) for each subsequent pressing of = (ie. 1+2=3 = 5 = 7….) my calculator right now is doing the opposite (ie. 1+2 = 3 = 4 = 5 = 6………).
(I have tried many many things… for now at the end of the function I have mathSign = null which prevents the program from being run more than once without another function first reseting the values… I did not leave that part in the posted code because my end goal is to not have to use it.)
function result(){
var x = output.original_value;
var y = parseFloat(output.value);
if(mathSign == '+'){
output.value = x + y;
}
else if(mathSign == '-'){
output.value = x - y;
}
else if(mathSign == '*'){
output.value = x * y;
}
else if(mathSign == '/'){
output.value = x / y;
}
else if(mathSign == 'n^'){
output.value = Math.pow(x,y);
}
else if(mathSign == 'reciprocal'){
output.value = 1 / output.original_value;
mathSign = null;
}
else if(mathSign == 'sqrt'){
output.value = Math.sqrt(output.original_value);
mathSign = null;
}
output.result = output.value;
}
This ended up being the best way to solve my problem. It is in essence a variable switcher and works amazingly with the rest of my calculator code 🙂