如何用JavaScript简单计算出旅行时间?

How to make simple calculation with JavaScript to know travel time?

我卡在这个上面了,它很简单,但我什么都不懂 JavaScript。

基本上我想基于三个不同的变量创建一个简单的计算来了解旅行时间。我是荷兰人,所以代码中有些词是荷兰语,有些是英语。第一个变量是 "woonplaats" ,它是起始位置。第二个是 "bestemming" ,这是目的地的位置。第三个是 "vervoer",这是您要乘坐的交通工具。

现在所有这些变量都有一个标准值。所以唯一需要做的就是计算。

这是我的脚本代码的样子:

function location() {

var woonplaats = document.getElementById("woonplaats").value;


switch (woonplaats) {
    case "amstelveen":
        locationAm();
        break;
    case "badhoevedorp":
        poelBa();
        break;

div2.innerHTML = "Jouw gemiddelde reistijd is: <b>" + tijd + "</b> km/h";




function locationAm() {

    var e = document.getElementById("bestemming");
    var eindbest = e.options[e.selectedIndex].value;
    var x = document.getElementById("bestemming");
    var vervoer = x.options[e.selectedIndex].value;


        if (eindbest == ameer) {
            var distance = 14500 ; }

            else if (eindbest === groning) {
            var distance = 183000 ; }

            else if(eindbest === zwolle) {
            var distance = 114000 ;
            }


        if (vervoer === kuruma ) {
            var time = distance / 28 ;
        }

        else if (vervoer === jitensha) {
         var time = var distance / 4 ;

         }

         else if ( vervoer === densha) {
         var time = var distance / 56 ;

         }

         else if (vervoer === scoot) {
         var time = var distance / 8 ; }




    div2.innerHTML = "your travel time will be <b>" + time + "</b> km/h";

}

function locationBa() {


    var e = document.getElementById("bestemming");
    var eindbest = e.options[e.selectedIndex].value;
    var x = document.getElementById("bestemming");
    var vervoer = x.options[e.selectedIndex].value;


     if (eindbest == ameer) {
            var distance = 13000 ; }

            else if (eindbest === groning) {
            var distance = 40000 ; }

            else if(eindbest === zwolle) {
            var distance = 600000 ;
            }


        if (vervoer === kuruma ) {
            var time = distance / 28 ;
        }

        else if (vervoer === jitensha) {
         var time = var distance / 4 ;

         }

         else if ( vervoer === densha) {
         var time = var distance / 56 ;

         }

         else if (vervoer === scoot) {
         var time = var distance / 8 ; }


    div2.innerHTML = "your travel time will be: <b>" + time + "</b>";

这是我的 body

<form>
                    I live in <select id="woonplaats">
                    <option value="amstelveen">Amstelveen</option>
                    <option value="badhoevedorp">Badhoevedorp</option>
                </select>

                    <p></p>
                    I'll travel to <select id="bestemming">
                    <option value="ameer">Aalsmeer</option>
                    <option value="groning">Groningen</option>
                    <option value="zwol">Zwolle</option>

                </select> 

                <p></p>
                My vehicle is <select id="vervoer">
                    <option value="kuruma">Auto</option>
                    <option value="jitensha">Fiets</option>
                    <option value="scoot">Scooter</option>
                    <option value="densha">Trein</option>
                </select>

                    <p></p>

                    <p></p>
                   calculate time <input onclick="poel()" type="button" value="Bereken!">
                </form>

                <p>&nbsp;</p>

                 <div id="div2">your travel time will be.. gemiddelde  ... km/h</div>

基本上这个想法是,第一个和第二个变量决定它们之间的距离,而车辆决定你行驶的速度,所以它应该计算行驶时间。

一个相对简单的问题需要大量代码。

您应该将距离编码为数据表而不是逻辑,并将车辆速度直接放入表格中作为单个 option 元素的 value 属性:

下面的代码完成了全部工作:

var distances = {
    amstelveen: {
        ameer: 14500,
        groning: 183000,
        einbest: 11400
    },
    badhoevedorp: {
        ameer: 13000,
        groning: 40000,
        zwolle: 600000
    }
};

document.getElementById('calculate').addEventListener('click', function () {
    var from = document.getElementById('woonplaats').value;
    var to = document.getElementById('bestemming').value;
    var speed = +document.getElementById('vervoer').value;

    var distance = distances[from][to];
    var time = distance / speed;
    document.getElementById('div2').innerHTML = "your travel time will be: <b>" + time + "</b>";
}, false);

演示在 http://jsfiddle.net/alnitak/cwdhbk2x/