Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Other Beginner JavaScript help

  • This topic is empty.
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #23697
    daGUY
    Member

    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:)

    #51817
    Toby Pitman
    Participant

    I’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!

    #51939
    Toby Pitman
    Participant

    Nice! 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?

    #52005
    James
    Member

    I 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());

Viewing 4 posts - 1 through 4 (of 4 total)
  • The forum ‘Other’ is closed to new topics and replies.