-1

I'm sure this has something to do with that it's nearly 5 AM...and that I'm missing something obvious.

Here is my code:

var dayInMonth = 2,
    lastDayNum = 30;

console.log(dayInMonth, (dayInMonth > lastDayNum)); // displays "2 false"

for(dayInMonth; dayInMonth > lastDayNum; dayInMonth++){
    console.log("here!") // not displaying anything
}

What is stopping the for loop from executing theconsole.log() statement?

4
  • 4
    Obviously dayInMonth > lastDayNum is false, i.e 2<30.... Take a nap and try again Commented Sep 14, 2012 at 8:57
  • 2
    +1 for take a nap. Naps rock! Commented Sep 14, 2012 at 8:58
  • Thank you everyone! The perils of programming on coffee and no sleep... Commented Sep 14, 2012 at 8:59
  • +1 as you don't deserve to lose rep for being tired. Go to sleep. Commented Sep 14, 2012 at 9:01

6 Answers 6

1

Wrong logical test (< rather than >);

for(; dayInMonth < lastDayNum; dayInMonth++){
    console.log("here!") // not displaying anything
}​
Sign up to request clarification or add additional context in comments.

Comments

1

dayInMonth > lastDayNum will never be greater

Comments

1

dayInMonth > lastDayNum should be dayInMonth <= lastDayNum, right?

Comments

1
for(dayInMonth; dayInMonth < lastDayNum; dayInMonth++){
    alert("here!") // not displaying anything
}​

You want < not >.

Comments

1

Try

dayInMonth < lastDayNum

A for loop is executed as long as the second parameter is true, not until it's false.

Comments

1
var dayInMonth = 2,
    lastDayNum = 30;

console.log(dayInMonth, (dayInMonth > lastDayNum)); // displays "2 false"

for(dayInMonth; dayInMonth < lastDayNum; dayInMonth++){
    console.log("here!") // not displaying anything
}

Inside for <, not >

Comments

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.