jjdouble000 jjdouble000 - 6 months ago 9
Javascript Question

what am I doing wrong with this switch statment?

I have it working in an If/else statement. I am trying to convert it into a switch statement but I am having trouble. I don't seem to be getting any errors in the console when I run it. I should be able to input current lat and lon and where I want to go in lat and lon and the output should be a direction

function caseSwitch(intCurrentLatitude, intCurrentLongitude, intDestinationLatitude, intDestinationLongitude){
//var intCurrentLatitude = parseFloat(prompt("What is your current Latitude?"));
//var intCurrentLongitude = parseFloat(prompt("What is your current Longitude?"));
//var intDestinationLatitude = parseFloat(prompt("What is your destination Latitude?"));
//var intDestinationLongitude = parseFloat(prompt("What is your destination longitude?"));

var Latitude = intCurrentLatitude;
var longitude = intCurrentLongitude;

var latitude_end = intDestinationLatitude;
var longitude_end = intDestinationLongitude;
var output = document.getElementById("case/switchConstruct");

var end = intDestinationLatitude - latitdue;
var end2 = intDestinationLongitude - longitude;

switch (true){
case (end > 0 && end2 > 0) :

output.textContent = "We'd be headed North East, capt'n!";
break;
case(end > 0 && end2 < 0) :

output.textContent = "Ye'd best head North West, captain!";
break;
case(end < 0 && end2 > 0) :

output.textContent = "Ye'd best head South East, captain!";
break;
case(end < 0 && end2 < 0) :

output.textContent = "Ye'd best head South West, captain!";
break;
case(end === 0 && end2 === 0) :

output.textContent = "Ye'd best head some direction, captain!";
break;
case(end < 0 && end2 === 0) :

output.textContent = "Ye'd best head South, captain!";
break;
case(end > 0 && end2 === 0) :

output.textContent = "Ye'd best head North, captain!";
break;
case(end === 0 && end2 < 0) :

output.textContent = "Ye'd best head West, captain!";
break;
case(end === 0 && end2 > 0) :

output.textContent = "Ye'd best head East, captain!";
break;
default :

output.textContent = "Capt'n, give us better directions!?!";
break;
}

}

Answer

Everything works fine , you just spelled Latitude wrong....

Change :

 var end = intDestinationLatitude - latitdue;

To:

var end = intDestinationLatitude - Latitude;

or vice versa...javascript is case sensitive...

Working here:

http://codepen.io/theConstructor/pen/OXVxbK?editors=1010

Hope this helps