Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3270874
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:40:41+00:00 2026-05-17T18:40:41+00:00

I’m trying to do some complex math where I need to call upon some

  • 0

I’m trying to do some “complex” math where I need to call upon some of JavaScript’s Math properties to solve the quadratic equation. Does the following method work?

    root = Math.pow(inputb,2) - 4 * inputa * inputc;
    root1 = (-inputb + Math.sqrt(root))/2*inputa;
    root2 = (-inputb - Math.sqrt(root))/2*inputa;

Does this look correct?

For some reason, I’m not seeing correct results..

inputa, inputb, and inputc are all variables which store user-input from a text field by the way.

FULL CODE

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Quadratic Root Finder</title>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>

<script>
window.onload = function() {



    $('.error').hide();



    document.getElementById('quadraticcalculate').onclick = function calculateQuad()
    {
        var inputa = document.getElementById('variablea').value;
        var inputb = document.getElementById('variableb').value;
        var inputc = document.getElementById('variablec').value;

        inputa = new Number(inputa); // try to convert to number
        if (isNaN(inputa)) { // use built in method to check for NaN
            $('#quadraticaerror').show();
            return;
        }

        inputb = new Number(inputb); // try to convert to number
        if (isNaN(inputb)) { // use built in method to check for NaN
            $('#quadraticberror').show();
            return;
        }

        inputc = new Number(inputc); // try to convert to number
        if (isNaN(inputc)) { // use built in method to check for NaN
            $('#quadraticcerror').show();
            return;
        }

        root = Math.pow(inputb,2) - 4 * inputa * inputc;
        root1 = (-inputb + Math.sqrt(root))/(2*inputa);
        root2 = (-inputb - Math.sqrt(root))/(2*inputa);

        document.getElementById('root1').value = root1;
        document.getElementById('root2').value = root2;
        if(root<'0')
        {
            document.getElementById('root1').value = 'No real solution'
            document.getElementById('root2').value = 'No real solution'
        }
        else {
            if(root=='0')
            {
                document.getElementById('root1').value = root1
                document.getElementById('root2').value = 'No Second Answer'
            }
            else {
                document.getElementById('root1').value = root1
                document.getElementById('root2').value = root1
                }
            }
    };



    document.getElementById('quadraticerase').onclick = function()
    {
        document.getElementById('quadraticform').reset();
        $('.error').hide();
    }

        document.getElementById('cubicerase').onclick = function()
    {
        document.getElementById('cubicform').reset();
        $('.error').hide();
    }



}
</script>

<style>
div.#wrapper
{
    text-align: center;
}
.error
{
    color: #FF0000;
}</style>

</head>

<body>
<div id="wrapper">
    <div id="quadratic">
        <form id="quadraticform">
            <h1>Quadratic</h1>
            a:<input id="variablea" value="" type="text">
            <br/>
            b:<input id="variableb" value="" type="text">
            <br />
            c:<input id="variablec" value="" type="text">
            <br />
            <input id="quadraticcalculate" value="Calculate!" type="button">
            <input id="quadraticerase" value="Clear" type="button">
            <br />
            <br />
            Roots:
            <br />
            <input id="root1" type="text" readonly>
            <br />
            <input id="root2" type="text" readonly>
            <p id="quadraticaerror" class="error">Error:  Variable a is not a valid integer!</p>
            <br />
            <p id="quadraticberror" class="error">Error:  Variable b is not a valid integer!</p>
            <br />
            <p id="quadraticcerror" class="error">Error:  Variable c is not a valid integer!</p>
        </form>
    </div>
    <div id="cubic">
        <form id="cubicform">
            <h1>Cubic</h1>
            a:<input id="variablea" value="" type="text">
            <br/>
            b:<input id="variableb" value="" type="text">
            <br />
            c:<input id="variablec" value="" type="text">
            <br />
            d:<input id="variabled" value="" type="text">
            <br />
            <input id="cubiccalculate" value="Calculate!" type="button">
            <input id="cubicerase" value="Clear" type="button">
            <br />
            <br />
            Roots:
            <br />
            <input id="root1" type="text" readonly>
            <br />
            <input id="root2" type="text" readonly>
            <p id="cubicaerror" class="error">Error:  Variable a is not a valid integer!</p>
            <br />
            <p id="cubicberror" class="error">Error:  Variable b is not a valid integer!</p>
            <br />
            <p id="cubiccerror" class="error">Error:  Variable c is not a valid integer!</p>
            <br />
            <p id="cubicderror" class="error">Error:  Variable d is not a valid integer!</p>
        </form>
    </div>
</div>
</body>
</html>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T18:40:42+00:00Added an answer on May 17, 2026 at 6:40 pm

    * and / have same precedence and are left associative so what you have effectively got is

    root = Math.pow(inputb,2) - 4 * inputa * inputc;
    root1 = ((-inputb + Math.sqrt(root))/2)*inputa;
    root2 = ((-inputb - Math.sqrt(root))/2)*inputa;
    

    what you want is

    root = Math.pow(inputb,2) - 4 * inputa * inputc;
    root1 = (-inputb + Math.sqrt(root))/(2*inputa);
    root2 = (-inputb - Math.sqrt(root))/(2*inputa);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
I have just tried to save a simple *.rtf file with some websites and
I am trying to loop through a bunch of documents I have to put
I have some data like this: 1 2 3 4 5 9 2 6
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Seemingly simple, but I cannot find anything relevant on the web. What is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.