Closed Thread Icon

Topic awaiting preservation: I think it is all right, but...sometimes... Pages that link to <a href="https://ozoneasylum.com/backlink?for=8695" title="Pages that link to Topic awaiting preservation: I think it is all right, but...sometimes..." rel="nofollow" >Topic awaiting preservation: I think it is all right, but...sometimes...\

 
Author Thread
Hiroki
Paranoid (IV) Inmate

From: NZ
Insane since: Dec 2002

posted posted 06-06-2003 00:35

Hi, guys.
I am practicing another exercise.
I am having taugh time on JavaScript as you know.
Well, this time my code sometimes work sometimes not.
Logically I did right for the code, I reckon.

My mission is:

quote:
Write a program that checks an input against a constant (called checknumber)
that is set to user input. If the number is >= checknumber then output "checknumber or above". Otherwise output "checknumber-1 or below".



My code is:

code:
<html>
<head>
<script type="text/javascript">
function cal(){
checkNum = document.theForm.cn.value;
if(document.theForm.num.value>=checkNum){
document.write((checkNum) + "or above");
}else{
document.write((checkNum-1) + "or less");
}
}
</script>
</head>
<body>
<form name="theForm">
<p>Enter the check number!
<input type="text" name="cn"></p>
<p>Enter the number you want!
<input type="text" name="num">
<input type="button" value="get it" onClick="cal()">
</p>
</form>
</body>
</html>



The problem is that when I run the code, sometimes I got wrong answer. This is quite simple. Do you see any problem??? My uploaded file is here.
For example, please enter 100000 for check number and 60 for second box. It is supposed to show "99999 or less" but it is showing "100000 or above".......

Hmm.....I don't see any problem...

Hiroki Kozai

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 06-06-2003 04:59

It's because of the string vs number problem. You're comparing the strings "60" and "100000", and strings are compared in alphabetical order, so "60" > "100000".

Just use parseInt() and it should work.

Hiroki
Paranoid (IV) Inmate

From: NZ
Insane since: Dec 2002

posted posted 06-08-2003 23:57

Hi, Slime. Many many thanks for your advice.
How was your weekend?
I refresh myself playing tennins and going out with my mates.
And had a party. Bit tired but really good weekend.
Now I fixed my code like you told me.
Many thanks for your help.
It worked fine.
Cya.

Hiroki Kozai

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 06-09-2003 00:34

You're welcome.

My sister got married this weekend. It was pretty weird. =)

Hiroki
Paranoid (IV) Inmate

From: NZ
Insane since: Dec 2002

posted posted 06-09-2003 00:45

Oh, that's great.
Conglutulations!!!
I have sister too, Slime.
Hope they will find good husband in the future.
Cya.

Hiroki Kozai

« BackwardsOnwards »

Show Forum Drop Down Menu