Tell us what’s happening:
I’m stuck in the logic of the calculator problem after the user presses the operator and presses the second number. I do I keep track of the second number entered. for instance when I press 2 then 5 it calls the handle number function which takes the argument value and passes the value to the display variable then the update() function gets called and passes the display value to the screen.innertext. The part I’m confused about is how can I keep track of the second number once I type in for instance 25 * (2) how can I keep track of the second value. Thank you an advance. my complete code
let screen=document.querySelector('.screen');
let display='0';
function takevalue(value){
if(isNaN(parseInt(value))){
handlesymbol(value);
}else{
handlenumber(value);
}
update();
}
function doMath(value){
if(display==='0'){
return;
}
}
function handlesymbol(value){
if(value==='C'){
display='0';
}
else if(value==='x'){
doMath(value);
}else if(value==='+'){
doMath(value);
}else if(value==='-'){
doMath(value);
}else if(value==='/'){
doMath(value);
}
}
function handlenumber(value){
if(display==='0'){
display=value;
}else{
display+=value;
}
}
function update(){
screen.innerText=display;
}
document.querySelector(".calcbody").addEventListener('click', function(event){
takevalue(event.target.innerText);
});
I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.
making sure I have the right idea thank you for the post .so for example would
an expression like 25*2 have a prop initial value of 25 and a prev value of “*” and finally an operand value of “2”
> Blockquote
let state={
initial:25,
prev:"*"
operand:"2"
}
Here start with this I wrote for you, I didnt change your code around too much, just organized what you want to do ( i.e the idea I was trying to convey… to point you in the right direction.).You will still need to pass the rest of the tests, let me know if you want more help:
js:
//set the initial screen
let screen = document.getElementById("display")
let StartingVal=0
let state={
currentScreen:StartingVal,
prev:"",
operand:"",
}
let {currentScreen,prev,operand}=state
screen.innerHTML=currentScreen
const handleChange=keyPressed=>{
let s;
if(keyPressed==="C"){
prev=""
operand=""
currentScreen=StartingVal
return screen.innerText=currentScreen
}
if(currentScreen[0]===undefined){
currentScreen=keyPressed
return screen.innerText=currentScreen
}
if(currentScreen[0]==="0"){
s = currentScreen.substring(1);
currentScreen=s
}
currentScreen+=keyPressed
screen.innerText=currentScreen
}
document.querySelector(".calcbody").addEventListener("click", function (event) {
handleChange(event.target.innerText)
});
// statediv.innerHTML=JSON.stringify(state, 2)