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:)
Thanks for the help. I ended up doing it like this:
JavaScript:
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\"];
And finally, a switch function to tell me which side based on the day:
<script type=\"text/javascript\"> switch (theDay) { case \"Sunday\": document.write(\"near\"); break; case \"Monday\": document.write(\"opposite\"); break; case \"Tuesday\": document.write(\"near\"); break; case \"Wednesday\": document.write(\"opposite\"); break; case \"Thursday\": document.write(\"near\"); break; case \"Friday\": document.write(\"opposite\"); break; case \"Saturday\": document.write(\"near\"); break; } </script>
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:
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 ;-)
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.'; }
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:)
;)
or....use the date() method and some 'if' statements. This should get you started.
.....or sell your car!
JavaScript:
(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:
And finally, a switch function to tell me which side based on the day:
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:
Just tried this and I think it should work...got to wait till tomorrow to check though!!! :oops:
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?
By the way, no need to wait till tomorrow - just change your system clock and refresh the page ;-)
Here: