0

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.

1
  • Maybe you should pass i to your function directly? (function adder(i) { ... }) Commented Sep 22, 2011 at 20:31

1 Answer 1

1

Pass i to adder() as an argument:

adder(i);

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

function adder( i ) {
   //...
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your answer. I tried that, but now it says that 'var horas[i] is undefined. Line 99' I can't find why it says that. If you want to check the complete code, I copied to pastebin, here [link]pastebin.com/dgfFnqKZ Thank you both, Senad and @Howard
I think it was my fault, I had the for loop for adder(); inside other function. I put it outside, and now it does nothing. It doesn't give neither error nor works

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.