HTML and CSS Reference
In-Depth Information
The code for this exercise can be found in folder 5.
Here are the steps:
1. Make sure that you have the geolocation.js file open in your text editor.
2. Create an if statement at the bottom of your findNearest() function and set the condition to: d1 <=
d2 && d1 <= d3 . The purpose of this is to determine whether the distance to location 1 is the smallest
of the three distances. The condition checks first to see if the value of d1 (location 1) is smaller or equal to
the value of d2 (location 2). If it is, it will also check to see if the value of d1 is smaller or equal to the
value of d3 (location 3). Both conditions need to be met in order for the complete statement to evaluate to
true.
function findNearest(lat, lon) {
...
// Find the smallest distance.
if (d1 <= d2 && d1 <= d3) {
}
}
3. Within this if block, add code that will add the class nearest to the <section> element with the ID
location1 .
function findNearest(lat, lon) {
...
// Find the smallest distance.
if (d1 <= d2 && d1 <= d3) {
// Location 1
document.getElementById(“location1").className = “nearest";
}
}
4. Now add an else if condition to the if statement. This time, you are going to be checking to see if loca-
tion 2 is the nearest, so you want to use the following condition: d2 <= d1 && d2 <= d3 .
function findNearest(lat, lon) {
...
// Find the smallest distance.
if (d1 <= d2 && d1 <= d3) {
// Location 1
document.getElementById(“location1").className = “nearest";
} else if (d2 <= d1 && d2 <= d3) {
}
}
Search WWH ::




Custom Search