开发者

Javascript function returning fixed value from array

I'm newbie with JavaScript, so I decided to develop a little application which is supposed to show the schedule of the streetcar of the place I live, because of the lack of the information on the official webpage.

I have several arrays with the starting time of the line, and as the time to reach each station it's the same, I only have to add the total minutes to the first hour.

There's a form开发者_开发问答 for the user to set a range of hours. So, my main problem is that the "adder();" function is supposed to iterate and print all the values from an array. Instead of doing that, it takes always the same index, 24, so if the array returned has less than 24 indexes, it does not work.

Here's the HTML:

< input type="button" class="submit" value="Enviar" onclick="caller()"/>

JavaScript:

function cropHours(i){

        if (i.substr(0,2) >= hora1user_recortada && i.substr(0,2) <= hora2user_recortada) {
            horas.push(i);
        }
        return horas;
 }

function adder() {
    minInicio1 = horas[i].substr(0,2);
    minInicio2 = horas[i].substr(3,2);
    document.getElementById("test4").innerHTML = "---" + minInicio1+"_"+minInicio2;
    y = parseInt(total) + parseInt(minInicio2);
   document.getElementById("test5").innerHTML = "total vale "+total+"minInicio1 vale "+minInicio1+"... minInicio2 vale "+minInicio2+"...Y vale "+y;
    html += "<td>"+y+"</td>";
    document.getElementById("horario").innerHTML = html;
}

This is a part of another function:

if (platform == 1) {
    for (var j = 0; j <= indexorigen; j++) {
        total += mins1[j];
    }
    for (var j = 0; j <= indexdestino; j++) {
        total2 += mins1[j];
    }
    if (today !== "Sábado" || today !== "Domingo") {
        for each (var i in horainiciolaboral1) {
            cropHours(i);
            //adder(horainiciolaboral1);
        }
    } else {

        for each (var i in horainiciofinde1) {
            cropHours(i);
        }
    }
} else {
    for (var x = 0; x <= indexorigen; x++) {
            total += mins2[x];
        }
        for (var x = 0; x <= indexdestino; x++) {
            total2 += mins2[x];
        }
    if (today !== "Sábado" || today !== "Domingo") {
        for each (var i in horainiciolaboral2) {
            cropHours(i);
        }
    } else {
        for each (var i in horainiciofinde2) {
            cropHours(i);
        }
    }
}

/*for (var i = 0; i <= horainiciolaboral1.length; i++) {
    adder(horainiciolaboral1);
}*/

//horario = horas.slice(11);
for each (var i in horas) {
    adder();
}
document.getElementById("test6").innerHTML = horas;
document.getElementById("test3").innerHTML = total + "----" + total2;

// ******************************************
// ** FUNCTION WHICH CALLS EVERY FUNCTION  **
// ******************************************
// STARTS
function caller() {
cleaner();
retrieve_origen(); 
retrieve_destino(); 
getIndex(); 
sumMinutes(); 
getHours();
}

This is the problem: for each (var i in horas) { adder(); }

Thank you in advance.


Pass i to adder() as an argument:

adder(i);

...and define it as a parameter in the function:

function adder( i ) {
   //...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜