87 Replies
this is the code
can someone tell me what did i do wrong
this is the error message im getting
😢
it looks like you're declaring operatorsArray in a block using let, and it goes out of scope at the closing curly brace
querySelectorAll does not return an array
i used const this time and it still didnt work
why not?
you need to use a spread operator or array.from method
That's also a very good reason 😄
spread operator?
[...]
querySelectorAll returns a NodeList
btw queryselectorall never returns an array?
ohh
let operatorsArray = [...document.querySelectorAll('.operator')]
NodeLists are array-like, but not quite arrays
wow
i didnt know these stuff
MDN Web Docs
Spread syntax (...) - JavaScript | MDN
The spread (...) syntax allows an iterable, such as an array or string, to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected. In an object literal, the spread syntax enumerates the properties of an object and adds the key-value pairs to the object being created.
nodelist thats the word i was missing thank you Jochem 😄
omg it seems its working
it just returned false in console log
lets see
i was reading ur message😭
sorry, I misread it again, please ignore 🙂
btw it returns false everytime
even if the condition is true
getComputedStyle won't output "white", I think it just outputs rgb color values
oh ya😭
some people told me to use getcomputedstyle instead of style
how should i get the value of background color in string
.style
only returns the inline styles of an element
why do you need to know the background color in this way?
like, what are you trying to accomplish?Smells of the x/y problem, yeah?
or someone trying to do something and going to it through the scenic route
yeah, definitely smells like that. I've never really had the need to look at the computed style rules in Javascript, there's pretty much always a better, easier option like selecting by class or some data attribute
ohh wait
nvm
actually i dont know how should i know the background color value
do u know any better method
except class
I'm just confused why you need to know it in the first place
The question still remains: what are you trying to do? IE, why do you need the background colour?
ohh
so
wait
We have been lol
actually im making calculator
so whenever i click on an operator button
i want its background color turned white
Can you please use complete sentences and not send tidbits one line at a time?
okk
:active in css should be enough?
and when i click on any numbers button, i wanna know if any operators background color is white
To what end? That's what we're trying to get at. Why do you need to know that?
i wanted to do it through js but i will have to use css if theres no way
so, the easiest way to do that is to just add a class to the button that turns it white, and check if that class is present later
If you're trying to use that to see what operation to use don't use bg colour. Save the button press in a variable and read that
so that i can calculate the first input and second input incase any operator is clicked
oh
yeah, do what Beck said
ohk
You should be saving state in JS, not via HTML/CSS
what does saving state mean?
something like:
So you have a global object that stores what's being used. The first function there changes the opeartor to
+
, -
, /
, or *
as necessary (based on the button pressed) and the array holds the numbers that need be operated on.
So if you type 15 + 4
the 15
is pushed into the array, the +
is added as the operator and then 4
is pused to the array. You'll want to add more functionality to actually do the operation on each operatorButtonClicked
—so the next time you click on, say, -
it'll add 15 and 4, clear the array, and add 19 to the array
I'd suggest using a switch
statement to handle the operation of currentOperator
okay
Bit late, but just some additional info
Instead of the spread operator you can also use Array.from() on the node list.
i would take a different and easier approach
a calculator is much more complicated than it seems
hey i also mentioned that Marky 😡
start out with 0
if the user types a number, replace it with the typed number, up until how manyh digits you wanna handle
the user then presses an operator
store the value in a variable
☝️ @ἔρως
it's an algorithm: a list of steps, but i forgot to put it on a list
continuing
sorry, i missed reading this
don't worry i'm just messing with ya 😄
im up until here
They're typing. Be patient 😉
actually
i have an idea
no i mean im at that step at the moment
actually till the second variable is stored
im in that step
cant think of next step
Edit fiddle - JSFiddle - Code Playground
Test your JavaScript, CSS, HTML or CoffeeScript online with JSFiddle code editor.
here's my approach
yes i remember
i still remember how long it took u to make it
10-15 minutes
big brain
btw how many years of programming experience u had when u made this?
its just an easy approach
still
same as now
but u said u made it last year
yes, but a year is 12 months
and we're 4 months into the new year
and that was 9-10 months into last year
also, i started to program in may/june
oh
whatt
sorry my cat was eating my mouse wire
whatttt
no way
u started programming in may of 2023?
no
it was on a may/june
😭
but which year
2021
2000
1900
been programming for about 18 years
oh
now it makes sense
it's just a matter of simplifying
and being able to recognize all requirements
wtf i just realized this
😭
overcomplicating something incredibly simple
yaa lol
why didn't you just attach event listeners to the buttons?
that would have been far superior and easier than reading background colors
this is my javascript at the moment
omg im getting it
i would use a proper function for the event listeners
you can then use the
this
variable, instead of fetching stuff outside the scopeyeah something weird is happening
im getting undefined when performing the operation (result)
maybe scope issue like u said
where should i make changes?
good question
make a codepen, get an answer
yooo
the problem was that the functions in the switch case was not returning anything
now its fixed
and my calculator is functioning
obviously i need to do more editing
the reset and del doesnt work at the moment
but the operation finally works
looks pretty
thanks
btw guys
can u see something weird is happening when i click " = " repeatedly after doing a minus or divide operation
why is that happening?
should i be worried about that?
also when i do any operations on the default value "0", i get 0 or NaN as the result
thats weird