-1

Here I have two functions: updateTooltipContent and distance.

When I try to call distance(latt) in updateTooltipContent it does not return any value. I cannot see why not

CODE:

function updateTooltipContent() {
    var fullt = $(this).width();
    var startt = $(this).position().left + 200;
    var endt = fullt + startt;
    var latt = $(this).attr("lat");
    return "Spending: " + formatTime(fullt) +  
     "</br>  Between:(" + formatTime(startt) + " and " + 
      formatTime(endt) + ") </br>" + distance(latt) + "km";
}
});

function distance(latt) {
    var bigArray = nArray();
    var dis = 0.00;
    for (var x = 0; x < bigArray.length; x++) {
        if (bigArray[x].lat == latt) {
            dis = bigArray[x].DISTANCE_FROM_PREVIOUS_OBJECT_LOCATION;
            break; // no point doing anymore loops as we've found the answer
        }
        return dis;
    }
}

This code work great but when I try to put some of code in function then wont to work: WORKING CODE WITHOUT FUNCTION DISTANCE()

function updateTooltipContent() {
    var fullt = $(this).width();
    var startt = $( this ).position().left + 200;
    var endt = fullt + startt;
      var latt = $(this).attr("lat");
      var bigArray = nArray();
var distance = 0.00;

for(var x = 0; x < bigArray.length; x++)
{
    if(bigArray[x].lat == latt)
    {
        distance = bigArray[x].DISTANCE_FROM_PREVIOUS_OBJECT_LOCATION;
        break; // no point doing anymore loops as we've found the answer
    }        
}
      return "Spending: "+formatTime(fullt) + "</br>  Between:("+formatTime(startt) + " and " +formatTime(endt)+") </br>" + distance.toFixed(2) + "km";
    }
});
11
  • 5
    " there is some error " Do we have to guess the error or are you going to tell us about it? Learn how to debug JavaScript. Commented Aug 5, 2013 at 8:12
  • yes fnction dont return any value, distance(latt) dont return any value Commented Aug 5, 2013 at 8:12
  • Is bigArray.length bigger than 0? Otherwise your function doesn't return anything. Commented Aug 5, 2013 at 8:13
  • 1
    how do you call updateTooltipContent()? what is this inside that function? Commented Aug 5, 2013 at 8:14
  • 2
    Formatting your code properly shows that you have an addition }); in your code. Just saying. What is nArray? There is too much information missing to help you. Please provide more information and a jsfiddle.net demo that reproduces the problem. Commented Aug 5, 2013 at 8:14

1 Answer 1

1

Once bigArray[x].lat == latt, statement break will be invoked, your for loop will be skipped immediately; since your return statement stays in the loop, of course nothing will return.

To fix this issue, just move your return statement out of the for loop.

Sign up to request clarification or add additional context in comments.

1 Comment

WORK, THANKS, SO EASY

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.