Jump to content

If...else Statement


Tusk
 Share

Recommended Posts

I want to display one of two values either "b" or "f"If "f=0" I want to display "b" and if not display "f" below is my code but its not working?<script type="text/javascript">function a_div_b(form) {a=eval(form.a.value)b=eval(form.b.value)c=a/bd=(Math.floor(a/:))e=b*df=a-eif (f=0) { form.ans.value = b }else { form.ans.value = f }}</script>

Link to comment
Share on other sites

Why are you calling eval() on the form fields? The main problem is, though, that the equality operator is ==, not =.

Link to comment
Share on other sites

Hi Synookhere is the whole form basically "a "and "b" are input fields if you can suggest a better way of defining them great, bare in mind I am really new to this so be gentle with explanations :) Thanks<html xmlns='http://www.w3.org/1999/xhtml' lang='en' xml:lang='en'><HEAD><script type="text/javascript">function a_div_b(form) {a=eval(form.a.value)b=eval(form.b.value)c=a/bd=(Math.floor(a/:))e=b*df=a-eform.ans.value = f}</script></HEAD><BODY><CENTER><FORM name="formx"><input type=text size=4 value=211 name="a">    <input type=text size=4 value=49 name="b">  =  <input type "number" value=0 name="ans" size=9>  <input type="button" value=" Calc " onClick="a_div_b(this.form)"></FORM></CENTER></html>

Link to comment
Share on other sites

as in change the function to read as follows:

function a_div_b(form) {	a=parseInt(form.a.value,10);	b=parseInt(form.b.value,10);	c=a/b;	d=(Math.floor(a/b));	e=b*d;	f=a-e;	if (f[color="#FF0000"][b]==[/b][/color]0) {		form.ans.value = b;	}		else	{		form.ans.value = f;	}}

what he meant by the equality operator is the condition for the if statement. i've made this bold and red to enhance clarity.single = sets the value of f, and == only checks the value. === checks the value and type.it is usually best practice to tab out your functions for easier readability when scripting in any language. also you might find it best to end each line that is not part of an if/while/for/switch/etc statement, with a semicolon (;).also you might want to use parseInt instead of eval, as these values are used in mathematical operations before the if statement.

Edited by Pierre 'Greywacke' du Toit
Link to comment
Share on other sites

Pierre Thanks,and for the explanation, I struggle so much trying to get things to work by looking for other examples hacking and pasting ...so when you give an explanation I realize ignorant i really am it should be bliss but with code its so frustrating ...one step forward 2 steps backwards.....when doe you run???? LOL Much apreciated :)

Link to comment
Share on other sites

the basics are covered really well in the w3schools tutorials.http://www.w3schools.com/js/default.aspare you still having issues with your problem? If so, could you post the latest changes to your code?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...