SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Dec 2011
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    If else not working...

    I have if Else statements that always returns the else part of the statement but never the "if" side. Could you look at my code and tell me where I am going wrong? I am aware I have too many variables declared.

    Code:
     var car_type = new Array();
    		car_type["2door"]=41446;
    		car_type["4door"]=52776;
    
    	//Set up array that represents the metal_type weight
    var metal_type= new Array();
    		metal_type["selectmetal"]=0;
    		metal_type["Aluminum"]=4.3512;
    		metal_type["Gold"]=30.9912;
    		metal_type["Platinum"]=34.09692;
    		metal_type["Silver"]=16.8276;
    
    	// getCarStorage() finds the storage based on the car storage space.
    
    function getCarStorage()
    {
    var carStorageArea=0;
    		//refers form id="carForm"
    var theForm = document.forms["carForm"];
    	
    var selectedCar = theForm.elements["selectedCar"];
    
    carStorageArea = document.getElementById("selectedCar").value; 
    
    		//We return carStorageArea
    return carStorageArea;
    }
    		//This function finds the metal bar weight
    function getMetalDensity()
    {
    var carStorageSpace=0;
    		//Get a reference to the form id="carForm"
    var theForm = document.forms["carForm"];
    		//Get a reference to the select id="metal"
    var selectedmetal = theForm.elements["metal"];
    		//equal to value user chose such as metal_type["Aluminum".value] would be equal to 4.3512
    carStorageSpace = metal_type[selectedmetal.value];
    		//finally we return carStorageSpace
    return carStorageSpace;
    }
    function calculateTotal()
    {
    var divobj = document.getElementById('total');
    
    		//Here we get the total by calling our function
    		//Each function returns a number
    var totalCanFit = getCarStorage() / 440;
    var spaceUsable = totalCanFit * .33;
    // var fit = car_type / 44.40625;
    var metal_name = document.getElementById("metal").value;
    var barsNeededFor1800 = 1800 / metal_type[metal_name];
    var total = barsNeededFor1800 * metal_type[metal_name];
    var barLimit = totalCanFit /  spaceUsable;
    var weightLimit = barsNeededFor1800 / spaceUsable;
    var doesFit = barsNeededFor1800 - totalCanFit
    
    var respond = "You will not be able to complete this job.  Please select either another car or bar type.";
    
    	if (barLimit <1)
      {
    	divobj.innerHTML = ("This will not work because you have surpassed the limit of " + barLimit.toFixed(0) );
      }
    	else if (weightLimit <1)
      {
    	divobj.innerHTML = ("This will not work: wl="+ weightLimit+","+spaceUsable+","+barsNeededFor1800+";");
      }
    else
    {
    respond = 'With the total cargo area of the chosen car, you are able to fit ' +totalCanFit.toFixed(0) + 
    		' bars. You will need ' + barsNeededFor1800.toFixed(0) + 
    		' bars to reach to the 1800 lbs limit. ';

  2. #2
    Non-Member
    Join Date
    Apr 2011
    Location
    no fixed address
    Posts
    851
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
                if (barLimit <1)
                {
                    divobj.innerHTML = ("This will not work because you have surpassed the limit of " + barLimit.toFixed(0) );
                }
                else if (weightLimit <1)
                {
                    divobj.innerHTML = ("This will not work: wl="+ weightLimit+","+spaceUsable+","+barsNeededFor1800+";");
                }
                else
                {
                    respond = 'With the total cargo area of the chosen car, you are able to fit ' +totalCanFit.toFixed(0) + 
                        ' bars. You will need ' + barsNeededFor1800.toFixed(0) + 
                        ' bars to reach to the 1800 lbs limit. ';
                }
    that means that barLimit is always >= 1 and weightLimit is always >= 1.

    You just need to do some basic debugging to find out why. You can use the developer tools in your browser to step through your code to check values of variables at various points or you can use alert() statements to check values of variables at different points. Just step through your code line by line until you find a variable whose value is not what it should be and then back track your code to find where the value is being assigned and fix it.

    Think of debugging as character building and a skill you need to develop if you're going to be a coder of any kind.

  3. #3
    SitePoint Member
    Join Date
    Dec 2011
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    LOL. Thanks! I was hoping that someone someone would tell me which line/lines I need to look at a bit more carefully. I have tried what you said to do but cannot see it. I am going cross eyed from all the reading. I have used Chrome and Firebug to attempt to find the issue but either I don't have the correct plugin, I don't know which one to use or just too lame to be able to figure this out.

  4. #4
    Non-Member
    Join Date
    Apr 2011
    Location
    no fixed address
    Posts
    851
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by flyboynm View Post
    LOL. Thanks! I was hoping that someone someone would tell me which line/lines I need to look at a bit more carefully. ......
    Debugging can be a pita and I certainly don't waste time debugging other peoples' screw ups - well not for free at least

    If your intention is to just dump code in a forum and ask someone else to fix it for you for free, then hopefully someone else will come along

    I described the process I would follow to debug your code, but I'm not going to actually do it for you.

  5. #5
    SitePoint Member
    Join Date
    Dec 2011
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I understand fully. I just cannot find the issue and that is why I posted on here. I was hoping that someone would tell me where I am messing up (such as a line # saying "you might want to check such and such line") and then I can take a closer look at the code to figure out why it isn't working. I was not asking someone to fix my code for me.

  6. #6
    Non-Member
    Join Date
    Apr 2011
    Location
    no fixed address
    Posts
    851
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by flyboynm View Post
    ..... I was hoping that someone would tell me where I am messing up (such as a line # saying "you might want to check such and such line") and then I can take a closer look at the code to figure out why it isn't working. I was not asking someone to fix my code for me.
    ok, but I don't understand why you can find the line by yourself. Even if you're not comfortable using browser developer tools, since you don't have a lot of lines of code you should be able to at least use simple alert() statements to check values of variables and so by stepping through your code with alerts() you should be able to find where a variable has an incorrect value and then fix the line that assigns the incorrect value.

    Like I said, if you want someone to step through the code for you, then hopefully someone else will come along.


Bookmarks

Posting Permissions

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