quick bit of help

Soldato
Joined
28 Jul 2003
Posts
4,074
Location
Hook
right i have done most the work so i think this is allowed.
i have made a program in javascript. that when u input the temperature of the water it will tell you if its a liquid, gas or solid.
but i cant get it to work with negative temperatures. any idea why?

Code:
<HTML>
<HEAD>
<TITLE> Temperature program
</TITLE>
<SCRIPT LANGUAGE = "JavaScript">


// A program for determining the state of water

theTemperature = 0;
state = 'ice';
theTemperature = window.prompt('what is the temperature?', '');

if ( theTemperature  < 0 ) state = ('ICE');
if ( theTemperature  > 0 <= 100) state = ('LIQUID');
if ( theTemperature  > 100) state = ('GAS');


document.write ( 'The water is : ', state );
document.write ( '<BR>' + 'Thank-you for using this program.');
document.close ();

</SCRIPT>
</HEAD>
<BODY>
</BODY>
</HTML>


thanks.
 
<HTML>
<HEAD>
<TITLE> Temperature program
</TITLE>
<SCRIPT LANGUAGE = "JavaScript">


// A program for determining the state of water

theTemperature = 0;
state = 'ice';
theTemperature = window.prompt('what is the temperature?', '');

if ( theTemperature < 0 )
{state = ('ICE')}
else
{ if (theTemperature > 100)
{state = ('GAS')}
else
{state = ('LIQUID')}
}
;


document.write ( 'The water is : ', state );
document.write ( '<BR>' + 'Thank-you for using this program.');
document.close ();

</SCRIPT>
</HEAD>
<BODY>
</BODY>
</HTML>
 
I havent looked at javascript in a long time - so not sure why your way isnt working exactly (I think it is the condition for liquid), but that is how I would always do something with IF statements so at most you check 2 conditions (opposed to always checking 3)
 
if ( theTemperature > 0 & theTemperature <= 100) state = ('LIQUID');


that fixes your current solution, I would say nesting the IFs is a better way to do it
 
Back
Top Bottom