Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    conditions error

    Hi!
    I'm very new to javascript and i'm trying to write a script that allows me to:
    -add to and substract from a value called temperature through methods warmer and cooler
    -read temperature
    -set a maximum and minimum value for temperature

    this is what i've come up with:
    Code:
    public class Heater
    {
        private int temperature;
        private int min;
        private int max;
        private int increment;
        private int recMin;
        private int recMax;
        
        public Heater()
        {
            temperature = 15;
            min = recMin;
            max = recMax;
            increment = 5;
        }
        
        public void insrtMin(int minimum)
        {
            recMin = minimum;
        }
        
        public void insrtMax(int maximum)
        {
            recMax = maximum;
        }
        
        public void warmer()
        {   
            if ((temperature + increment) > max)
            {
    
            }
            else
            {
                temperature = temperature + increment;
            }
        }
        
        public void cooler()
        {   
            if ((temperature - increment) < min)
            {
    
            }
            else
            {
                temperature = temperature - increment;
            }
        }     
    
        public int returnTemp()
        {
            return temperature;
        }
    }
    The code worked fine until I added the conditions to 'warmer' and 'cooler'. Now, however, only the cooler method seems to work. What did I miss?

    thanks!

  • #2
    Senior Coder DanInMa's Avatar
    Join Date
    Nov 2010
    Location
    Salem,Ma
    Posts
    1,577
    Thanks
    13
    Thanked 248 Times in 248 Posts
    you need the "Java" forum not javascript

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,910
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    This is the JavaScript forum. Java and Javascript are entirely different programming languages, in spite of the confusingly similar names. Rather like Austria and Australia! Ask a mod to move this thread to the right forum.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #4
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks! Sorry about that..

  • #5
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,503
    Thanks
    3
    Thanked 500 Times in 487 Posts
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    </head>
    
    <body>
     <input name="" id="tst" /> <br />
     <input type="button" name="" value="Warmer" onmouseup="heater.heat('tst',1);"/>
     <input type="button" name="" value="Colder" onmouseup="heater.heat('tst',-1);"/>
     <input type="button" name="" value="Set Min" onmouseup="heater.setmin('tst',0);"/>
     <input type="button" name="" value="Set Max" onmouseup="heater.setmax('tst',300);"/>
    
    <script  type="text/javascript">
    /*<![CDATA[*/
    
    var heater={
    
     init:function(o){
      heater[o.id]={
       obj:document.getElementById(o.id),
       temp:o.temperature,
       min:o.min,
       max:o.max,
       i:o.increment
      }
     },
    
     heat:function(id,ud){
      var o=heater[id],ud=typeof(ud)=='number'&&ud==0?0:ud<0?-1:1;
      if (o){
       o.obj.value=Math.min(Math.max(o.temp+=(ud*o.i),o.min),o.max);
      }
     },
    
     setmin:function(id,nu){
      var o=heater[id];
      if (o&&typeof(nu)=='number'){
       o.min=Math.min(nu,o.max);
       this.heat(id,0);
      }
     },
    
     setmax:function(id,nu){
      var o=heater[id];
      if (o&&typeof(nu)=='number'){
       o.max=Math.max(nu,o.min);
       this.heat(id,0);
      }
     }
    
    }
    
    heater.init({
     id:'tst',
     temperature:100,
     min:10,
     max:200,
     increment:2
    });
    
    /*]]>*/
    </script>
    
    </body>
    
    </html>
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •