Ask coding questions

← Back to all posts
JS function not running
isaiah08 (72)

Try running this program. You will get an error message, saying tools() is not a function. But if you go to the JS code, you see that tools() is a function. This also happens for the materials function. Why?

Answered by OldWizard209 (1531) [earned 5 cycles]
View Answer
OldWizard209 (1531)

It is probably because you have also defined an array to the variable tools and materials. Now because programming languages are interpreted in line-order, which is interpreting line 1, then line 2, then line 3, so on. That way, in the computer's memory, tool is a variable first. So when you name it to a function, JS gets confused. So what you can do is, rename the variable tools to toolsVariable and the function to toolsFunction. Do the same for materials. Your code should then work.

Here is the code:

Coder100 (18146)

You defined a variable with the same name! That is a big no-no. Unless you understand shadowing, don't do it! Rename the variable/function!