Closed Thread Icon

Topic awaiting preservation: Boolean problem (maybe) Pages that link to <a href="https://ozoneasylum.com/backlink?for=8709" title="Pages that link to Topic awaiting preservation: Boolean problem (maybe)" rel="nofollow" >Topic awaiting preservation: Boolean problem (maybe)\

 
Author Thread
Hiroki
Paranoid (IV) Inmate

From: NZ
Insane since: Dec 2002

posted posted 06-10-2003 07:12

Hi, guys.
It is just about time to go home, actually.
But may I ask you this?

my mission:

quote:
Write a program that asks the user for a number.
If the number is zero, print "zero"
If the number is one, print "one"
If the number is greater than or equal to two, print "Greater than or equal to two"
If the number is less than 0, print "negative"



code:
<html>
<head>
<script type="text/javascript">
function cal(){
var Num=parseInt(document.theForm.num.value);

if(Num<0){
document.theForm.result.value="Negative";
}else if(Num>=2){
document.theForm.result.value="Great";
}else if(Num = 1){
document.theForm.result.value="ONE";
}else if(Num=0){
document.theForm.result.value="ZERO";
}else{
document.theForm.result.value="Hmm....";
}

}

</script>
</head>
<body>
<h3>Mission:</h3>
<p>Write a program that asks the user for a number. <br />
If the number is zero, print "zero"<br />
If the number is one, print "one"<br />
If the number is greater than or equal to two, print "Greater than or equal to two"<br />
If the number is less than 0, print "negative"
</p>

<form name="theForm">
<p> Enter the number:
<input type="text" name="num">
<input type="button" value=" Get it! " onClick="cal()">
<input type="reset" value="Reset"></p>
<p> Your number is:
<input type="text" name="result"></p>
</form>
</body>
</html>


My uploaded file is here.

Problems is that when I enter 0, it shows one as output, not zero. And when I enter 1.5 or whatever between 0 and 2, but not integer, it shows one as well. I think I coded right. But why doesn't it work???

Would you see anything??? Please help.



Hiroki Kozai

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 06-10-2003 14:58

When comparing values, you must use a double-equals sign!

if(Num = 1)

This assigns the value of 1 to Num, and then returns the assigned value 1, which is always true. It should be

if(Num == 1)

which compares the value of Num to 1, and returns true only if they're equivalent.

Hiroki
Paranoid (IV) Inmate

From: NZ
Insane since: Dec 2002

posted posted 06-10-2003 23:22

Hi, Slime. Good morning.
How are you today? It is bloody cold here, New Zealand.
Heavy frost and chilly cold sea breeze.
Well, thank you for your reply. I really appreciate that.
Now it is sort of working fine.

But I am afraid still having problem.
The problem is that when I enter the value 0.3, output gets zero. But zero is supposed to be shown only when 0 is entered into box. Would you know what I mean? Also when I enter 1.3 or whatever 1 plus decimal, output gets one. It is suppoed to be "Hmm...." for output.

Here is my code:

code:
<script type="text/javascript">
function cal(){
var Num=parseInt(document.theForm.num.value);

if(Num<0){
document.theForm.result.value="Negative";

}else if(Num>=2){

document.theForm.result.value="Great";

}else if(Num == 1){

document.theForm.result.value="ONE";

}else if(Num == 0){

document.theForm.result.value="ZERO";

}else{
document.theForm.result.value="Hmm...";
}


}

</script>



I think this code is right. But probably not.
Would you see anything wrong???



Hiroki Kozai

HZR
Bipolar (III) Inmate

From: Cold Sweden
Insane since: Jul 2002

posted posted 06-10-2003 23:43

Yea, that's because you use parseInt(). It will convert 0.3 to 0. Why do you use it at all?



[This message has been edited by HZR (edited 06-10-2003).]

Dracusis
Maniac (V) Inmate

From: Brisbane, Australia
Insane since: Apr 2001

posted posted 06-11-2003 00:43

Remember, ia Integer is a whole number, any non-whole number (that being any number with a percision greater that a whole number, something with decimal places) is a floating point number also know as a float.

parseInt() is handy when you want to turn string values into integers like the string "5px" into the integer 5. But this only works because it looks for the first numerical values in the string, so if you had "5.25px" then you'd still get the integer 5 but if you had "px5" you'd get a NaN (Not a Number) result because it would find the "p" first and return an error as it can't convert that to a number.

Now, if you wanted to turn "5.25px" into the float of 5.25 then you'd want to use parseFloat().

However, both the parseInt() and parseFloat() functions are only meant to take string vlues as an argument. I realise that your textbook/teacher told you to eval() any form inputs or the numerical values entered might not work but, well, using eval() on any user input is just an all round bad idea. If I enter some javascript code into a form field that you eval()'d then I could really mess up your page.

Using parseInt() will do roughly the same thing but and it's a tad more user friendly, but it'll only work if you want to use integer values. You could then just use parseFloat() on every form input you expect a numerical value from but to be honest, I really can't see why you need to parse any data being inputed via a form. I've never encounterd a browser with a DOM that didn't treat numerical form values as integers or floats right off the bat, so in short, just use the form value as is. It'll work just fine that way.

=)

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 06-11-2003 03:43

Actually, I've been telling him the opposite, because the truth is, it doesn't *always* work that way. If you add two form values together, it will not add the numbers, but it will concatenate the strings. That's the primary time when it will mess up. Aside from that, using the ParseFloat or ParseInt functions are still a good idea, so that you know that if the user doesn't input a number, the code will cause an error rather than silently continuing and not letting you know that something went wrong. It's also important for beginning programmers to understand the distinction between strings which look like numbers ("32") and numbers themselves (32).

The solution to this, as Dracusis pointed out, is to use parseFloat rather than parseInt.

Dracusis
Maniac (V) Inmate

From: Brisbane, Australia
Insane since: Apr 2001

posted posted 06-11-2003 03:52
quote:
If you add two form values together, it will not add the numbers, but it will concatenate the strings.



Never knew that. Thanks for correcting my oversight =)

Hiroki
Paranoid (IV) Inmate

From: NZ
Insane since: Dec 2002

posted posted 06-11-2003 04:13

Hi, guys. Many many thanks for your replies.
I am really glad to hear from you.
Well, you know I have been studying JavaScript for a while.
I am doing it myself.
I had programming class last year, which was QBASIC.
All these my questions are from that textbook.
I am trying to write questions from QBASIC book using JavaScript.
I don't know if this is the best way to learn or not.
But feel getting better everyday.
I really thank you for your helps.
My all knowledge has been improved from you, guys.
Thank you very much.

ps. I changed my code from parseInt to parseFloat. It worked fine. Great! Many thanks.

Hiroki Kozai

« BackwardsOnwards »

Show Forum Drop Down Menu