displaying days of the week in java script

I need to - Create a HTML form with a text entry field and a button.. When a number is entered in the text entry field and the button clicked a Javascript function called DayOfTheWeek() is invoked. This function uses a switch statement to determine the day of the week corresponding to the number entered, i.e if the number entered is 1 the message “It’s Monday” is displayed, if the number entered is 2 the message “It’s Tuesday” is displayed and so on. If a number which is not between 1-7 is entered then the message “Not a valid day of the week” is displayed.

my html

<input type="text" name="text1"/>

<input type="button" value="Click me" onclick="days(text1.value);"/>

my java script

function days(dayOfTheWeek)
{
switch (dayOfTheWeek) {

case “1”: 
    alert(“It\’s Monday”);
        break;
case “2”:
        alert(“It\’s Tuesday”);
    break;

case “3”:
        alert(“It\’s Wednesday”);
    break;

case “4”:
        alert(“It\’s Thursday”);
    break;

case “5”:
        alert(“It\’s Friday”);
    break;

case “6”:
        alert(“It\’s Saturday”);
    break;

case “7”:
        alert(“It\’s Sunday”);
    break;

default:
        alert(“Not a valid day”);
    break;  

}
}

PLEASE HELP I keep getting an error with firebug saying days is not defind

Answers:

Answer
function days(dayOfTheWeek) {
  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";

  var n = weekday[dayOfTheWeek];
  return n;
}
Answer

Your updated code

<input type="text" name="text1" id="text1" />
<input type="button" value="Click me" onclick="javascript:days(parseInt(document.getElementById('text1').value));" />
<script language=javascript>
function days(dayOfTheWeek)
{
switch (dayOfTheWeek) {

case 1: 
    alert("It\’s Monday");
        break;
case 2:
        alert("It\’s Tuesday");
    break;

case 3:
        alert("It\’s Wednesday");
    break;

case 4:
        alert("It\’s Thursday");
    break;

case 5:
        alert("It\’s Friday");
    break;

case 6:
        alert("It\’s Saturday");
    break;

case 7:
        alert("It\’s Sunday");
    break;

default:
        alert("Not a valid day");
    break;  

}
}
</script>

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.