I have a simple calculator. the numbers are seen on the screen when you press to the buttons but the numbers still can see on the screen if you press the buttons even you turn off the calculator after you did some process. how can I fix it?
yes, it was my first js project, i will try to reduce repetition. thanks
I tried to remove event listener at 363. js code line but it’s didn’t work .
I did some changes the code but still not work well.
Look
case del.innerHTML:
if (math.innerHTML == ""){
}
else if(result.innerHTML != "") result.innerHTML = "";
else
math.innerHTML = math.innerHTML.slice(0, math.innerHTML.length -1);
This is a good hint that you need to get back to basics, and come back later to this project.
Empty if statements, bodging around logic, in my opinion you are not ready for this project.
Implicit “blockless {}” else statements? Please, is like trying to run when you cannot walk.
I am not trying to put you down but rather to encourage you to go back to be comfortable with the basics.
This for example is a crime:
else if(math.innerHTML[math.innerHTML.length - 1] == "-")
math.innerHTML += "";
else if(math.innerHTML[math.innerHTML.length - 1] == "*")
math.innerHTML += "";
else if(math.innerHTML[math.innerHTML.length - 1] == "/")
math.innerHTML += "";
maybe
// -. *, / etc
if(["-","*", "/"].includes(math.innerHTML[math.innerHTML.length - 1] )) {
math.innerHTML += ""; // you only need to do it once
}
just an example.
The calculator one is a hard one, I suggest you come back to it when you have more understanding otherwise you will only be frustrated.
Only my opinion, take it with a pinch of salt.
I really want to help with your current project but is so unreadable it gives me headache.
ALSO, please try to write a function per functionality.
The buttonValue does way too much and makes it very hard to debug.
That should be split in 5-6 functions probably and then you can inspect easily what goes wrong.
I understand your suggestion and accept that I’m amator in js. But, I think your samples is wrong. You say the code should be like this
if(["-","*", "/"].includes(math.innerHTML[math.innerHTML.length - 1] )) {
math.innerHTML += "";
}
okay but if I an amateur, how can i code like this?
I think your suggestion code is hard me.
I code like this becase I’m an amateur.
else if(math.innerHTML[math.innerHTML.length - 1] == "-")
math.innerHTML += "";
else if(math.innerHTML[math.innerHTML.length - 1] == "*")
math.innerHTML += "";
else if(math.innerHTML[math.innerHTML.length - 1] == "/")
math.innerHTML += "";
I will try to perform your suggestions.
Why don’t you start with the javascript challenges until you are comfortable with conventional coding?
If you get to have the basics about:
-
Scope, Scope - MDN Web Docs Glossary: Definitions of Web-related terms | MDN, You-Dont-Know-JS/ch1.md at 1st-ed · getify/You-Dont-Know-JS · GitHub, You-Dont-Know-JS/ch2.md at 1st-ed · getify/You-Dont-Know-JS · GitHub
-
Control flow Control flow - MDN Web Docs Glossary: Definitions of Web-related terms | MDN, Control flow and error handling - JavaScript | MDN
-
Functions in depth, what is an early return (probably this is the scope of this topic)
-
DOM api
,then you can dive into projects like the js calculator.
This project is a nasty one, is genuinely hard one.
If you are really keen to do it though, get a working example from codepen and try to understand that already working solution before writing yours.
Hope you see my point.
Yes, I see the point. But I think real problem is that you can’t understanding what your weakness without projects. I completed w3schools js course but you can’t remember all of them without doing some projects / coding. I searched basic js projects and seen this project, actly it worked well to my expectation, it’s just keep write the values to screen even it turn off, i tried to fix that. I will study again the basics.
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.