const press = document.querySelectorAll('input');
counter = 0;
let integers = "";
press.forEach(item => {
item.addEventListener('click', (e)=>{
if(counter === 1){
term1 = input;
console.log(term1);
integers = "";
}
if(counter === 2){
term2 = input;
console.log(term2);
integers = "";
}
if(e.target.id === "number"){
let number = (e.target.value);
integers += number;
}
if(e.target.id === "operator"){
input = parseFloat(integers);
console.log(integers);
counter++;
}
})
});
I'm new to Javascript and have been working on a simple calculator I'm trying to build. The numbers have a value in html based on the number they are (i.e. the value for 1 is value="1" in html. Operators have an id of "operator" and numbers have an id of "number". In my code, I try to have things set so my integer value is reset to it's initial value of an empty string once an operator is clicked. This is so the input for numbers comes out correctly. Unfortunately, once my counter hits 2, I get a value of NaN logged into the console.
It seems that the value for integers is kept as an empty string rather than being reassigned its value through the number variable like it did initially. I am just trying to find out why this value isn't reassigned through my loops, I'm sure I made a big mistake somewhere but I'm just not seeing it. I know this is a terrible way to make a calculator, but I am working on logic so that I get a good grasp of programming in general, and also to make things on my own so that I'm not stuck copying code and actually learning. Sorry if I made any dumb mistakes, but any help would be appreciated.
Aucun commentaire:
Enregistrer un commentaire