By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,564 Members | 947 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,564 IT Pros & Developers. It's quick & easy.

help with script

P: 1
I need to make a script that will calculate the moon phase based on the moon's position... it's for an online game

here's what I've got - members locate the position by viewing the page source and it's always a number

then they would enter that number into an input box and submit it, which then would figure the answer one of 2 ways:

if the number is >=180, it subtracts 157.5 and then divides by 22.5, rounds the number and that's the answer

if the number is <180, it adds 202.5 and then divides by 22.5, rounds the number and that's the answer

I've posted on several different coding forums to try to get help: I wrote a script once before with lots of help from other people but I didn't know half of what I was doing or even what it all meant, just that it did what I wanted it to...

so on one forum someone posted this:

Expand|Select|Wrap|Line Numbers
  1. function calculate(position) {
  2.   var phase;
  3.   position = parseFloat(position);
  4.   if (position > 180) {
  5.      phase = Math.round((position - 157.5) / 22.5);
  6.      provideAnswer(phase);
  7.      }
  8.   else if (position < 180) {
  9.      phase = Math.round((position - 202.5) / 22.5);
  10.      provideAnswer(phase);
  11.      }
  12.   else {
  13.     var text = document.createTextNode("You must enter a number!");
  14.     document.getElementsByTagName("div")[0].appendChild(text);
  15.     }
  16.   }
  17.  
  18. function provideAnswer(phase) {
  19.   // Do Something
  20.   }
and the html code was
Expand|Select|Wrap|Line Numbers
  1. <div>
  2.   <input type="text" id="position">
  3.   <input type="submit" onclick="calculate('position');">
  4. </div>
all that did was write You must enter a number! over and over again

then he said that the code was wrong and that calculate('position') should be calculate(document.getElementById('position').valu e) but when I changed the values in both the script and the html it just disabled it completely

please help?
Feb 23 '07 #1
Share this Question
Share on Google+
1 Reply

acoder
Expert Mod 15k+
P: 16,027
Try
Expand|Select|Wrap|Line Numbers
  1. var pos = parseFloat(position);
then replace position in the if statements with pos.

You should also check for invalid numbers using isNaN(pos).
Feb 23 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.