Project: Holy Guacamole!

holy-guacamole-featured

It was pointed out to me that depending on the radius parameter I set, the results were often more than 1000 meters away.  For readability’s sake, he suggested I change how it appeared if it could be translated into kilometers.  My research let me to find a new (to me) method called “toFixed” that would convert my number into a string to the number of decimal points I preferred.

I knew I would want to create an “if-else” statement within my already existing for-loop.

if (places[i].venue.location.distance > 1000) {
     var dist = $('<p class="dist">').text('You are '+ ((places[i].venue.location.distance/1000).toFixed(1)) + 'km away.');
} else {
     var dist = $('<p class="dist">').text('You are ' + places[i].venue.location.distance + 'm away.');
}

The above is how that statement ended up.  If the distance of the result was greater than 1000 meters: take that distance, divide it by 1000 and then round it to 1 place after the decimal point and concatenate ‘km away’ at the end.  Otherwise, take the distance as it is and concatenate ‘m away’ at the end.

URL: www.allisontarr.com/guacamole

Github: https://github.com/allisonplus/API-guacamole

Leave a Reply

Your email address will not be published. Required fields are marked *