这个开关声明我做错了什么?

时间:2016-06-03 21:28:12

标签: javascript switch-statement logic

我在 If / else 语句中使用它。我试图将其转换为switch语句,但我遇到了麻烦。我运行时似乎没有在控制台中出现任何错误。我应该能够输入当前的lat和lon以及我想要进入lat和lon的位置,输出应该是一个方向

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;
            }

    }

1 个答案:

答案 0 :(得分:1)

一切正常,你只是拼写了Latitude错误......

更改

 var end = intDestinationLatitude - latitdue;

var end = intDestinationLatitude - Latitude;

反之亦然...... javascript区分大小写......

在这里工作:

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

希望这有帮助