我需要创建一个函数,该函数将整数从1
带到7
。
如果数字小于1
或大于7
,则函数应返回null
。然后每个数字应该代表星期几。
所以如果我尝试类似函数返回Day(1)
的东西,它应该给我一个'Sunday'
的输出,
> Day(2)
⋖ 'Monday'
> Day(3)
⋖ 'Tuesday'
等等...
所以这是我的方法,但它没有奏效,我认为我的语法有点粗略,所以我想得到正确的方法:)
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;
}
}
正如 Pointy 所说,你的函数应该看起来像这样:
function returnDay(x){
return x < 1 || x > 7 ? null : weekDAY[x]
}
或者像这样:
function returnDay(x){
if(x < 1 || x > 7) {
return null;
}
else {
return weekDAY[x];
}
}
您正在尝试将"else"与三元运算符一起使用。可以使用三元运算符代替使用if-else块,但不能像在解决方案中使用的那样将三元运算符与else块一起使用。
此外,数组从 0 开始。因此,理想情况下,应从 0 开始分配。就像我在下面的代码片段中所做的那样。
if-else 块的工作原理:
if(condition){
// If 'condition' is true this code executes
} else {
// If 'condition' is not true, this code executes
}
三元运算符的工作原理:
(condition) ? {code when condition is true} : {code when condition is false};
因此,在您的情况下,这将是:
return (x<1) || (x>7) ? null : weekDay[x];
在您的情况下,整个解决方案将是:
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));
首先,数组是从零开始的,这意味着第一个索引从 0 开始,所以你的代码应该看起来像这样
weekDAY[0] = 'Sunday' ;
weekDAY[1] = 'Monday';
weekDAY[2]= 'Tuesday';
weekDAY[3] = 'Wednesday';
weekDAY[4] = 'Thursday';
weekDAY[5] = 'Friday';
weekDAY[6] = 'Saturday';
或者,您可以通过这样做来编写更少的行,
weekDay = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday']
您查找的功能可能如下所示
function returnDay(day) {
if (day > weekDay.length+1 || day < 1) {
return null
} else {
return weekDay[day-1]
}
}
使用switch
语句
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))
使用ternary
运算符
如果你必须使用你在例子中使用的数组,那么你可以做这样的事情:
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))
条件(三元)运算符
在您的示例中,您似乎误解了"条件(三元)运算符"。例如,让我们将解决方案中使用的语句转换为常规if/else
语句。
三元运算符:
return day < 0 || day > 7 ? null : weekDAY[day]
常规if/else
声明:
if (day < 0 || day > 7) {
return null
} else {
return weekDAY[day]
}
语法:
condition ? ifTrue : ifFalse
数组的索引位于0
您的阵列从1
开始。这实际上并不意味着您的数组在1
处编制索引。它仍将在0
处编制索引。如果我们记录weekDAY
数组,我们将看到以下内容:
[
undefined,
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
]
如您所见,我们将0
索引设置为undefined
.现在这对我们来说不是问题(因为我们不允许在dayOfTheWeek
函数中使用低于1
的整数),但是如果我们决定遍历这个数组,我们将循环undefined
.这可能会在以后的代码中产生错误。
若要避免此问题,应尝试以这种方式声明数组:
weekDAY = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday']
右大括号正在关闭与 ? 运算符一起使用的函数。
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];
}
// 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
使用条件:
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));
我正在 Udemy 上学习并完成了这样的练习:
function returnDay (num) {
let daysOfWeek = ['not a day','Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
return num < 1 || num > 7 ? null : daysOfWeek[num];
}
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];
}
}