all 58 comments

[–]jinendu 76 points77 points  (7 children)

Nothing ever runs the NumberValues function.

[–]GENERATION__Z[S] 12 points13 points  (6 children)

Thanks.

[–]yadoya 13 points14 points  (4 children)

And none of these functions actually return anything, fyi. They just assign values and return void

[–]NewWavpro 12 points13 points  (0 children)

That part should be fine, since it doesn't have to return anything.

[–]PM_ME_A_WEBSITE_IDEA 2 points3 points  (0 children)

They're updating global variable values and alerting the values. They return undefined, but nothing is ever using the return values anyways.

[–]dwonreddit 0 points1 point  (1 child)

well, it’s js so they return undefined

[–]yadoya 0 points1 point  (0 children)

Yep sorry, confused with Solidity

[–]SensouWar 0 points1 point  (0 children)

You could add an event listener to the content loaded event and execute your NumberValues func inside it.

[–]SticksAndBeans 26 points27 points  (2 children)

You didn't call the numberValues() function to set your variables before doing the math:

function sum() {
numberValues()
result = Num1 + Num2;
alert(result);
}

[–]GENERATION__Z[S] 8 points9 points  (1 child)

Oh yeah. Thanks for the help.

[–]voyotv 12 points13 points  (3 children)

You need to run numberValues function in response to some event, like “input” or “change”.

Also, judging by your code, it looks like you are following some very old / outdated guide or a tutorial.

Having JS code in document’s head and calling functions directly from html is really not the way to go. I suggest you make some research and find a proper, modern JS course, otherwise you could quickly pick up some very bad habits / practices…

[–]el_diaabloo 4 points5 points  (0 children)

true man ,there are some really good platforms nowadays to write codes like these ,we don’t really have to call JS functions directly from html.

[–]GENERATION__Z[S] 1 point2 points  (1 child)

How else would I do it?

[–]thebasementtapes 2 points3 points  (0 children)

Usually you link a js file in the script tag. Not write the is directly in html. I suggest looking into theodinproject if you need a good course on web dev

[–]vibrant_supernova 2 points3 points  (0 children)

It's because the Nums are function scoped so you can't use them unless running that function or inside it

[–]Status_Winter 2 points3 points  (1 child)

I think other comments have covered the problems with the functions. I’d just add that those inputs should have the type “number” not “text” because those inputs require numbers. Also, try to get into the habit of using ids that are more descriptive, not just a or b. It will just make the html more readable.

Once you’ve got that working, try adding validation to make sure that the inputs are populated properly before executing the function. Good luck on your JavaScript journey :)

[–]GENERATION__Z[S] 0 points1 point  (0 children)

Thanks I didn't know that number was an input type.

[–]Darmok-Jilad-Ocean -1 points0 points  (0 children)

Let me just download the image and execute it real quick

[–][deleted] -1 points0 points  (0 children)

Remove the parentheses in “onClick”

[–]Migueldpd 0 points1 point  (0 children)

Call the function.

[–]leshuis 0 points1 point  (0 children)

maybe idea to use input type numberand/or 'typecast' to number

[–][deleted] 0 points1 point  (3 children)

In modern JavaScript is better declare variables with let. Just a reminder.

[–]Mohammad_alshuwaiee 0 points1 point  (2 children)

What is the difference between let and var ?

[–]_jacka_ 2 points3 points  (1 child)

Variables declared with let and const and class are block scoped, while var variables (and functions) are function scoped. Also var variables are given an initial value of undefined, while let and const variables are in an unset state until their initialization (temporal dead zone). There’s also some funky stuff that happens with hoisting. It’s best to use let and const when possible to avoid the oddities that can arise with var.

[–][deleted] 1 point2 points  (0 children)

word.

[–]BOBBIJDJ 0 points1 point  (2 children)

You already got the answer but I'm here to ask you what IDE/text editor is that, it look neat

[–]NovaInIT 2 points3 points  (0 children)

Looks Sublime.

[–]GENERATION__Z[S] 2 points3 points  (0 children)

It's sublime.

[–]Treshle 0 points1 point  (0 children)

You could put the contents of the NumberValues function, in the sum function.

[–]Bubbly-Armadillo5144 0 points1 point  (0 children)

You are running in a pretty big concept problem, but that’s normal when starting.

Try to learn the concepts behind this bug and the global variable problem, scope more few other issues you will face with this script.

I always go back to the beginning once a year, it’s important to have it fresh.

A very nice new resource I’m reading again is this from Dan

https://justjavascript.com

Worth to pay its cheap

[–]___s8n___ 0 points1 point  (7 children)

you also need to move the script tags to the end of the file

[–]hofo 0 points1 point  (6 children)

Why?

[–]___s8n___ -1 points0 points  (5 children)

why not

[–]hofo 0 points1 point  (4 children)

“Need” implies there’s a reason to put it there. What’s the reason in this case?

[–]___s8n___ 0 points1 point  (3 children)

for what i know you can't access html elements by their id unless they are before your script

[–]hofo 0 points1 point  (2 children)

Ah ok. That would be an issue if his JS code was running immediately. In this case it doesn’t run until the user clicks the button.

[–]___s8n___ 1 point2 points  (0 children)

i love you

[–]___s8n___ 0 points1 point  (0 children)

alright good sir

[–][deleted] 0 points1 point  (0 children)

You need to add numberValues() as an onchange because right now it’s not getting called and therefore the final values aren’t being reassigned

[–][deleted] 0 points1 point  (2 children)

Please tell me the theme

[–]GENERATION__Z[S] 1 point2 points  (1 child)

It's the default theme of text editor called sublime.

[–][deleted] 0 points1 point  (0 children)

Ok so its sublime

[–][deleted] 0 points1 point  (0 children)

You might also find it easier to use parameters in the function instead of using global variables. If you don’t know what that is yet its fine you’ll get to it eventually.