Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    New Coder
    Join Date
    Oct 2006
    Location
    Pittsburg, KS
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    need script for this:

    Hello everyone, I'm needing a basic calculator script with some twists to it...

    I need to have a form where a member enters their moon position - which is a number

    then when they click submit, the form calculates the actual phase

    if the number they enter is >180 it first subtracts 157.5 then divides by 22.5 and rounds up or down

    if the number they enter is <180 it adds 202.5 then divides by 22.5 and rounds up or down

    the answer is provided as both a number and an image - but I can easily add that no problem...

    I've hunted everywhere for a script, but can't find one because I'm really not sure what it would even be called..

    If anyone can help me with this by either pointing me in the right direction or whatever, please let me know, I'm going to start working on the basics to it though

    thanks in advance!
    ~phoenix

  • #2
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,316
    Thanks
    29
    Thanked 280 Times in 274 Posts
    Try something like this:

    JavaScript\:
    Code:
    function calculate(position) {
      var phase;
      position = parseFloat(position);
      if (position > 180) {
         phase = Math.round((position - 157.5) / 22.5);
         provideAnswer(phase);
         }
      else if (position < 180) {
         phase = Math.round((position - 202.5) / 22.5);
         provideAnswer(phase);
         }
      else {
        var text = document.createTextNode("You must enter a number!");
        document.getElementsByTagName("div")[0].appendChild(text);
        }
      }
    
    function provideAnswer(phase) {
      // Do Something
      }
    HTML:
    Code:
    <div>
      <input type="text" id="position">
      <input type="submit" onclick="calculate('position');">
    </div>
    For every complex problem, there is an answer that is clear, simple, and wrong.

  • #3
    New Coder
    Join Date
    Oct 2006
    Location
    Pittsburg, KS
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I did try that, without any changes to see it in action, in the backend of my site all I ever get whenever I enter something is that I need to enter a number and no matter what it just repeats that over and over again, writing it over and over again...

    I just don't know enough about javascript for squat

  • #4
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,316
    Thanks
    29
    Thanked 280 Times in 274 Posts
    Quote Originally Posted by phoenix1215 View Post
    I did try that, without any changes to see it in action, in the backend of my site all I ever get whenever I enter something is that I need to enter a number and no matter what it just repeats that over and over again, writing it over and over again...

    I just don't know enough about javascript for squat
    There’s a mistake in the script: calculate('position') should read calculate(document.getElementById('position').value). The script itself is partial though and not designed to do anything out of the box.
    For every complex problem, there is an answer that is clear, simple, and wrong.

  • #5
    New Coder
    Join Date
    Oct 2006
    Location
    Pittsburg, KS
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    alrighty then, well thanks for the help

  • #6
    New Coder
    Join Date
    Oct 2006
    Location
    Pittsburg, KS
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Arbitrator View Post
    There’s a mistake in the script: calculate('position') should read calculate(document.getElementById('position').value). The script itself is partial though and not designed to do anything out of the box.
    I played around with this and it completely disabled it, I'm lost... and I don't even know where to begin

  • #7
    New Coder
    Join Date
    Jan 2007
    Posts
    93
    Thanks
    0
    Thanked 0 Times in 0 Posts
    here in one in two lines of code.


    Code:
    <html>
    <head>
    <script type="text/JavaScript">
    
    function moon(){
    var pos= parseFloat(document.getElementById('pos').value)
    document.getElementById('pos').value= (Math.round(pos > 180 ? (pos-157.5)/22.5 : (pos+202.5)/22.5) || "enter a number")
    }
    
    </script>
    </head>
    <body>
    <input type="text" id="pos">
    <input type="submit" onclick="moon()">
    
    </body>
    </html>
    Last edited by tonyp12; 02-23-2007 at 01:56 AM.

  • #8
    New Coder
    Join Date
    Oct 2006
    Location
    Pittsburg, KS
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by tonyp12 View Post
    here in one in two lines of code.


    Code:
    <html>
    <head>
    <script type="text/JavaScript">
    
    function moon(){
    var pos= parseFloat(document.getElementById('pos').value)
    document.getElementById('pos').value= (Math.round(pos > 180 ? (pos-157.5)/22.5 : (pos+202.5)/22.5) || "enter a number")
    }
    
    </script>
    </head>
    <body>
    <input type="text" id="pos">
    <input type="submit" onclick="moon()">
    
    </body>
    </html>
    omg! that's awesome! thank you so much, do you know how I could get the answer to show up beneath the input box?

  • #9
    New Coder
    Join Date
    Jan 2007
    Posts
    93
    Thanks
    0
    Thanked 0 Times in 0 Posts
    change the second line of code so it starts with:
    document.getElementById('answ').innerHTML=

    add this to the hml:
    <br>The answer is : <span id="answ"></span>
    Last edited by tonyp12; 02-23-2007 at 02:42 AM.

  • #10
    New Coder
    Join Date
    Oct 2006
    Location
    Pittsburg, KS
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by tonyp12 View Post
    change the second line of code to:
    document.getElementById('answ').innerHTML=


    add this to the hml:

    <br>The answer is : <span id="answ"></span>
    very very cool... you are the man!!!

    I was going out of my mind trying to figure out that on earth was going on with that other code, though it was giving me a very good idea of how things work, except that I couldn't get it to do anything other than say "You must enter a number!"

  • #11
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,316
    Thanks
    29
    Thanked 280 Times in 274 Posts
    *sigh* Invalid HTML and proprietary code. Below demonstrates another way of doing it with correct code. If you’re interested in what all this stuff is doing, try reading QuirksMode: W3C DOM Compatibility - Core.

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
      "http://www.w3.org/TR/html4/strict.dtd">
    
    <html lang="en-US">
      <head>
    
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta http-equiv="Content-Script-Type" content="text/javascript">
        <meta name="Author" content="Patrick Garies">
        <title>HTML 4.01 Strict Document</title>
    
        <style type="text/css">
          * { margin: 0; }
          html { padding: 1em; }
          .NaN { color: red; }
          .phase { color: green; }
        </style>
    
        <script type="text/javascript">
          function calculateMoonPhase(position) {
            // Variables
            var phase, calculator = document.getElementById("MoonPhaseCalculator");
            position = parseFloat(document.getElementById("MoonPosition").value);
    
            // Delete Existing Output
            if (calculator.lastChild.firstChild) {
              calculator.removeChild(calculator.lastChild);
              }
    
            /* If the value is a number, calculate the phase.
               Otherwise, tell the user to enter a numerical value. */
            if (position > 180) {
              phase = Math.round((position - 157.5) / 22.5);
              }
            else if (position < 180) {
              phase = Math.round((position - 202.5) / 22.5);
              }
            else if (position == isNaN) {
              var output = document.createElement("div");
              output.className = "NaN";
              output.appendChild(document.createTextNode("You must enter a number!"));
              calculator.appendChild(output);
              }
    
            /* If the value was a number, output the calculation. */
            if (position != isNaN) {
              var output = document.createElement("div");
              output.className = "phase";
              output.appendChild(document.createTextNode("The phase is " + phase + "."));
              calculator.appendChild(output);
              }
            }
        </script>
    
      </head>
      <body>
    
        <div id="MoonPhaseCalculator">
          <label for="MoonPosition">Input Moon Position:</label>
          <input type="text" id="MoonPosition">
          <input type="submit" onclick="calculateMoonPhase()">
        </div>
    
      </body>
    </html>
    For every complex problem, there is an answer that is clear, simple, and wrong.

  • #12
    New Coder
    Join Date
    Oct 2006
    Location
    Pittsburg, KS
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Arbitrator View Post
    *sigh* Invalid HTML and proprietary code. Below demonstrates another way of doing it with correct code. If you’re interested in what all this stuff is doing, try reading QuirksMode: W3C DOM Compatibility - Core.

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
      "http://www.w3.org/TR/html4/strict.dtd">
    
    <html lang="en-US">
      <head>
    
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta http-equiv="Content-Script-Type" content="text/javascript">
        <meta name="Author" content="Patrick Garies">
        <title>HTML 4.01 Strict Document</title>
    
        <style type="text/css">
          * { margin: 0; }
          html { padding: 1em; }
          .NaN { color: red; }
          .phase { color: green; }
        </style>
    
        <script type="text/javascript">
          function calculateMoonPhase(position) {
            // Variables
            var phase, calculator = document.getElementById("MoonPhaseCalculator");
            position = parseFloat(document.getElementById("MoonPosition").value);
    
            // Delete Existing Output
            if (calculator.lastChild.firstChild) {
              calculator.removeChild(calculator.lastChild);
              }
    
            /* If the value is a number, calculate the phase.
               Otherwise, tell the user to enter a numerical value. */
            if (position > 180) {
              phase = Math.round((position - 157.5) / 22.5);
              }
            else if (position < 180) {
              phase = Math.round((position - 202.5) / 22.5);
              }
            else if (position == isNaN) {
              var output = document.createElement("div");
              output.className = "NaN";
              output.appendChild(document.createTextNode("You must enter a number!"));
              calculator.appendChild(output);
              }
    
            /* If the value was a number, output the calculation. */
            if (position != isNaN) {
              var output = document.createElement("div");
              output.className = "phase";
              output.appendChild(document.createTextNode("The phase is " + phase + "."));
              calculator.appendChild(output);
              }
            }
        </script>
    
      </head>
      <body>
    
        <div id="MoonPhaseCalculator">
          <label for="MoonPosition">Input Moon Position:</label>
          <input type="text" id="MoonPosition">
          <input type="submit" onclick="calculateMoonPhase()">
        </div>
    
      </body>
    </html>
    thank you so much for the link... I guess that's my biggest problem, I know what I want but not how to search for it and then most of the tutorials that I find aren't very clear... I learned html, css, and php from scratch - but when I start dealing with things inside my joomla installation I get befuddled a lot

    anyway, thank you so much for you help! you've both been awesome!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •