Home › Forums › JavaScript › Why the value of count not incrementing?
- This topic is empty.
-
AuthorPosts
-
February 8, 2014 at 2:36 am #162222
sayedtaqui
ParticipantI was writing some jQuery to remove all the li elements except the first one. I know the other way of doing it however I couldn’t understand the problem in this situation.
var ticker = $('#ticker'), tickerLi = ticker.find('li'), count = 0; tickerLi.each(function(){ console.log(count); if(count === 0) return true; else $(this).remove(); count++; });
why is the value of count not increasing ? If I remove
if(count === 0)
return true;
else
$(this).remove();condition it does however with this condition it always returns 0.
** is there any logical reason for this?**
thanks.
February 8, 2014 at 12:25 pm #162248dyr
ParticipantThe value of count is 0 the first time through the loop so the first conditional is true and the loop returns true. The only logic that has failed is yours! :P
You must do
var ticker = $('#ticker'), tickerLi = ticker.find('li'), count = 0; tickerLi.each(function(){ console.log(count); if(count === tickerLi.length - 1) // exit when count reached number of LI's - 1 (one LI is left) return; else $(this).remove(); count++; });
Count starts at 0 and counts up to (the number of LI’s minus 1).
OR
var ticker = $('#ticker'), tickerLi = ticker.find('li'), count = tickerLi.length; tickerLi.each(function(){ console.log(count); if(count === 1) // exit when there's 1 LI left return true; else $(this).remove(); count--; // counting down from tickerLi.length to 1 });
Count starts at the number of LI’s and counts down to 1.
February 8, 2014 at 1:11 pm #162252__
ParticipantI was writing some jQuery to remove all the li elements except the first one.
Since you’re using jQuery anyway, you could simply do
$('#ticker li:not(:first)').remove();
February 9, 2014 at 8:11 am #162280sayedtaqui
Participant@dyr Thanks a lot , but I m not able to understand what actually happened in my case. The loop is iterating five times however the count doesn’t increment. Can you please explain a little.
if(count === 0) return true; else $(this).remove();
why this logic is wrong and why the value of count is not increasing.
February 9, 2014 at 8:57 am #162284dyr
ParticipantBecause in your code you SET COUNT to 0 and then the FIRST thing you check in your loop is if COUNT IS 0, which it is. That’s the most fundamental logic at work right there. You must learn to think like a computer behaves.
You need to read my post more closely.
Your code, my comments:
var ticker = $('#ticker'), tickerLi = ticker.find('li'), count = 0; // count is 0 tickerLi.each(function(){ // count is still 0 if(count === 0) // evaluates to true the first time through the loop return true; // gets executed else $(this).remove(); // never gets executed count++; });
February 9, 2014 at 10:09 pm #162318sayedtaqui
ParticipantAhh now I understand the only misconception I had, was this
‘return false’ == ‘break’
‘return true’ == ‘continue’
like ‘return false’ breaks and doesn’t let rest of the code get executed, ‘return true’ do the opposite, it lets the rest of code executed.
thanks for the clarification.February 9, 2014 at 11:04 pm #162321__
ParticipantAhh now I understand the only misconception I had
… like ‘return false’ breaks and doesn’t let rest of the code get executed, ‘return true’ do the opposite, it lets the rest of code executed.
Right;
return
always does exactly that: it returns from the function, and none of the code following is executed. Whether you returntrue
orfalse
or {some other value} has no impact on that.the idea of
break
andcontinue
apply to loops. While.each()
does loop over each item in the jQuery object, your function isn’t actually inside that loop (it’s called from inside that loop).February 9, 2014 at 11:09 pm #162322sayedtaqui
ParticipantThanks, got it.
February 9, 2014 at 11:11 pm #162323__
ParticipantYou’re welcome.
(Please note I edited my post above: I had misunderstood part of your comment.)
-
AuthorPosts
- The forum ‘JavaScript’ is closed to new topics and replies.