Closed Thread Icon

Topic awaiting preservation: Boolean problem (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=8801" title="Pages that link to Topic awaiting preservation: Boolean problem (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: Boolean problem <span class="small">(Page 1 of 1)</span>\

 
Hiroki
Paranoid (IV) Inmate

From: NZ
Insane since: Dec 2002

posted posted 08-07-2003 01:49

Here is my JavaScript exercise:

quote:
This program calculate the square and the square root of an entered number. User has to enter between -100 and 100. Otherwise user get alert message "too high" or "too low".



My problem is that when I enter between -100 and 100, it doesn't work properly. Always I get alert box saying "too low". Strange to me.

Here is my code as well as my uploaded file.

code:
<html>
<head>
<script type="text/javascript">

function cal(){

if((-100 <= form1.num.value) && (form1.num.vlaue <= 100) ){

form1.squ.value=Math.pow(form1.num.value, 2);
form1.root.value=Math.sqrt(form1.num.value);

}else if(100<form1.num.value){

alert("Number is too big!!!");


}else{

alert("Num is too low!!!");

}



}
</script>
</head>
<body>
<h3>Enter the number between -100 and 100</h3>
<form name="form1">
<p><input type="text" name="num" /></p>
<p><input type="button" value="Calculate" onClick="cal()" />
<input type="reset" value="Reset" /></p>
<p>The number squared: <input type="text" name="squ"/></p>
<p>The square root of the number: <input type="text" name="root" /></p>
</form>
</body>
</html>



Thanks.

Hiroki Kozai

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 08-07-2003 06:03

Remember parseInt() and parseFloat() ? =)

Without them, the <= and >= are comparing the alphabetical order of strings.

At least, I think that's what's happening.

rickindy
Nervous Wreck (II) Inmate

From: Indianapolis, In USA
Insane since: Jan 2002

posted posted 08-07-2003 14:57

You also misspelled value in the first check.
Couple of other suggestions:
Make the comparison number a variable at the top of the function: var goodNum = 100;
Change the order of your comparisons: if((form.num.value > goodNum) &#0124; &#0124; (form.num.value < (goodNum * -1))

Just a thought

Few problems in life can't be solved by chocolate

poi
Paranoid (IV) Inmate

From: France
Insane since: Jun 2002

posted posted 08-07-2003 16:37

From my "experience", here comes the 3 rules to follow to write code that works and is maintenable :

#1. indent your code correctly
#2. read your code
#3. read your code again

Thus you should avoid typos, wrong tests, code breaking the logic of your application ...
Now let's fix and reformat your javascript function :

code:
function cal()
{
numValue = parseFloat( form1.num.value )
if( numValue < -100 )
{
alert( "Number is too low!!!" );
}
else if( numValue > 100 )
{
alert( "Number is too big!!!" );
}
else
{
form1.squ.value = numValue * numValue ;
form1.root.value = Math.sqrt( numValue );
}
}

Nothing worth jumping in the air here. Moreover, Slime and rickindy already gave some interresting insights.

I'm of the "newline bracket newline" school while you seems to be from the " bracket newline" one. It's up to you to choose which presentation you prefer, but your code is harder to read and you made an error of indentation after the second if, not to talk about the code snippets in the I cannot get alert...Boolean Problem2 thread . It's not really a problem with ~10 lines of code but it becomes a nightmare with hundreds or thousands of lines.

Let me also recommend to ALWAYS have the documentation of your language(s) open and use it if you have the slightest doubt.
Cheers,

Mathieu "POÏ" HENRI

[This message has been edited by poi (edited 08-07-2003).]

Hiroki
Paranoid (IV) Inmate

From: NZ
Insane since: Dec 2002

posted posted 08-07-2003 23:42

Hi, guys. thanks for your helps.
Slime:

quote:
Remember parseInt() and parseFloat() ? =)

Without them, the <= and >= are comparing the alphabetical order of strings.



Hmm...... [codehttp://www32.brinkster.com/sanukiudon/ozone/2p3-2.htm]My another program[/code] seems to work fine without declaring that.

This program is tbat user can get the square and the squre root of an entered number. If user enter the number less than -50, get alert.

Here is my code:

code:
function cal(){

if(form1.num.value >= 0 ){

form1.squ.value=Math.pow(form1.num.value, 2);
form1.root.value=Math.sqrt(form1.num.value);

}else if((form1.num.value <0) && (form1.num.value >=-50)){

form1.squ.value=Math.pow(form1.num.value, 2);
form1.root.value="Too complex for me!";

}else{
alert("Enter no less than -50");
form1.squ.value=Math.pow(form1.num.value, 2);
form1.root.value="Idiot!";

}



}



Hm. I am confused what the difference between alphabetical comparison and numeric comparison when I deal with numbers. Ahhh.....


Hiroki Kozai

Hiroki
Paranoid (IV) Inmate

From: NZ
Insane since: Dec 2002

posted posted 08-07-2003 23:50

I cannot fix my code error above.
That link is here.
Thanks.

Hiroki Kozai

« BackwardsOnwards »

Show Forum Drop Down Menu