Jump to content

Function not functioning


Corbeau

Recommended Posts

Hello. I have a problem that's making me feel like an idiot.

 

I have this function. The exact code is maybe not that important because, if I copy/paste the exact code into the program, it is working (but still appending it just in case). However, when I put it in front of the program, I get "TypeError: rad is not a function".

 

There are other functions so I tried putting him in the same SCRIPT tags, tried to separate them, neither is working.

<script>function rad(x,y){    var temp=0;    if ((y==0) || ((Math.abs(x)/(Math.abs(y)+0.01))>=2)) temp=Math.abs(x);    else temp=Math.abs(y)+div(Math.abs(x),2)+Math.abs(x)%2;    if ((Math.abs(x/(y+.01))<2) && (y<0)) temp=temp-Math.abs(x)%2;    return temp;}</script>

Also, "div" is a defined function and that one is working.

Edited by Corbeau
Link to comment
Share on other sites

Usually this sort of problem is due to not using window.onload to start your code. As the page is loading it loads from top to bottom. To assure that the code is complete before the code runs you can either put your code block at the bottom of the body section or you can use the window.onload or document.ready events to start your code. I usually use...

<!DOCTYPE html><html><head><title></title><script>window.onload = init;function init(){//start my code}function other(){//...}</script></head><body></body></html>

You have to also be careful to not use reserved words for things such as variables or function names. That can also cause errors.

Edited by davej
Link to comment
Share on other sites

Nope. Still "not a function".

 

And I did check for reserved words, it seems "rad" is legit.

 

Is there any difference if the function is placed in the <head> and the rest of the code in <body>? I tried both separately and all within <body>, neither worked.

Link to comment
Share on other sites

You will have to post a better example of how the placement of the code creates your problem. I tried it here and it works...

<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>title</title><script>window.onload = init;function init(){document.getElementById('btn').onclick = put;}function put(){var n = parseInt(document.getElementById('inp').value);document.getElementById('out').innerHTML = rad(n,3);}function div(n,m){return n/m;}function rad(x,y){    var temp=0;    if ((y==0) || ((Math.abs(x)/(Math.abs(y)+0.01))>=2)) temp=Math.abs(x);    else temp=Math.abs(y)+div(Math.abs(x),2)+Math.abs(x)%2;    if ((Math.abs(x/(y+.01))<2) && (y<0)) temp=temp-Math.abs(x)%2;    return temp;}</script></head><body><input type="text" id="inp"/><input type="button" id="btn" value="Enter"/><div id="out"></div></body></html>
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...