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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Can I Divide 2 numbers then a 3rd number by the result

    I want to build a web form that will allow users to enter 3 different numbers and get a result. The answer is arrived at by the following. Enter 3 variable A, B, and C. Then A / B = D. Then C / D = the Answer.

    Is there a way to set that up as a single click script? Everything I have tried has failed hard.

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,133
    Thanks
    12
    Thanked 332 Times in 328 Posts
    what had you tried?
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #3
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by astronomyisfun View Post
    I want to build a web form that will allow users to enter 3 different numbers and get a result. The answer is arrived at by the following. Enter 3 variable A, B, and C. Then A / B = D. Then C / D = the Answer.

    Is there a way to set that up as a single click script? Everything I have tried has failed hard.
    you need a idea how to get the input with a single click or something else?

    "Please enter three distinct numbers separated with comma: ex 2,3,5"

    best regards and happy holidays

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Ummm...oesxyl, read what he said:
    I want to build a web form ...
    So no, I don't think that's the right answer.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by astronomyisfun View Post
    I want to build a web form that will allow users to enter 3 different numbers and get a result. The answer is arrived at by the following. Enter 3 variable A, B, and C. Then A / B = D. Then C / D = the Answer.

    Is there a way to set that up as a single click script? Everything I have tried has failed hard.
    Quote Originally Posted by Old Pedant View Post
    Ummm...oesxyl, read what he said:
    sometime i misunderstand what somebody say or do but i never try to answer without reading the question.

    So no, I don't think that's the right answer.
    which part in my anser stop him to build a webform?

    best regards

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Okay, I guess that would work. It would just be a clumsy web form.

    Code:
    <html>
    <head>
    <script type="text/javascript">
    function calculate(form)
    {
        var A = parseFloat(form.A.value);
        var B = parseFloat(form.B.value);
        var C = parseFloat(form.C.value);
        if ( isNaN(A) || isNaN(B) || isNaN(C) )
        {
            alert("All 3 values must be valid numbers!");
            return;
        }
        var D = A/B;
        form.answer.value = C/D;
    }
    </script>
    </head>
    <body>
    <form>
    Enter the three numbers:<ul>
    <li>A: <input name="A"></li>
    <li>B: <input name="B"></li>
    <li>C: <input name="C"></li>
    </ul>
    <input type="button" value="Calculate!" onclick="calculate(this.form)">
    <hr>
    Answer: <input name="answer" readonly>
    </form>
    </body>
    versus your version:
    Code:
    <html>
    <head>
    <script type="text/javascript">
    function calculate(form)
    {
        var values = form.three.value.split(",");
        if ( values.length != 3 ) {
             alert("You did not enter 3 separate values with commas between");
             return;
        }
        var A = parseFloat(values[0].replace(/\s/g,""));
        var B = parseFloat(values[1].replace(/\s/g,""));
        var C = parseFloat(values[2].replace(/\s/g,""));
        if ( isNaN(A) || isNaN(B) || isNaN(C) )
        {
            alert("All 3 values must be valid numbers!");
            return;
        }
        var D = A/B;
        form.answer.value = C/D;
    }
    </script>
    </head>
    <body>
    <form>
    Enter three numbers separated by commas:
    <input name="three">
    <br>
    <input type="button" value="Calculate!" onclick="calculate(this.form)">
    <hr>
    Answer: <input name="answer" readonly>
    </form>
    </body>
    I know which form would make more sense to me, at least.

    EDITED: To add in the JavaScript code, so that oesxyl will be happy. <grin/>
    Last edited by Old Pedant; 12-31-2010 at 12:14 AM.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Old Pedant View Post
    Okay, I guess that would work. It would just be a clumsy web form.

    Code:
    <form>
    Enter the three numbers:<ul>
    <li>A: <input name="A"></li>
    <li>B: <input name="B"></li>
    <li>C: <input name="C"></li>
    </ul>
    <input type="button" value="Calculate!" onclick="calculate(this.form)">
    </form>
    versus your version:
    Code:
    <form>
    Enter three numbers separated by commas:
    <input name="three">
    <br>
    <input type="button" value="Calculate!" onclick="calculate(this.form)">
    </form>
    I know which form would make more sense to me, at least.
    we are in javascript forum, i think op would be interested in javascript,

    best regards

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    we are in javascript forum, i think op would be interested in javascript
    Okay, okay. So now he has JavaScript.

    Better?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #9
    Regular Coder Krupski's Avatar
    Join Date
    Dec 2010
    Location
    United States of America
    Posts
    505
    Thanks
    39
    Thanked 47 Times in 46 Posts
    Quote Originally Posted by Old Pedant View Post
    Okay, I guess that would work. It would just be a clumsy web form.

    EDITED: To add in the JavaScript code, so that oesxyl will be happy. <grin/>

    Ha! I did some code too!

    Code:
    <html>
    <head>
    <title>Demo</title>
    <script type="text/javascript">
    function calculate(form)
    {
        var a = form.A.value; /* get input values into vars */
        var b = form.B.value;
        var c = form.C.value;
    
        if (findBad(b)) { return; } /* check for divide by zero! */
        var d = a / b; /* calc a/b */
    
        if (findBad(d)) { return; } /* check for divide by zero! */
        var answer = c / d; /* calc c/d */
    
        var e = document.getElementById('result'); /* point to answer element */
        e.innerHTML = 'The answer is: ' + answer; /* "print" text into answer span */
    }
    function findBad(test)
    {
        if (test == 0)
        {
            var e = document.getElementById('result');
            e.innerHTML = 'Divide by zero error!'
            return true;
        }
        return false;
    }
    </script>
    </head>
    <body>
    <form>
    Enter the three numbers:<ul>
    <li>A: <input name="A"></li>
    <li>B: <input name="B"></li>
    <li>C: <input name="C"></li>
    </ul>
    <input type="button" value="Calculate!" onclick="calculate(this.form)">
    </form>
    <span id="result"></span>
    </body>
    </html>
    Funny... you checked for NaN and I checked for divide-by-zero!


    LOL!
    "Anything that is complex is not useful and anything that is useful is simple. This has been my whole life's motto." -- Mikhail T. Kalashnikov

  • #10
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Old Pedant View Post
    Okay, okay. So now he has JavaScript.

    Better?
    i don't know, op didn't answer to none of the questions until now, Dormilich and mine,

    best regards and happy holidays,

  • #11
    Regular Coder Krupski's Avatar
    Join Date
    Dec 2010
    Location
    United States of America
    Posts
    505
    Thanks
    39
    Thanked 47 Times in 46 Posts
    Quick question.......


    "hidden input name.value=text"

    ...vs...

    "documentGetElementById('span_id').innerHTML=text"

    ....is there any reason to use one over the other?
    "Anything that is complex is not useful and anything that is useful is simple. This has been my whole life's motto." -- Mikhail T. Kalashnikov

  • #12
    Regular Coder Nile's Avatar
    Join Date
    Jun 2008
    Posts
    280
    Thanks
    2
    Thanked 46 Times in 46 Posts
    What are you talking about..? First of all, I have no idea what "hidden input name.value=text" is. Second of all:
    Code:
    documentGetElementById('span_id').innerHTML=text
    Isn't valid

  • #13
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Quote Originally Posted by Krupski View Post
    Quick question.......


    "hidden input name.value=text"

    ...vs...

    "documentGetElementById('span_id').innerHTML=text"

    ....is there any reason to use one over the other?
    Personal preference? Laziness?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #14
    Regular Coder Nile's Avatar
    Join Date
    Jun 2008
    Posts
    280
    Thanks
    2
    Thanked 46 Times in 46 Posts
    Quote Originally Posted by Old Pedant View Post
    Personal preference? Laziness?
    Both of his codes are invalid though...?

  • #15
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    He was just asking a general question and he was sloppy in the code he asked with.

    So change the code to
    Code:
    hidden_input_name.value=text;
    
    ...vs...
    
    document.getElementById('span_id').innerHTML=text
    Though I admit I dunno why it would be a *hidden* input element. I think he misread my code, is all.

    But in any case, his actual code was correct:
    Code:
        var e = document.getElementById('result');
        e.innerHTML = 'The answer is: ' + answer;
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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