Grow your CSS skills. Land your dream job.

Beginner JavaScript help

  • # December 4, 2008 at 11:39 am

    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

    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!

    # December 5, 2008 at 10:38 am

    Thanks for the help. I ended up doing it like this:

    JavaScript:

    Code:
    var d = new Date();
    theDay = d.getDay();
    theMonth = d.getMonth();
    theDate = d.getDate();

    var dayNames = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
    var monthNames = ["January","February","March","April","May","June","July","August","September","October","November","December"];

    theDay = dayNames[theDay];
    theMonth = monthNames[theMonth];

    (I threw in the month and day too, just for the heck of it)

    Then in the HTML, I wrote the current date with this:

    Code:

    And finally, a switch function to tell me which side based on the day:

    Code:

    There’s probably a more efficient way to do it, but I was just proud of myself that I got it to work at all! :lol:

    # December 5, 2008 at 11:12 am

    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?

    # December 5, 2008 at 11:16 am

    Ah, cool. That’s what I really wanted to do – say "near" for one entire set of days and "opposite" for the other (rather than having to repeat everything for every day).

    By the way, no need to wait till tomorrow – just change your system clock and refresh the page ;-)

    # December 8, 2008 at 11:39 am

    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 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".