Knowee
Questions
Features
Study Tools

Write a function 'returnDay' that takes one parameter(number from 1 to 7) and returns the day of the week.(where 1 is Monday, 2 is Tuesday, 3 is Wednesday, etc).If the number is less than 1 or greater than 7, the function should return null.Note: Store the days of the week in the array.

Question

Write a function 'returnDay' that takes one parameter(number from 1 to 7) and returns the day of the week.(where 1 is Monday, 2 is Tuesday, 3 is Wednesday, etc).If the number is less than 1 or greater than 7, the function should return null.Note: Store the days of the week in the array.

🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a JavaScript function that fulfills your requirements:

function returnDay(num) {
    let days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
    if (num < 1 || num > 7) {
        return null;
    } else {
        return days[num - 1];
    }
}

This function works as follows:

  1. It first defines an array days that contains the days of the week, starting from Monday and ending on Sunday.

  2. Then it checks if the input number num is less than 1 or greater than 7. If it is, the function returns null.

  3. If the number is between 1 and 7 (inclusive), the function returns the corresponding day of the week. Note that we subtract 1 from num because array indices in JavaScript start from 0, not 1. So, for example, days[0] is 'Monday', days[1] is 'Tuesday', and so on.

This problem has been solved

Similar Questions

Create a short program that uses list_of_days to store a list of the seven days of the week. Ask the user to choose a day of the week by picking a number from 1-7. Print the day in the index position with that number. Use conditionals to display which days are weekdays and which are weekend days.

There are 7 days in a week.Let x represent the number of weeks and y represent the corresponding number of days.Use the equation y=7x to find the value of y when x=1.y=

Given day of the week encoded as 0 = Sun, 1 = Mon, 2 = Tue, ... 6 = Sat, and a boolean vacation indicating if we are on vacation. Write the function alarm_clock(day, vacation) that returns a string of the form "7:00" indicating when the alarm clock should ring. On weekdays (Mon-Fri), the alarm should be "7:00" and on the weekend (Sat and Sun) it should be "10:00". However, if we are on vacation, then on weekdays it should be "10:00" and weekends it should be "off".Examplesalarm_clock(3, False) -> "7:00"alarm_clock(5, True) -> "10:00"alarm_clock(6, True) -> "off"

Which of the following functions returns the day of the week from a given date in Excel? Answer( Please choose a correct answer )   CLEARDAY()WEEKDAY()MONTH()YEAR()

Which MS Excel function (or combination of functions) can be used to determine the number of weekdays between two specific dates?

1/2

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.