Jump to content

Ternary Problem


paulmo

Recommended Posts

Put parenthesis around the expression:

(time > 12 && time < 18)? ... : ...

By the way, have you stopped to think what would happen if the time happened to be exactly 12?

Link to comment
Share on other sites

"isn't working" doesn't help much. If you mean that 12 gets the wrong response, I can see that quite clearly. You don't actually have a condition that ever matches 12.Otherwise, it looks fine. I pasted it into my Error Console, changed document.write to alert, and no problems except for the one I mentioned.What other problems do you have?

Link to comment
Share on other sites

The time>12 && time<18 part isn't working:
time<12 ? document.write(" morning, ") : (time>12 && time<18  ? document.write(" afternoon, ") : document.write(" evening, "));

Thanks for suggestions.

hmmm..... let's try something like this:
var time_of_day = "";time_of_day = ( (time < 12) ? "morning" : ((time >= 12 && time < 18) ? "afternoon" : "evening") );document.write("Hello.  Good " + time_of_day);

edit: basically DD was getting at what I was meaning to point out, which is that 12 never gets matched. Is that the case you were testing for?

Link to comment
Share on other sites

The parenthesis really shouldn't matter, unless we're grouping more complex logical conditions in a single expression. The original actual has more parentheses than required (unless you like them for readability, which is fine).

Link to comment
Share on other sites

The parenthesis really shouldn't matter, unless we're grouping more complex logical conditions in a single expression. The original actual has more parentheses than required (unless you like them for readability, which is fine).
good point. I often like them for the same reason you cite, readability. I like to use them to help break down statements within multi-line statements, like when using (multiple) ternary operators. Also, it should be noted that the OP should be writing out time just verify that it indeed even contains a value.
Link to comment
Share on other sites

You know something's right with the world when three different programmers, thousands of miles away from each other, fixate on exactly the same issue in something like a minute. (It looks like more, but you could see at the time that thescientist was working on his first response for quite while.)You also know that the afternoon before US Thanksgiving is pretty danged slow . . .

Link to comment
Share on other sites

hah, you said three programmers, and I had completely overlooked Ingolme's post! doh, the afternoon isn't going that slow for me, lol. :)

Link to comment
Share on other sites

Thanks ingolme for suggestions and DD for >= and Scientist for variable approach. Trying it out now with different times, it's all working. Seemed a couple days ago it wasn't going to "evening." I might have been disoriented.Oh and happy Thanksgiving to all of you, esp. Justsomeguy, Deirde's Dad and Scientist. You guys are really great. I'm so grateful for your responsiveness in this forum.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...