Jump to content
Sign in to follow this  
Corbeau

Function not functioning

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

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
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>

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...