PDA

View Full Version : ElseIf Counterpart


Bobba Buoy
04-06-2004, 09:47 AM
If you want to have a vb-like ElseIf structure in javascript, how do you do that?

Example: I want a greeting that says, "Good Morning" if it is before noon, "Good Afternoon" if it is before 5:00pm, and "Good Evening" otherwise.

Here is the 2-greeting version:


<script type="text/javascript">
var d = new Date()
var time = d.getHours()

if (time < 12)
{
document.write("<b>Good morning! Welcome to Some Teacher's Site!</b>")
}
else
{
document.write("<b>Good day! Welcome to Some Teacher's Site!</b>")
}
</script>


Thanks!

Anemic Royalty
04-06-2004, 09:50 AM
Oh my gawd. You are sooo dumb.

My grandma knows how to code that.

agent002
04-06-2004, 09:51 AM
In JavaScript, it is else if, so:
if(iLike == 'bananas'){
alert('I like bananas!');
}
else if(iLike == 'oranges'){
alert('I like oranges!');
}
else{
alert('I like something else! (probably cherries!)');
}


Originally posted by Anemic Royalty
Oh my gawd. You are sooo dumb.

My grandma knows how to code that.
we don't need smart asses like you

Anemic Royalty
04-06-2004, 10:03 AM
oh yeah... well you like fruit, fruit.

Bobba Buoy
04-06-2004, 10:05 AM
Thanks, agent002, for your help and your reaction to the 'other' posts.

I appreciate it!

Ian
04-06-2004, 07:15 PM
Anemic Royalty, worthless comments like that are not welcome or necessary around here. I'm not wasting my time giving you a warning as it appears the only reason you signed up was to give some abuse so I have banned you. If you have any problem as to why you were banned feel free to PM me.
Thanks.

eRad
04-06-2004, 07:38 PM
Originally posted by Ian
Anemic Royalty, worthless comments like that are not welcome or necessary around here. I'm not wasting my time giving you a warning as it appears the only reason you signed up was to give some abuse so I have banned you. If you have any problem as to why you were banned feel free to PM me.
Thanks.
Smooth.

It's fine to have a little humour, especially in a slightly humourous question such as the one posed, but that's just unecessary.

Must say it's good to see the kind of behaviour enforced in these forums. I have been in enough to know that it is a rare commodity.

Kram
04-06-2004, 09:46 PM
MAN that stuff annoys me, who does that kind of thing?

PennyRoyal Tea
04-07-2004, 09:41 AM
Originally posted by Kram
MAN that stuff annoys me, who does that kind of thing?

I know, I find banning people to be kind of childish.

Goldilocks
04-07-2004, 09:49 AM
Originally posted by PennyRoyal Tea
I know, I find banning people to be kind of childish.

I don't think Kram was talking about banning people. Besides, we don't make a habit of banning people unless it is necessary. Banning isn't childish, sometimes it is the only way we can keep these forums in order.

And yes, I know that you have been banned and re-registered. If you choose to cause more trouble we will do an IP ban.

agent002
04-07-2004, 09:52 AM
Originally posted by PennyRoyal Tea
I know, I find banning people to be kind of childish.
so are you anemic wise ass back with another username or? I also believe Kram meant to ask why people like you are allowed in social life, not why you were banned. I understood you were supposed to talk with Ian with PMs, not register another username.

BTW, have you ever thought of moving to Siberia?

Pegasus
04-07-2004, 12:02 PM
Umm... before this thread degenerates any further....?

So what we're looking at for a multiple if/else statement is something like this?


<script type="text/javascript">
var d = new Date()
var time = d.getHours()

if (time < 12)
{
document.write("<b>Good morning! Welcome to Some Teacher's Site!</b>")
}
else if (time >12<17)
{document.write("<b>Good afternoon! Welcome to Some Teacher's Site!</b>")
}
else
{
document.write("<b>Good evening! Welcome to Some Teacher's Site!</b>")
}
</script>

Is that how you change the time variable? Or am I missing something?

Peg

agent002
04-07-2004, 12:07 PM
It went a little wrong, Peg.
else if (time >12<17)
that should be
else if (time>12 || time<17)
:)

Pegasus
04-07-2004, 12:24 PM
Ah! *g* Well, considering that I knew what to say and only got my pronounciation wrong, I'm doing okay. I know precious little about Javascript (not much past what I've learned from "Javascript for Dummies).

I knew it didn't look right, but I wasn't sure how it was supposed to be written. Thanks.

I'm putting together a resume to apply for a job at the local school board. Or rather, I'm putting together a website idea that I'm going to present to them. I think a script like that would impress them. *lol* They're easily impressed, I think.

Thanks!

Peg

Kram
04-08-2004, 01:01 AM
Sorry for the late response, but i was talking about the guy giving crap to the poster. Not banning. I think banning is very good, especially to those who deserve it.

Vincent Puglia
04-08-2004, 08:33 AM
Hi,

Here's another version.


<script type="text/javascript" language="javascript">

var d = new Date()
var time = d.getHours()

var msgArray = new Array()
msgArray[0] = "morning";
msgArray[1] = "afternoon";
msgArray[2] = "evening";

var teachArray = new Array();
teachArray[0] = 'Pegasus'
teachArray[1] = 'Ms. Doubtfire'
teachArray[2] = 'Mr. Chips'


function doit(divID, teachID)
{

if (time < 12) msg = msgArray[0];
else if (time > 12 < 17) msg = msgArray[1];
else msg = msgArray[2];

document.getElementById(divID).innerHTML = "<b>Good " + msg + "! Welcome to " + teachArray[teachID] + "'s Site!</b>"
document.getElementById(divID).style.display = 'block';
}
</script>
</head>
<body>
<div id="theGreeting" style="display:none"></div>
<a href="#" id="0" onclick="doit('theGreeting',this.id)">Pegasus</a><br />
<a href="#" id="1" onclick="doit('theGreeting',this.id)">Ms. Doubtfire</a><br />
<a href="#" id="2" onclick="doit('theGreeting',this.id)">Mr.Chips</a><br />
</body>



Vinny