Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Web Development
  3. JavaScript
  4. if statement not working

if statement not working

Scheduled Pinned Locked Moved JavaScript
htmlhelpquestion
6 Posts 4 Posters 8 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    chizzy42
    wrote on last edited by
    #1

    Hi All, Hope all is well well. I have a coding question I cant seem to figure out. I have a form in HTML5 which has columns that I want to sum and show in a totals box. The totals are summed when a button that calls the sum() function when clicked. From the code below I'm just trying to sum two boxes which represent a value for each hour total. My problem is that when I click on the button with only a value in the first box, then i get a NaN in the results box...no matter how many times i click it...but when i enter a value in the second box the sum is completed and the correct total is displayed. Could someone explain please why this only works with 2 values and not just 1....hope this is clear. Thanks to all who try regards

    function sum() {
    var result= 0;
        var txtFirstNo = document.getElementById('Text1').value;
        var txtSecondNo = document.getElementById('Text6').value;
    		
        if (!isNaN(txtFirstNo)){ 
    	result = parseInt(txtFirstNo);
    	document.getElementById('Tar').value = parseInt(result);
            }
            
            if (!isNaN(txtSecondNo)){ 
    	result = parseInt(txtFirstNo) + parseInt(txtSecondNo);
    	document.getElementById('Tar').value = result;
            }
            
           if (!isNaN(result)) {
                document.getElementById('Tar').value = result;
            }
        
        }
    
    N W 2 Replies Last reply
    0
    • C chizzy42

      Hi All, Hope all is well well. I have a coding question I cant seem to figure out. I have a form in HTML5 which has columns that I want to sum and show in a totals box. The totals are summed when a button that calls the sum() function when clicked. From the code below I'm just trying to sum two boxes which represent a value for each hour total. My problem is that when I click on the button with only a value in the first box, then i get a NaN in the results box...no matter how many times i click it...but when i enter a value in the second box the sum is completed and the correct total is displayed. Could someone explain please why this only works with 2 values and not just 1....hope this is clear. Thanks to all who try regards

      function sum() {
      var result= 0;
          var txtFirstNo = document.getElementById('Text1').value;
          var txtSecondNo = document.getElementById('Text6').value;
      		
          if (!isNaN(txtFirstNo)){ 
      	result = parseInt(txtFirstNo);
      	document.getElementById('Tar').value = parseInt(result);
              }
              
              if (!isNaN(txtSecondNo)){ 
      	result = parseInt(txtFirstNo) + parseInt(txtSecondNo);
      	document.getElementById('Tar').value = result;
              }
              
             if (!isNaN(result)) {
                  document.getElementById('Tar').value = result;
              }
          
          }
      
      N Offline
      N Offline
      NotTodayYo
      wrote on last edited by
      #2

      All you have to do is debug your code and you'll figure it out. Note, isNaN('') returns false because '' gets converted to a number first (0). But all you have to do is debug. Very easy.

      1 Reply Last reply
      0
      • C chizzy42

        Hi All, Hope all is well well. I have a coding question I cant seem to figure out. I have a form in HTML5 which has columns that I want to sum and show in a totals box. The totals are summed when a button that calls the sum() function when clicked. From the code below I'm just trying to sum two boxes which represent a value for each hour total. My problem is that when I click on the button with only a value in the first box, then i get a NaN in the results box...no matter how many times i click it...but when i enter a value in the second box the sum is completed and the correct total is displayed. Could someone explain please why this only works with 2 values and not just 1....hope this is clear. Thanks to all who try regards

        function sum() {
        var result= 0;
            var txtFirstNo = document.getElementById('Text1').value;
            var txtSecondNo = document.getElementById('Text6').value;
        		
            if (!isNaN(txtFirstNo)){ 
        	result = parseInt(txtFirstNo);
        	document.getElementById('Tar').value = parseInt(result);
                }
                
                if (!isNaN(txtSecondNo)){ 
        	result = parseInt(txtFirstNo) + parseInt(txtSecondNo);
        	document.getElementById('Tar').value = result;
                }
                
               if (!isNaN(result)) {
                    document.getElementById('Tar').value = result;
                }
            
            }
        
        W Offline
        W Offline
        W Balboos GHB
        wrote on last edited by
        #3

        NaN means "Not a Number" and that's telling you something of the problem. What is the value of what's in the empty box? If you parseInt() something that is in possibly not a number did you look up what parseInt() gives back[^]? OK - so you add the value from the box you typed in (a number) to the value in the box that has something that is almost certainly not a number:   how can it give you any sort of result except that it is not a number (NaN) ? What do you get if you type your name in the box (instead of a number)? Or even with a number in the other? One solution is to check the value and if parseInt() give you something that is not an INT then you set it to 0 . Are you doing that with in any way with your isNaN() test?

        Ravings en masse^

        "The difference between genius and stupidity is that genius has its limits." - Albert Einstein

        "If you are searching for perfection in others, then you seek disappointment. If you seek perfection in yourself, then you will find failure." - Balboos HaGadol Mar 2010

        C 1 Reply Last reply
        0
        • W W Balboos GHB

          NaN means "Not a Number" and that's telling you something of the problem. What is the value of what's in the empty box? If you parseInt() something that is in possibly not a number did you look up what parseInt() gives back[^]? OK - so you add the value from the box you typed in (a number) to the value in the box that has something that is almost certainly not a number:   how can it give you any sort of result except that it is not a number (NaN) ? What do you get if you type your name in the box (instead of a number)? Or even with a number in the other? One solution is to check the value and if parseInt() give you something that is not an INT then you set it to 0 . Are you doing that with in any way with your isNaN() test?

          Ravings en masse^

          "The difference between genius and stupidity is that genius has its limits." - Albert Einstein

          "If you are searching for perfection in others, then you seek disappointment. If you seek perfection in yourself, then you will find failure." - Balboos HaGadol Mar 2010

          C Offline
          C Offline
          chizzy42
          wrote on last edited by
          #4

          hi

          W∴ Balboos

          , thanks for the reply. Ive rethought the condition of the if statement and the following code seems to work. The only issue being if one of the text boxes is left empty then a NaN is given in the result . Just have to make sure the user puts a zero in for that hour just now to prevent that until i think of something else regards

              function sum() {
          
                  var txtFirstNo = document.getElementById('Text1').value;
                  var txtSecondNo = document.getElementById('Text6').value;
                  var txtThirdNo = document.getElementById('Text11').value;
              var txtFourthNo = document.getElementById('Text16').value;
          
          	if(!(document.getElementById('Text1').value).length==0){
                      result = parseInt(txtFirstNo);
          	document.getElementById('Tar').value = result;
                      }
          
                  if(!(document.getElementById('Text6').value).length==0){
          	result = parseInt(txtFirstNo) + parseInt(txtSecondNo);
          	document.getElementById('Tar').value = result;
                      }
                  
                      if(!(document.getElementById('Text11').value).length==0){
          	result = parseInt(txtFirstNo) + parseInt(txtSecondNo) + parseInt(txtThirdNo);
          	document.getElementById('Tar').value = result;
                      }
          
                      if(!(document.getElementById('Text16').value).length==0){
          	result = parseInt(txtFirstNo) + parseInt(txtSecondNo) + parseInt(txtThirdNo) + parseInt(txtFourthNo);
          	document.getElementById('Tar').value = result;
                      }  
              }
          

          ;)

          M 1 Reply Last reply
          0
          • C chizzy42

            hi

            W∴ Balboos

            , thanks for the reply. Ive rethought the condition of the if statement and the following code seems to work. The only issue being if one of the text boxes is left empty then a NaN is given in the result . Just have to make sure the user puts a zero in for that hour just now to prevent that until i think of something else regards

                function sum() {
            
                    var txtFirstNo = document.getElementById('Text1').value;
                    var txtSecondNo = document.getElementById('Text6').value;
                    var txtThirdNo = document.getElementById('Text11').value;
                var txtFourthNo = document.getElementById('Text16').value;
            
            	if(!(document.getElementById('Text1').value).length==0){
                        result = parseInt(txtFirstNo);
            	document.getElementById('Tar').value = result;
                        }
            
                    if(!(document.getElementById('Text6').value).length==0){
            	result = parseInt(txtFirstNo) + parseInt(txtSecondNo);
            	document.getElementById('Tar').value = result;
                        }
                    
                        if(!(document.getElementById('Text11').value).length==0){
            	result = parseInt(txtFirstNo) + parseInt(txtSecondNo) + parseInt(txtThirdNo);
            	document.getElementById('Tar').value = result;
                        }
            
                        if(!(document.getElementById('Text16').value).length==0){
            	result = parseInt(txtFirstNo) + parseInt(txtSecondNo) + parseInt(txtThirdNo) + parseInt(txtFourthNo);
            	document.getElementById('Tar').value = result;
                        }  
                }
            

            ;)

            M Offline
            M Offline
            Member_15100276
            wrote on last edited by
            #5

            Two things I think you need to focus on learning: Console Log, and Typeof. Javascript has a few primitive types of variables. It looks like you started going back and forth with int coming up as strings. I saw you put in parseInt which is what you need. What else may help is instead of putting things out to a browser element, you can access them with the Console. Console = press F12 and look at the "Console". (Except IE because IE still sucks) So you can try your first one with this:

            console.log("Hello World")

            This is rather important as next thing to try to see if a value is an Integer or not is this:

            console.log(typeof myVar)

            That should tell you Integer or String or Array. If you try to add a String to an Integer you get a hybrid and the Int is converted to a string.

            var myVar = "Hello" + 12345;
            console.log(myVar);

            The output of that will be "Hello12345" Happens with Numbers also, which is when you end up with NaN or "Not a Number" which can be checked with "isNaN(var)". So lets say you have this:

            var varA = 10;
            var varB = "20";
            var sum = varA + varB;

            Did you notice the quotes on varB? It means that although there are numeric characters in it, the variable is treated like a String, which causes varA to be typecast to a String. Your output there would be "1020" not the 30 like you expect. Back to the console for just a moment. If you need to check the value of a variable or object or something in your code without jumping thru a thousand hoops to even see what that is, you can "LOG" that. In the console you can type your variable name. Simply type "varA" without the quotes and the console will output whatever the VALUE of varA is. You can also try putting in "typeof varA" (again, no quotes) and see if the VALUE is an integer or string, which is where I think you have the most trouble! It doesnt fix your problem, but I hope this helps to undestand what the problem is and some of the tools you have available to you!

            C 1 Reply Last reply
            0
            • M Member_15100276

              Two things I think you need to focus on learning: Console Log, and Typeof. Javascript has a few primitive types of variables. It looks like you started going back and forth with int coming up as strings. I saw you put in parseInt which is what you need. What else may help is instead of putting things out to a browser element, you can access them with the Console. Console = press F12 and look at the "Console". (Except IE because IE still sucks) So you can try your first one with this:

              console.log("Hello World")

              This is rather important as next thing to try to see if a value is an Integer or not is this:

              console.log(typeof myVar)

              That should tell you Integer or String or Array. If you try to add a String to an Integer you get a hybrid and the Int is converted to a string.

              var myVar = "Hello" + 12345;
              console.log(myVar);

              The output of that will be "Hello12345" Happens with Numbers also, which is when you end up with NaN or "Not a Number" which can be checked with "isNaN(var)". So lets say you have this:

              var varA = 10;
              var varB = "20";
              var sum = varA + varB;

              Did you notice the quotes on varB? It means that although there are numeric characters in it, the variable is treated like a String, which causes varA to be typecast to a String. Your output there would be "1020" not the 30 like you expect. Back to the console for just a moment. If you need to check the value of a variable or object or something in your code without jumping thru a thousand hoops to even see what that is, you can "LOG" that. In the console you can type your variable name. Simply type "varA" without the quotes and the console will output whatever the VALUE of varA is. You can also try putting in "typeof varA" (again, no quotes) and see if the VALUE is an integer or string, which is where I think you have the most trouble! It doesnt fix your problem, but I hope this helps to undestand what the problem is and some of the tools you have available to you!

              C Offline
              C Offline
              chizzy42
              wrote on last edited by
              #6

              15100276

              Thanks alot for taking the time to reply in such a detailed way, its much appreciated. its a lot clearer now and the touchscreen project now sums the columns and ive added conditional formatting ...the coding would probably send shivers to your bones but tidying it up and making it more efficient is the next step. Again thanks for taking the time to reply.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups