In the front-end field, many times, a simple function can have many different implementation methods. Today, we will take the different script implementation methods of the week as an example, hoping to inspire more ideas for children.
1. Use if statement:
var str = "";var week = new Date().getDay();if (week == 0) { str = "Today is Sunday";} else if (week == 1) { str = "Today is Monday";} else if (week == 2) { str = "Today is Tuesday";} else if (week == 3) { str = "Today is Wednesday";} else if (week == 4) { str = "Today is Thursday";} else if (week == 5) { str = "Today is Friday";} else if (week == 6) { str = "Today is Saturday";}2. Use switch case statement:
var str1 = "Today is the week";var week = new Date().getDay(); switch (week) { case 0 : str1 += "day"; break; case 1 : str1 += "one"; break; case 2 : str1 += "two"; break; case 3 : str1 += "three"; break; case 4 : str1 += "four"; break; case 5 : str1 += "five"; break; case 6 : str1 += "six"; break;}alert(str1);3. A magical artifact, one sentence to solve:
var str = "Today is the week" + "Day One, Two, Three, Four, Five, Six".charAt(new Date().getDay());alert(str);
The above different script writing methods (recommended) on the week are all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.