Returning the Day of the Week as a String - Exercise

Contact Us or call 1-877-932-8228
Returning the Day of the Week as a String - Exercise

Returning the Day of the Week as a String

Duration: 15 to 25 minutes.

In this exercise, you will create a function that returns the day of the week as a string.

  1. Open BuiltInObjects/Exercises/DateUDFs.html for editing.
  2. Write a dayAsString() function that returns the day of the week as a string.
  3. Write an enterDay() function that prompts the user for the day of the week and then alerts the string value of that day by calling the dayAsString() function.
  4. Write a getCurrentDay() function that alerts today's actual day of the week according to the user's machine.
  5. Add a "CHOOSE DAY" button that calls the enterDay() function.
  6. Add a "GET CURRENT DAY" button that calls the getCurrentDay() function.
  7. Test your solution in a browser.

Solution:

BuiltInObjects/Solutions/DateUDFs.html
---- C O D E   O M I T T E D ----

<script type="text/javascript">
---- C O D E   O M I T T E D ----
	function dayAsString(num){
		var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
		return days[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 ----
Next