#Self made JS calculator not displaying values, just NaN

24 messages · Page 1 of 1 (latest)

fleet ember
#

I've seen this before...

#

you're incorrectly trying to set innerText inside the switch()

versed torrent
#

sorry, i had it moved in the updated version. Now it's undefined

#

thanks for the help thus far

versed torrent
#

@fleet ember any idea thx

past fable
#

have you tried debugging with devtools?

versed torrent
#

i have not, im a little new so ive just been toying in codepen

#

is it better to pull it into vscode?

past fable
#

nah, just open devtools (right click and "inspect"), add a debugger; (javascript keyword for setting a hardcoded-debug-breakpoint) somewhere near where you set the result and run it by clicking "calculate"

#

it'll let you inspect all the different variables by hovering over them with the mouse cursor

#

like you are using html elements as if they were integers, thats not how that works

versed torrent
#

so i can't just create a div and use its id to send an integer val?

past fable
#

no, they are mere handles to DOMElements, though they are objects that are designed to give you every bit of info and interaction you could ever want from them

#

you set variables called heightNum and lengthNum in the beginning, just add .value ad the end of the getElementById() calls and don't forget to wrap the entire thing in a parseInt to convert the returned string in .value to an integer

fleet ember
#

parseInt() won't work so well for fractions 😉

versed torrent
#

parseFloat() then right

#

love u guys

fleet ember
#

or Number()

versed torrent
#

I'm still getting a NaN error.

#

perhaps .innerText isn't the right function for a float var?

fleet ember
#

log it and see?

#

oh, to assign the result? you can pass a number just fine. that's not what's giving you NaN

#

you're calling parseFloat(lengthValue) and parseFloat(heightValue) 1000 times. why not just assign the parsed number to lengthValue and heightValue from the start?

#

your issue is you're assigning .value to those variables when your script loads, not in the click handler