/    Sign up×
Community /Pin to ProfileBookmark

If…else Statement, Cant get it to work?

I want to display one of two values either “b” or “f”

If “f=0” I want to display “b” and if not i want to 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/b
d=(Math.floor(a/b))
e=b*d
f=a-e

if (f=0)
{
form.ans.value = b
}
else
{
form.ans.value = f
}
}
</script>

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@RoxyBooOct 30.2010 — the syntax is

if (x==a) {

}
Copy linkTweet thisAlerts:
@KorOct 31.2010 — <i>
</i>if (f=0)

That is not a comparison. It is an assignment. An assignment is evaluated as a Boolean, but only in relationship of the type of each member. If one of them is false, null or undefined, the assignment returns a Boolean false. But there is no comparison at all between the members. In JavaScript:

the assignment operator is: =

the comparison operator (by value) is: ==

the comparison operator (by value and type) is: ===

You should use a comparison operator.
Copy linkTweet thisAlerts:
@TuskauthorOct 31.2010 — Thanks Kor and RoxyBoo

It works now much appreciated !!
×

Success!

Help @Tusk 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 6.17,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...