Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
145
rated 0 times [  149] [ 4]  / answers: 1 / hits: 33949  / 11 Years ago, tue, june 25, 2013, 12:00:00

How do I check if I'm on the last iteration of this loop? I'm sorry for asking this question. I'm used to programming in VB.NET and javascript seems very cryptic by nature.



if (QuerySplit.length > 1) {
var NewQuery
for (i=0; i<QuerySplit.length; i++)
{
// if we're not on the last iteration then
if (i != QuerySplit.length) {
// build the new query
NewQuery = QuerySplit[i].value + AND
}
}
}

More From » javascript

 Answers
19

Take note that you need var NewQuery = ; and check for length - 1. Also, the last if statement is just a guess of what you probably want to do:



if (QuerySplit.length > 1) {
var NewQuery = ;
for (i = 0; i < QuerySplit.length; i++) {
// if we're not on the last iteration then
if (i != QuerySplit.length - 1) {
// build the new query
NewQuery += QuerySplit[i].value + AND
} else {
NewQuery += QuerySplit[i].value;
}
}
}


If QuerySplit.length is 4, then:



0, 1, 2, 3



...are the indexes. So you want to check for when the index is 3 and that's your last iteration.


[#77419] Monday, June 24, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
lara

Total Points: 462
Total Questions: 100
Total Answers: 102

Location: Jersey
Member since Mon, Jun 14, 2021
3 Years ago
lara questions
;