简体   繁体   中英

Javascript and HTML Catch Throw error returned

Using a website called JS Bin ( https://jsbin.com ) i am trying to code a quadratic formula solver and when i run my code it returns this:

<script>try {function solve(a, b, c) { 
var result = (-1 * b + Math.sqrt(Math.pow(b, 2) - (4 * a * c))) / (2 * a); 
var result2 = (-1 * b - Math.sqrt(Math.pow(b, 2) - (4 * a * c))) / (2 * a); 
return result + "<br>" + result2;
} catch (error) { throw error; }

here is the HTML:

<form> <input type="number" placeholder="A"> 
 <input type="number" placeholder="B">
 <input type="number" placeholder="C">
 <textarea rows="10" cols="60" placeholder="Your Output">

and here is the Javascript:

function solve(a, b, c) { 
var result = (-1 * b + Math.sqrt(Math.pow(b, 2) - (4 * a * c))) / (2 * a); 
var result2 = (-1 * b - Math.sqrt(Math.pow(b, 2) - (4 * a * c))) / (2 * a); 
return result + "<br>" + result2; 

I tried to find an answer online but i did not find anything helpful, can anyone help me with this? i want the code to just produce the output of the javascript into the textarea.

You should just close your <textarea> tag using </textarea> . Also, don't forget to close your solve function declaration with a curly bracket } at the end :

 function solve(a, b, c) { var result = (-1 * b + Math.sqrt(Math.pow(b, 2) - (4 * a * c))) / (2 * a); var result2 = (-1 * b - Math.sqrt(Math.pow(b, 2) - (4 * a * c))) / (2 * a); return result + "<br>" + result2; }
 <form> <input type="number" placeholder="A"> <input type="number" placeholder="B"> <input type="number" placeholder="C"> <textarea rows="10" cols="60" placeholder="Your Output"></textarea> </form>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM