- This topic is empty.
-
AuthorPosts
-
December 4, 2008 at 11:39 am #23697daGUYMember
I know HTML and CSS very well, but I’m pretty much a complete newbie at JavaScript. In order to kind of teach myself, my goal is to solve this simple problem:
In my town, you have to park on a different side of the street depending on what day of the week it is. On Tuesday, Thursday, and Saturday, you cannot park on the side closest to my house between 2:00 and 6:00 AM. It’s confusing, because when I drive home after work, I’m always parking the car the night before the day that the ban applies. For example, when I go home tonight (Thursday), I will park closest to my house because at 2:00 AM it will be Friday.
So basically, what I want the script to do is tell me to park on one side if it’s Tuesday, Thursday, or Saturday, and park on the other if it’s any other day. How would I do that?
(Ignore the fact that I can just see where everyone else is parked – I wanted to do this just as a JavaScript exercise :lol:)
December 4, 2008 at 1:49 pm #51817Toby PitmanParticipantI’d suggest making a timetable and sticking it on your fridge!
;)
or….use the date() method and some ‘if’ statements. This should get you started.
Code:var currentTime = new Date()
var day = currentTime.getDay();var weekday=new Array(7);
weekday[0]=”Sunday”;
weekday[1]=”Monday”;
weekday[2]=”Tuesday”;
weekday[3]=”Wednesday”;
weekday[4]=”Thursday”;
weekday[5]=”Friday”;
weekday[6]=”Saturday”;document.write(weekday[day]);
…..or sell your car!
December 5, 2008 at 11:12 am #51939Toby PitmanParticipantNice! Let’s hope it saves you some money in parking fines!
Just tried this and I think it should work…got to wait till tomorrow to check though!!! :oops:
Code:var currentTime = new Date()
var day = currentTime.getDay();var weekday=new Array(7);
weekday[0]=”Sunday”;
weekday[1]=”Monday”;
weekday[2]=”Tuesday”;
weekday[3]=”Wednesday”;
weekday[4]=”Thursday”;
weekday[5]=”Friday”;
weekday[6]=”Saturday”;if (day == [0, 2, 4, 6])
{
document.write(“It’s ” + weekday[day] + “! Park near!”);
}
else
{
document.write(“It’s ” + weekday[day] + “! Park opposite!”);
}I don’t ever really write traditional JS so this was a good exercise for me too! I think that might be the first time I’ve ever used an array!
Maybe we should set another one?
December 8, 2008 at 11:39 am #52005JamesMemberI thought I’d give it a go :)
Here:
Code:function getPositionForToday() {
var days = (‘Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday’).split(‘,’),
currentDay = (new Date()).getDay();
return ‘It’s ‘ + days[currentDay] + ‘ today so you should be parking on the ‘ + ( currentDay%2===0 ? ‘NEAR’ : ‘OPPOSITE’ ) + ‘ side.’;
}alert(getPositionForToday());
-
AuthorPosts
- The forum ‘Other’ is closed to new topics and replies.