Welcome to our free JavaScript tutorial. This tutorial is based on Webucator's Introduction to JavaScript Training course.
In this exercise, you will create a function that returns the day of the week as a string.
dayAsString()
function that returns the day of the week as a string, with "0" returning "Sunday", "1" returning "Monday", etc.enterDay()
function that prompts the user for the day of the week (as a number) and then alerts the string value of that day by calling the dayAsString()
function.getCurrentDay()
function that alerts today's actual day of the week according to the user's machine.enterDay()
function.getCurrentDay()
function.---- C O D E O M I T T E D ---- <script> ---- C O D E O M I T T E D ---- function dayAsString(num){ var weekDays = []; weekDays[0] = "Sunday"; weekDays[1] = "Monday"; weekDays[2] = "Tuesday"; weekDays[3] = "Wednesday"; weekDays[4] = "Thursday"; weekDays[5] = "Friday"; weekDays[6] = "Saturday"; return weekDays[num-1]; } ---- C O D E O M I T T E D ---- function getCurrentMonth(){ var today = new Date(); alert(monthAsString(today.getMonth()+1)); } function enterDay(){ var userDay = prompt("What day of the week is it?", ""); alert("Today is " + dayAsString(userDay) + "."); } function getCurrentDay(){ var today = new Date(); alert(dayAsString(today.getDay()+1)); } </script> ---- C O D E O M I T T E D ---- <input type="button" value="CHOOSE MONTH" onclick="enterMonth();"> <input type="button" value="GET CURRENT MONTH" onclick="getCurrentMonth();"><br> <input type="button" value="CHOOSE DAY" onclick="enterDay();"> <input type="button" value="GET CURRENT DAY" onclick="getCurrentDay();"> ---- C O D E O M I T T E D ----
This tutorial is based on Webucator's Introduction to JavaScript Training Course. We also offer many other JavaScript Training courses. Sign up today to get help from a live instructor.