[英]Why am I getting undefined instead of null?
I wrote a function that returns a day of the week given a number (1 = Monday, 2 = Tuesday etc) and I was supposed to create a caveat where if the number was less than 1 or greater than 7, the function should return null.我写了一个 function 返回给定数字的一周中的一天(1 = 星期一,2 = 星期二等),我应该创建一个警告,如果数字小于 1 或大于 7,则 function 应返回 Z307A625ECC60 . Why is it instead returning undefined for 0 and 8?为什么它会为 0 和 8 返回 undefined?
daysOfWeek = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'] function returnDay(day) { day = daysOfWeek[day - 1]; if (day > 7 || day < 1) { return null } else { return day } } console.log(returnDay(8)) console.log(returnDay(0))
You are overwriting your argument, which is considered bad practice.您正在覆盖您的论点,这被认为是不好的做法。
When you access daysOfWeek[0 -1]
=> daysOfWeek[-1]
=> undefined
, you then evaluate is undefined < 1
which is falsy
.当您访问daysOfWeek[0 -1]
=> daysOfWeek[-1]
=> undefined
时,您的评估结果是undefined < 1
这是falsy
。 So you enter the else, which returns day
=> undefined
所以你输入 else,它返回day
=> undefined
The below code works下面的代码有效
daysOfWeek = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
function returnDay(day) {
if (day <= 7 && day >= 1) {
return daysOfWeek[day - 1]
}
return null
}
console.log(returnDay(8))
console.log(returnDay(0))
Or, even more dense或者,更密集
daysOfWeek = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
function returnDay = d => daysOfWeek[d - 1] || null
console.log(returnDay(8))
console.log(returnDay(0))
Basically, you have two range checks in your code right now, and that's the biggest issue.基本上,您的代码中现在有两个范围检查,这是最大的问题。
Here's the first check:这是第一个检查:
day = daysOfWeek[day - 1];
... which ends with undefined
unless day
has valid value (so that there's an element in daysOfWeek
that corresponds to that value). ... 以undefined
结尾,除非day
具有有效值(因此daysOfWeek
中有一个元素对应于该值)。
And here's the second check:这是第二次检查:
if (day > 7 || day < 1) { ... }
... which ends up doing wrong things because day
variable gets a different value in that line of the first check. ...最终会做错事,因为day
变量在第一次检查的那一行中获得了不同的值。 You can fix this, of course, by introducing another name...当然,您可以通过引入另一个名称来解决此问题...
const dayTitle = daysOfWeek[day - 1];
if (day > 7 || day < 1) { ... }
... but that means you're still checking the same condition twice . ...但这意味着您仍在检查相同的条件两次。 And it's never a good idea to repeat yourself.重复自己永远不是一个好主意。
So here's how it can be done:所以这是如何做到的:
function returnDay(day) {
return daysOfWeek[day - 1] || null;
}
... yep, that simple: the boundary range check now is done by JS, not you and your code. ...是的,就这么简单:边界范围检查现在由 JS 完成,而不是您和您的代码。 In other words, if day
value cannot be used as a proper index for that dayOfWeek
array, you'll just get undefined
(which will be coalesced by || null
to, well, null
).换句话说,如果day
值不能用作该dayOfWeek
数组的正确索引,那么您只会得到undefined
(它将由|| null
合并到null
)。
And yes, having null
as kind of default value in this case seems weird to me.是的,在这种情况下,将null
作为默认值对我来说似乎很奇怪。
So I changed the arguement so that day is no longer being overwritten and it looks like this:所以我改变了论点,所以那天不再被覆盖,它看起来像这样:
function returnDay(n){
day = daysOfWeek[n-1];
if (n > 7 || n < 1){
return null
} else {
return day
}
}
console.log(returnDay(8))
console.log(returnDay(0))
I now successfully get null for returnDay(0) and returnDay(8)我现在成功获得了 returnDay(0) 和 returnDay(8) 的 null
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.