'JavaScript Function Exercise Day Of The Week
I need to make a function that takes an integer from 1 to 7.
If the number is less than 1 or greater than 7, the function should return null. Then each number should represent the day of the week.
So if I try something like function return Day(1), it should give me an output of 'Sunday',
> Day(2)
⋖ 'Monday'
> Day(3)
⋖ 'Tuesday'
And so on...
So this is my approach, yet it didn't work and I think my syntax is a little bit sketchy, so I would like to get the correct approach :)
const weekDAY = new Array(7);
weekDAY[1] = 'Sunday' ;
weekDAY[2] = 'Monday';
weekDAY[3] = 'Tuesday';
weekDAY[4] = 'Wednesday';
weekDAY[5] = 'Thursday';
weekDAY[6] = 'Friday';
weekDAY[7] = 'Saturday';
function returnDay(x) {
return (x < 1) || (x > 7) ? null :
} else {
let thisDay = weekDAY[x]
return thisDay;
}
}
Solution 1:[1]
You are trying to use an 'else' with the ternary operator. A ternary operator can be used instead of using an if-else block, but you cannot use a ternary operator with an else block like you have used in your solution.
Also, Arrays are 0 based. Therefore, you should ideally start allocation from 0. Like I've done in the below code snippet.
How an if-else block works:
if(condition){
// If 'condition' is true this code executes
} else {
// If 'condition' is not true, this code executes
}
How the ternary operator works:
(condition) ? {code when condition is true} : {code when condition is false};
So in your case that would be:
return (x<1) || (x>7) ? null : weekDay[x];
The entire solution in your case would be:
const 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';
function returnDay(x){
return (x < 1) || (x > 7) ? null : weekDAY[x];
}
//Calling the function for result and printing it to the console
//using console.log()
console.log(returnDay(2));
Solution 2:[2]
As Pointy says, your function should look like this:
function returnDay(x){
return x < 1 || x > 7 ? null : weekDAY[x]
}
Or like this:
function returnDay(x){
if(x < 1 || x > 7) {
return null;
}
else {
return weekDAY[x];
}
}
Solution 3:[3]
Firstly, Array's are zero-based, meaning the first index starts from 0 so your code should look like this
weekDAY[0] = 'Sunday' ;
weekDAY[1] = 'Monday';
weekDAY[2]= 'Tuesday';
weekDAY[3] = 'Wednesday';
weekDAY[4] = 'Thursday';
weekDAY[5] = 'Friday';
weekDAY[6] = 'Saturday';
Alternatively you can write this a fewer lines by doing this,
weekDay = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday']
The Function your looking for could look something like this
function returnDay(day) {
if (day > weekDay.length+1 || day < 1) {
return null
} else {
return weekDay[day-1]
}
}
Solution 4:[4]
Using a switch statement
const dayOfTheWeek = day => {
if (day < 1 || day > 7) return null
switch (day) {
case 1:
return 'Sunday'
break
case 2:
return 'Monday'
break
case 3:
return 'Tuesday'
break
case 4:
return 'Wednesday'
break
case 5:
return 'Thursday'
break
case 6:
return 'Friday'
break
case 7:
return 'Saturday'
}
}
console.log(dayOfTheWeek(7))
Using a ternary operator
If you have to use the array you gave to use in your example then you can do something like this:
const weekDAY= new Array(7)
weekDAY[1] = 'Sunday'
weekDAY[2] = 'Monday'
weekDAY[3] = 'Tuesday'
weekDAY[4] = 'Wednesday'
weekDAY[5] = 'Thursday'
weekDAY[6] = 'Friday'
weekDAY[7] = 'Saturday'
const dayOfTheWeek = day => {
return day < 0 || day > 7 ? null : weekDAY[day]
}
console.log(dayOfTheWeek(3))
Conditional (ternary) operator
In your example, you seem to have misunderstood the "conditional (ternary) operator". For example, let’s convert the one we used in my solution to a regular if/else statement.
Ternary operator:
return day < 0 || day > 7 ? null : weekDAY[day]
Regular if/else statement:
if (day < 0 || day > 7) {
return null
} else {
return weekDAY[day]
}
Syntax:
condition ? ifTrue : ifFalse
Arrays are indexed at 0
Your array starts at 1. This actually doesn't mean your array is indexed at 1. It will still be indexed at 0. If we log the weekDAY array, we will see this:
[
undefined,
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
]
As you can see, we have the 0 index set to undefined. This right now isn't an issue for us (because we do not allow integers below 1) in our dayOfTheWeek function, but if we decided to loop over this array, we will it loop over undefined. This could create an error in our code later on.
To avoid this problem, you should try and declare arrays this way:
weekDAY = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday']
Solution 5:[5]
The closing brace is closing the function which you used with the ? operator.
const weekDAY = new Array(7);
weekDAY[1] = 'Sunday' ;
weekDAY[2] = 'Monday';
weekDAY[3] = 'Tuesday';
weekDAY[4] = 'Wednesday';
weekDAY[5] = 'Thursday';
weekDAY[6] = 'Friday';
weekDAY[7] = 'Saturday';
function returnDay(x) {
if(x < 1 || x > 7) {
return null;
}
else {
return weekDAY[x];
}
Solution 6:[6]
// create object to store days of the week
const days = {
1: "Monday",
2: "Tuesday",
3: "Wednesday",
4: "Thursday",
5: "Friday",
6: "Saturday",
7: "Sunday",
};
// create function
function returnDay(num) {
return num < 1 || num > 7 ? null : days[num];
}
// call function
console.log(returnDay(3)); // Wednesday
console.log(returnDay(8)); // null
console.log(returnDay(0)); // null
Solution 7:[7]
Using a conditional:
const weekDAY = new Array();
weekDAY[1] = 'Monday' ;
weekDAY[2] = 'Tuesday';
weekDAY[3] = 'Wednesday';
weekDAY[4] = 'Thursday';
weekDAY[5] = 'Friday';
weekDAY[6] = 'Saturday';
weekDAY[7] = 'Sunday';
function returnDay(x){
if(x < 1 || x > 7) {
return null;
}
else {
return weekDAY[x];
}
}
console.log(returnDay(1));
console.log(returnDay(7));
console.log(returnDay(4));
console.log(returnDay(0));
Solution 8:[8]
I'm learning on Udemy and have completed the exercise like this:
function returnDay (num) {
let daysOfWeek = ['not a day','Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
return num < 1 || num > 7 ? null : daysOfWeek[num];
}
Solution 9:[9]
const days = {
1:"Monday",
2:"Tuesday",
3:"Wednesday",
4:"Thursday",
5:"Friday",
6:"Saturday",
7:"Sunday"
}
const dayNum = Object.values(days);
function returnDay(a) {
if(a < 1 || a > 7) {
return null;
} else {
return dayNum[a - 1];
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
