Results 1 to 7 of 7

Thread: Need to do a simple arithmetic operation

  1. #1
    Join Date
    Apr 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need to do a simple arithmetic operation

    Hi folks, need a little help writing out some code here. Disclaimer: my coding knowledge is non-existent, but I'm a quick learner.

    I'm using blogspot.com to host a site and need to take in a numerical value from the user, do some basic arithmetic to that value, and then spit out the result.

    Specifically, I'd like to calculate the number of pages given the number of words. For example, say the word count (input user value) is 1,335. I'd like my html code to divide that number by 300 and spit out the answer (in decimal form if necessary).

    Thanks in advance for your help!

    S.

  2. #2
    Join Date
    Jun 2007
    Posts
    543
    Thanks
    3
    Thanked 78 Times in 78 Posts
    Blog Entries
    1

    Default

    paginate(1000); would result in "3 33/100"
    Code:
    <script type="text/javascript">
    function paginate(num) {
    if (typeof (num+0) == "number") {//if num is a number
    var pagesT = num/300;//number of pages
    var remainder = ((num%300)/300==0)?"":(((num%300)/300)+"").substr(2,2)+"/100";//if there is a remainder, convert to n/100
    var pagesP = pagesT-remainder;//pages to display
    pagesP=pagesP+" "+remainder;// add up all of the results into a string
    alert(pagesP);
    } else {
    alert("Inputed value is not a number!"); //if num is not a number
    }
    }
    </script>
    Last edited by Master_script_maker; 04-15-2008 at 08:42 PM.
    [Jasme Library (Javascript Motion Effects)] My Site
    /\/\@§†ê® §©®¡þ† /\/\@|{ê®
    There are 10 kinds of people in the world, those that understand binary and those that don't.

  3. #3
    Join Date
    Apr 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Thanks for your reply! But it doesn't work

    Thanks for your reply master script maker.

    Unfortunately when I tried to run it on blogspot.com nothing happened. Didn't even see a text box to enter the page count.

    Maybe I'm posting in the wrong section. I need html coding for a website. I think I may need the coding in terms of a <form> rather than a <script>. Don't know enough about coding to be sure though.

    Appreciate any further help!

  4. #4
    Join Date
    Jun 2007
    Posts
    543
    Thanks
    3
    Thanked 78 Times in 78 Posts
    Blog Entries
    1

    Default

    oh sorry, i only thought you needed the script. place this in the body:
    Code:
    <script type="text/javascript">
    function paginate(num) {
    if (typeof (num+0) == "number") {//if num is a number
    var pagesT = num/300;//number of pages
    var remainder = ((num%300)/300==0)?"":(((num%300)/300)+"").substr(2,2)+"/100";//if there is a remainder, convert to n/100
    var pagesP = pagesT-remainder;//pages to display
    pagesP=pagesP+" "+remainder;// add up all of the results into a string
    alert("Pages: "+pagesP);
    } else {
    alert("Inputed value is not a number!"); //if num is not a number
    }
    }
    </script>
    <form method="get" action="#">
    Number of words: <input type="text" name="words" id="words"><br>
    <input type="button" value="Calculate the number of pages" onclick="paginate(document.getElementById('words').value);">
    </form>
    [Jasme Library (Javascript Motion Effects)] My Site
    /\/\@§†ê® §©®¡þ† /\/\@|{ê®
    There are 10 kinds of people in the world, those that understand binary and those that don't.

  5. #5
    Join Date
    Apr 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default thanks! sorry to bug you again...

    I've been trying to get this thing working for awhile now with not much success. So I appreciate your help thus far!

    Just need to fix one more thing. I used the new code you provided and it successfully provides the user with a text box asking for the word count, but unfortunately the answer is not displayed anywhere for the user to see (apologies for not being clear with what I was looking for.)

    Basically i'd like to enter the word count, and then have the answer *displayed* on the same screen.

    Thanks again for all your help!

  6. #6
    Join Date
    Jun 2007
    Posts
    543
    Thanks
    3
    Thanked 78 Times in 78 Posts
    Blog Entries
    1

    Default

    my last script was buggy, try this:
    Code:
    <script type="text/javascript">
    function paginate(num) {
    if (typeof (parseInt(num)) == "number") {//if num is a number
    var pagesT = num/300;//number of pages
    var remainder = (num%300)/300;
    var remainderD = (remainder==0)?"":(remainder+" ").substr(2,2)+"/100";//if there is a remainder, convert to n/100
    var pagesP = pagesT-remainder;//pages to display
    pagesP=pagesP+" "+remainderD;// add up all of the results into a string
    document.getElementById("pages").innerHTML=pagesP;
    } else {
    alert("Please enter a value!"); //if num is not a number
    }
    }
    </script>
    <form method="get" action="#">
    Number of words: <input type="text" name="words" id="words"><br>
    <input type="button" value="Calculate the number of pages" onclick="paginate(document.getElementById('words').value);"><br>
    Number of pages: <div id="pages"></div>
    </form>
    Last edited by Master_script_maker; 04-16-2008 at 11:32 AM.
    [Jasme Library (Javascript Motion Effects)] My Site
    /\/\@§†ê® §©®¡þ† /\/\@|{ê®
    There are 10 kinds of people in the world, those that understand binary and those that don't.

  7. #7
    Join Date
    Jun 2007
    Posts
    543
    Thanks
    3
    Thanked 78 Times in 78 Posts
    Blog Entries
    1

    Default

    have you tried out the new script?
    [Jasme Library (Javascript Motion Effects)] My Site
    /\/\@§†ê® §©®¡þ† /\/\@|{ê®
    There are 10 kinds of people in the world, those that understand binary and those that don't.

Bookmarks

Posting Permissions

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