/    Sign up×
Community /Pin to ProfileBookmark

if then; and switch case; trouble in function

Well, I have not worked with javascript in a while, but
reading through most of the tutorials, it’s not too much
different than php. However, something is not working
correctly.

[code=php]
<script type=”text/javascript” language=”JavaScript”>
<!–

function CalculateTotals(){
f=document.calculator;
// if buysell = 1; buy. = 2; sell.
// if buy entry – stop

// if then statement here

f.first_result_temporary = Number(f.entry.value) – Number(f.stop.value);
f.first_result.value = f.first_result_temporary.toFixed(4);

…rest of code…
}
//–>
</script>
</head>

<BODY onLoad=”document.calculator.entry.focus()” >

<form NAME=”calculator” method=”post” ACTION=””>

…code.. code…

<tr><td>Buy/Sell</td>
<td><select name=”buysell” onChange=”CalculateTotals()” >
<option value=”1″ >Buy</option>
<option value=”2″ >Sell</option>
</select>

…code .. code…

<TR><TD>entry </TD>
<TD><input NAME=”entry” value=”1.0100″ size=”3″
OnBlur=”CalculateTotals()”></TD></TR>

<tr><td>stop </td>
<td><input type=”text” name=”stop” value=”50″
size=”3″ onChange=”CalculateTotals()”></td></tr>

…code … code…

<TR><TD>First Result</td><td>
<input NAME=”first_result” size=”4″ OnFocus=”document.calculator.entry.select();
document.calculator.entry.focus();”><td> entry + second_number</TD></TR>

…code…code..

[/code]

The above code works just fine.
However, once I try to add an “… if then …” or a “…switch case…”
it fails.

[code=php]
if(f.buysell.value = 1){
f.first_result_temporary = Number(f.entry.value) – Number(f.stop.value);
f.first_result.value = f.first_result_temporary.toFixed(4);
}
if(f.buysell.value = 2){
f.first_result_temporary = Number(f.stop.value) – Number(f.entry.value) ;
f.first_result.value = f.first_result_temporary.toFixed(4);
}

[/code]

Once I insert this code, it fails, and it also fails using “… switch case …”
I’ve also tried it using ” Number(f.buysell.value) ” also, and it still fails.

remove the statements and it works fine.

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@FangSep 28.2010 — comparison not equality[CODE]if(parseInt(f.buysell.value) == 1){[/CODE]
Copy linkTweet thisAlerts:
@anothenauthorSep 28.2010 — comparison not equality[CODE]if(parseInt(f.buysell.value) == 1){[/CODE][/QUOTE]

thx Fang

YAAY! it works!

that's embarrassing, I should have caught that.

Now I tried it without the parseInt() because I don't know what it is,

and it works okay.

I'll do some research and see what that is.

thx again
Copy linkTweet thisAlerts:
@anothenauthorSep 28.2010 — thx Fang

YAAY! it works!

that's embarrassing, I should have caught that.

Now I tried it without the parseInt() because I don't know what it is,

and it works okay.

I'll do some research and see what that is.

thx again[/QUOTE]




Oh, it's similar to the " Number() "
Copy linkTweet thisAlerts:
@KorSep 28.2010 — 
Now I tried it without the parseInt() because I don't know what it is,

and it works okay.[/QUOTE]

It woks as well because [COLOR="Blue"]==[/COLOR] means a comparison of values. But JavaScript can perform a comparison by value [I]and[/I] type, on using the operator [COLOR="Blue"]===[/COLOR], in which case it won't work.

All the values of the form's elements are, for JavaScript, [I]strings[/I]. JavaScript makes a difference between strings and numbers ("2" vs 2), but the difference is by the [I]type[/I] of the variable
<i>
</i>alert('2'[COLOR="Blue"]==[/COLOR]2) // return true, as it is a bit-wise comparison (only by value)
alert('2'[COLOR="Blue"]===[/COLOR]2) //return false, as the variables are different as type - String/Number, even they have the same value


However, in practice it is better to transform always a string which has a number as value into a number before performing comparisons or math operations. A good habit, just to avoid possible errors. There are several ways to do that, but the method [B]Number()[/B] is my recommendation.

[I][B]Number()[/B] is not similar with [B]parseInt()[/B].[/I]

[B]parseInt()[/B] is, in fact, a method to parse an integer from a certain base (up to 36) to the decimal base. The full syntax is, in fact parseInt([I]string,base[/I]), where the base should be specified, as it is [I]not[/I] 10 by default
<i>
</i>parseInt('ff',16) // return 255, as this is the parsed value from hexadecimal to decimal
parseInt('08') // return 0, as the parser considers it an octal number
parseInt('08',10) // return 8, as we expected, as now the base (radix) is specified


[B]parseFloat()[/B] is somehow similar with [B]Number()[/B], except that [B]Number()[/B] is able to return 0 [I]in case the value is an empty string[/I], which is useful in form calculation applications.
×

Success!

Help @anothen 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.21,
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,
)...