dayOfWeek

Easy · Conditionals · 5 test cases

Given a number from 1 to 7, return the corresponding day of the week ('Sunday' for 1, 'Monday' for 2, etc.). If the number is out of range, return 'Invalid day'.

Examples

dayOfWeek(1) → 'Sunday'
dayOfWeek(3) → 'Tuesday'
dayOfWeek(8) → 'Invalid day'

Starter code

function dayOfWeek(dayNum) {

}

Complexity of the optimal solution

Time O(1), space O(1). A switch statement (or an array lookup) provides a direct jump to the correct case, making this a constant time operation regardless of the input number.

Solve dayOfWeek in the browser