www.webdeveloper.com
Results 1 to 6 of 6

Thread: Javascript Code Help

  1. #1
    Join Date
    Jul 2013
    Posts
    4

    Javascript Code Help

    I have the following JS code which creates a calculator, however the calculator isn't functioning properly. Only the last 2 commands for each set of outputs are working. Anyone know why?

    function weightVolume(a) {

    var outPut = '';
    if(a <= 90) { outPut = '0-76'; }
    if(a < 110) { outPut = '94-110'; }
    if(a < 130) { outPut = '111-127'; }
    if(a < 150) { outPut = '128-144'; }
    if(a < 170) { outPut = '145-161'; }
    if(a < 190) { outPut = '162-178'; }
    if(a < 210) { outPut = '196-212'; }
    if(a > 230) { outPut = '213-230'; }
    document.getElementById("volume").innerHTML = outPut + ' Litres';

    }

    function heightWidth(a) {
    var outPut = '';
    if(a <= 60) { outPut = '30'; }
    if(a <= 72) { outPut = '31-32'; }
    if(a > 72) { outPut = '32'; }
    document.getElementById("width").innerHTML = outPut + 'in';
    }

    function areaLength(a) {
    var outPut = '';
    if(a === "Cruising" ) { outPut = "10&rsquo;6 or 12&rsquo;"; }
    if(a === "Surf" ) { outPut = "10&rsquo; or Less"; }
    if(a === "Racing" ) { outPut = "12&rsquo; or More"; }
    document.getElementById("area").innerHTML = outPut;
    }

  2. #2
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,264
    If a is 80 then all lines of the function weightVolume are true except the last.
    Then you have to use some else statements !

  3. #3
    Join Date
    Jul 2013
    Posts
    4
    Could you give a suggestion?

  4. #4
    Join Date
    Sep 2007
    Posts
    315
    http://www.w3schools.com/js/js_if_else.asp

    http://www.w3schools.com/js/tryit.as...e=tryjs_elseif

    Code:
     
    <script type="text/javascript">
    
    var a = 80;
    
    var n = [a <= 90, a< 110, a < 130, a < 150, a < 170, a < 190, a < 210, a > 230];
    
    alert(n);
    
    var k = ['0-76','94-110','111-127','128-144','145-161','162-178','196-212','213-230'];
    
    var i, outPut = '';
     
      for(i=0; i< n.length; i++) {
    
      if( n[i] ) { outPut = k[i] + ' litres' ; break; }
    }
    
    alert('outPut= '+ outPut );
    
    </script>
    Last edited by Ayşe; 07-03-2013 at 04:21 PM.
    The Time Through Ages
    1. By the Time,
    2. Verily Man is in loss,
    3. Except such as have Faith, and do righteous deeds, and (join together) in the mutual enjoining of Truth, and of Patience and Constancy.

  5. #5
    Join Date
    Jul 2013
    Posts
    4
    Ayse,

    Thank you for the suggestion but that code doesn't work correctly.

  6. #6
    Join Date
    Jul 2013
    Posts
    4
    However, the link you provided helped me figure it out, so thank you!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles