/    Sign up×
Community /Pin to ProfileBookmark

Javascript math with variables

I have just started with java script, but already, I have ran into some noob related issues. I am trying to make a simple program to solve for X in a quadratic equation. I have three prompts which ask for the variables of a, b, and c, and then it is supposed to use the quadratic formula to solve for X. However, whenever I try to run it, it will give me the prompts and then not do anything. Here is my script.
<!DOCTYPE html>
<html>
<body>
<script>
var A = prompt(“what is variable a?”)
var B = prompt(“what is variable b?”)
var C = prompt(“what is variable c?”)
var BA = math.pow(B,2)
var Answer1 = (4*A*C-1+BA)
var Answer2 = (2*A)
var Answer3 = (Answer1/Answer2)
alert(Answer3)
</script>
</body>
</html>
Right now, it is just solving for the discriminant, and I was going to continue, but i got this error and was stuck. Could anyone tell me what I’m doing wrong? I’m just using notepad.

to post a comment
JavaScript

10 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERMar 25.2014 — Convert your strings to numbers.

Avoid 'prompt's in the future or give some default values.

There is not such command as math. It is Math.

Use the error console if using the FF or Chrome browsers.
<i>
</i>&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;body&gt;
&lt;script&gt;
// From: http://www.webdeveloper.com/forum/showthread.php?292299-Javascript-math-with-variables

var A = Number(prompt("what is variable a?",1));
var B = Number(prompt("what is variable b?",2));
var C = Number(prompt("what is variable c?",3));
var BA = Math.pow(B,2);
var Answer1 = (4*A*C-1+BA);
var Answer2 = (2*A);
var Answer3 = (Answer1/Answer2);
alert(Answer3);
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;

Good Luck!

?
Copy linkTweet thisAlerts:
@Dudeguy21authorMar 26.2014 — Convert your strings to numbers.

Avoid 'prompt's in the future or give some default values.

There is not such command as math. It is Math.

Use the error console if using the FF or Chrome browsers.
<i>
</i>&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;body&gt;
&lt;script&gt;
// From: http://www.webdeveloper.com/forum/showthread.php?292299-Javascript-math-with-variables

var A = Number(prompt("what is variable a?",1));
var B = Number(prompt("what is variable b?",2));
var C = Number(prompt("what is variable c?",3));
var BA = Math.pow(B,2);
var Answer1 = (4*A*C-1+BA);
var Answer2 = (2*A);
var Answer3 = (Answer1/Answer2);
alert(Answer3);
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;

Good Luck!

?[/QUOTE]




Thanks for the help!!! But I seem to have encountered a similar problem. Again, the prompts won't even show up. I don't think I made the same mistake... But again, I am a noob at this. Here is the updated script:

<!DOCTYPE html>

<html>

<body>

<script>

var A = Number(prompt("what is variable a?",0));

var B = Number(prompt("what is variable b?",0));

var C = Number(prompt("what is variable c?",0));

var BA = Math.pow(B,2);

var Answer1 = (4*A*C-1+BA);

var Answer2 = (2*A);

var Answer3 = math.sqrt(Answer1);

var Answer4 = (Answer3/Answer2);

var Answer5 = (-B/Answer2);

var Answer6 = (Answer5+Answer4);

var Answer7= = (Answer5-Answer4);

(alert(Answer6 "or" Answer7))

</script>

</body>

</html>
Copy linkTweet thisAlerts:
@Kevin2Mar 26.2014 — See lines in red and compare to your code.
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;body&gt;
&lt;script&gt;
var A = Number(prompt("what is variable a?",0));
var B = Number(prompt("what is variable b?",0));
var C = Number(prompt("what is variable c?",0));
var BA = Math.pow(B,2);
var Answer1 = (4*A*C-1+BA);
var Answer2 = (2*A);
[B][COLOR="#FF0000"]var Answer3 = Math.sqrt(Answer1);[/COLOR][/B]
var Answer4 = (Answer3/Answer2);
var Answer5 = (-B/Answer2);
var Answer6 = (Answer5+Answer4);
[B][COLOR="#FF0000"]var Answer7 = (Answer5-Answer4);
alert(Answer6 + " or " + Answer7);[/COLOR][/B]
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@Dudeguy21authorMar 26.2014 — See lines in red and compare to your code.
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;body&gt;
&lt;script&gt;
var A = Number(prompt("what is variable a?",0));
var B = Number(prompt("what is variable b?",0));
var C = Number(prompt("what is variable c?",0));
var BA = Math.pow(B,2);
var Answer1 = (4*A*C-1+BA);
var Answer2 = (2*A);
[B][COLOR="#FF0000"]var Answer3 = Math.sqrt(Answer1);[/COLOR][/B]
var Answer4 = (Answer3/Answer2);
var Answer5 = (-B/Answer2);
var Answer6 = (Answer5+Answer4);
[B][COLOR="#FF0000"]var Answer7 = (Answer5-Answer4);
alert(Answer6 + " or " + Answer7);[/COLOR][/B]
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;
[/QUOTE]


Thanks so much! I'll try to look more closely at my code in the future.
Copy linkTweet thisAlerts:
@Kevin2Mar 26.2014 — Because:

a) I remember a horribly frustrating exercise to find the real and non-real roots of the quadratic formula in a FORTRAN class in college a millenium ago.

b) I detest, hate, loathe and despise JavaScript Prompts and Alerts

I placed your math in a new "wrapper":
[code=html]<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Quadratic Equation</title>
<style>
.alert{
color:red;
}
</style>
<script>
function q(a,b,c,s,r1,r2,rb,n,z,aa,ab,ac){
var A = a.value,
B = b.value,
C = c.value;
if ((A != '') && (B != '') && (C != '')){
var BA = Math.pow(B,2),
Answer1 = (4*A*C-1+BA),
Answer2 = (2*A),
Answer3 = Math.sqrt(Answer1),
Answer4 = (Answer3/Answer2),
Answer5 = (-B/Answer2),
Answer6 = (Answer5+Answer4),
Answer7 = (Answer5-Answer4);
r1.innerHTML = Answer6;
r2.innerHTML = Answer7;
if ((r1.innerHTML == 'NaN') || (r2.innerHTML == 'NaN')){ // non-real roots
n.style.display = 'block';
rb.style.display = 'block';
}else{
s.style.display = 'block';
rb.style.display = 'block';
}
}else{
if (A == ''){
aa.style.display = 'inline';
z.style.display = 'none';
}
if (B == ''){
ab.style.display = 'inline';
}
if (C == ''){
ac.style.display = 'inline';
}
}
}

function valid(el,alert,n,s,rb,z){
if ((el = a) && (el.value == '0')){ // prevents dividing by zero
z.style.display = 'inline';
alert.style.display = 'none';
el.value = '';
el.focus();
}else{
z.style.display = 'none';
alert.style.display = 'none';
n.style.display = 'none';
s.style.display = 'none';
rb.style.display = 'none';
}
}
</script>
</head>
<body>

<h1>The Quadratic Equation</h1>

<p>Solves for real roots only.</p>

<p><label for="a">Enter a numeric value for variable a:</label> <input type="number" id="a" placeholder="2" onblur="valid(this,alerta,nan,show,reset,zero)" /> <span class="alert" id="alerta" style="display:none;">&lt;&mdash;&mdash; Missing or non-numeric value!</span><span class="alert" id="zero" style="display:none;">&lt;&mdash;&mdash; Variable 'a' cannot equal 0 (zero).</span><br />
<label for="b">Enter a numeric value for variable b:</label> <input type="number" id="b" placeholder="4" onblur="valid(this,alertb,nan,show,reset,zero)" /> <span class="alert" id="alertb" style="display:none;">&lt;&mdash;&mdash; Missing or non-numeric value!</span><br />
<label for="c">Enter a numeric value for variable c:</label> <input type="number" id="c" placeholder="6" onblur="valid(this,alertc,nan,show,reset,zero)" /> <span class="alert" id="alertc" style="display:none;">&lt;&mdash;&mdash; Missing or non-numeric value!</span></p>

<p><input type="button" value="Solve" onclick="q(a,b,c,show,result1,result2,reset,nan,zero,alerta,alertb,alertc)" /></p>

<p id="show" style="display:none;">Results:<br />
<b><span id="result1"></span></b><br />
&nbsp; &nbsp; &ndash; OR &ndash;<br />
<b><span id="result2"></span></b></p>

<p id="nan" style="display:none;">The values entered yielded non-real roots.</p>

<p id="reset" style="display:none;"><input type="button" value="Reset" onclick="window.location.reload()" /></p>

</body>
</html>[/code]
Copy linkTweet thisAlerts:
@Dudeguy21authorApr 28.2014 — Thank you! That was very helpful, and a big improvement. Sorry it took so long to reply. Thanks for your help everyone!
Copy linkTweet thisAlerts:
@Dudeguy21authorNov 10.2015 — Also, I recently realized that this script does not give a correct answer. Just throwing that out there (almost two year later)
Copy linkTweet thisAlerts:
@Dudeguy21authorNov 10.2015 — I just updated the script to output the right values. The calculating part is also no longer confusing as hell.

http://jsfiddle.net/Dudeguy21/9jappmnd/
Copy linkTweet thisAlerts:
@wbportNov 10.2015 — You take the square root of "b-squared minus 4ac" and Answer1 = (4*A*C-1+BA),[/QUOTE] isn't it, you have 4AC minus 1 plus b-squared. Try "BA -4*A*C" or "(4*A*C*-1+BA)" instead.
Copy linkTweet thisAlerts:
@Dudeguy21authorNov 11.2015 — Thanks for pointing that out! That's why I rewrote it, and ditched the odd "answer1, answer2 etc." system.
×

Success!

Help @Dudeguy21 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...