用于布尔返回的 Javascript 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10286393/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript function for boolean returns
提问by Matt Zelenak
I'm having some issues getting a proper boolean return on section 4.1 of Codecademy's Javascript tutorial. Here is the code:
我在 Codecademy 的 Javascript 教程的第 4.1 节中获得正确的布尔值返回时遇到了一些问题。这是代码:
// Define quarter here.
var quarter = function(n) {
if (n / 4 ){
return true;
} else {
return false;
}
};
if (quarter(4) === 1) {
console.log("The statement is true.");
} else {
console.log("The statement is false.");
}
From what I can see, I am passing the newly defined quarter variable a function with a parameter of 'n' that I then divide by 4 to see if it returns 1 for true, or 0 (else) for false. I then am using the 'quarter' function in an if loop to check for equality of 1 of the number '4' passed as 'n'.
从我所见,我将新定义的四分之一变量传递给一个参数为“n”的函数,然后除以 4 以查看它是否返回 1 表示真,或 0(否则)表示假。然后,我在 if 循环中使用“四分之一”函数来检查作为“n”传递的数字“4”中的 1 是否相等。
I'm assuming this is some basic logic that I am just not used to using (as a front end developer looking to get into JavaScript programming) but I would definitely appreciate some thoughts and guidance.
我假设这是一些我不习惯使用的基本逻辑(作为一名希望进入 JavaScript 编程的前端开发人员),但我肯定会感谢一些想法和指导。
回答by Pointy
In JavaScript, the constants true
and false
are notnumbers; they're a separate type.
在JavaScript中,常量true
和false
是不是数字; 它们是单独的类型。
Furthermore, you're comparing with ===
and that will explicitly prevent type conversion during the comparison.
此外,您正在与 进行比较,===
这将在比较期间明确阻止类型转换。
Note that n / 4
is going to be true
(non-zero) for all values of "n" except 0 (edityou probably meant to use %
). And in general, any construction of the form:
请注意,对于除 0 之外的所有“n”值(编辑您可能打算使用),n / 4
这将是true
(非零)。一般来说,任何形式的构造:%
if (expression) {
return true;
}
else {
return false;
}
can be replaced by:
可以替换为:
return !!(expression);
or, alternatively,
或者,或者,
return Boolean(expression);
回答by Developer
Change your if statement to be consistent, since 1 != true. The triple equals will not allow it.
将 if 语句更改为一致,因为 1 != true。三重等于不允许。
if (quarter(4) == true) {
Your function is also incorrect, I think you want something more like this, to return if it's divisible by 4:
你的函数也不正确,我认为你想要更像这样的东西,如果它可以被 4 整除就返回:
var quarter = function(n) {
if (n % 4 == 0){
return true;
} else {
return false;
}
};
This can be shortened to this:
这可以缩短为:
var quarter = function(n) {
return n % 4 == 0;
}
回答by Dave Newton
Using ===
means you're asking for a "strict equals"--not truthy or falsey. This means you can't check for a number, because a number is not strictly true
. See this SO question for more details.
Using===
意味着您要求“严格等于” - 不是真的或假的。这意味着您无法检查数字,因为严格来说数字不是true
. 有关更多详细信息,请参阅此 SO 问题。
The calling code should either check for === true
, or just skip the explicit value compare, and just be if (quarter(4)) { ...
.
调用代码应该检查=== true
,或者只是跳过显式值比较,而只是if (quarter(4)) { ...
。
回答by Gabriel_Student_Innovated
Couldn't find an answer on here that worked, so I assume I just missed it. I also find that Googling solutions make things worse for me.
在这里找不到有效的答案,所以我想我只是错过了。我还发现谷歌搜索解决方案让我的情况更糟。
I tend to find that I overthink the solutions in Javascript, and this obviously leads to an inability to solve the solutions.
我倾向于发现我对 Javascript 中的解决方案考虑过多,这显然导致无法解决解决方案。
You also don't necessarily need the return true
, but I added it anyways.
您也不一定需要 return true
,但我还是添加了它。
Just remember that logically ordered simplicity is key.
请记住,逻辑有序的简单性是关键。
var quarter = function(number) { var number = n / 4; var n = 48; if (quarter(number) % 3 === 0 ) { console.log("The statement is true"); return true; } else { console.log("The statement is false"); } }
I hope that works for you.
我希望这对你有用。
I'll use my lack of JavaScript skill and knowledge to explain this to beginners like myself, and you. And anybody who has an extreme knowledge of JavaScript will probably disagree, but it's also why most programming employers will take a Communication major over a programmer anyday:
我将利用我缺乏 JavaScript 技能和知识向像我这样的初学者和你解释这一点。任何对 JavaScript 有深入了解的人都可能不同意,但这也是为什么大多数编程雇主每天都会选择通信专业而不是程序员的原因:
- Define a function named quarter, meaning you are defining a function--quarter -- but you must first define what quarter-- var.
var quarter = function(number)
- The function returns a value that equals to
The function is being defined asn / 4
The value is number.quarter(number)
You then introduce a new var that defines the value withnumber = n / 4
To return true and begin your if statement,
is now quarter, the function you're defining.function(number or n)
The basis of providing a correct equation relies on it returning true in the form of
You do this by then defining nwith a number that will be divided 4, and then that number being able to be divided by 3 with no remainder.Since numberconsole.log("The statement is true!")
has been defined asn / 4
, With nbeing defined as 48in this example, both:quarter(number)
andquarter(n)
will work for the if / else statement.
To bring a remainder of 0 after dividing by 3, use% 3 === 0
.- So
if (quarter(number) % === 3 {
is now the defined function quarter.number
is a parameter, and you assign the value of the parameter with variables,var number = n / 4
andvar n = 48
.
- So
- 定义一个函数命名的四分之一,这意味着你要定义function--季度-但你必须首先定义什么quarter--变种。
var quarter = function(number)
- 该函数返回一个等于
该函数被定义为n / 4
值为number。quarter(number)
然后引入一个新的 var 来定义值number = n / 4
要返回 true 并开始您的 if 语句,
现在是季度,您正在定义的功能。function(number or n)
提供正确方程的基础依赖于它以以下形式返回真
您可以通过将n定义为一个将被除以4 的数字,然后该数字能够被 3 除以没有余数。console.log("The statement is true!")
因为数字已被定义为n / 4
,在本例中n被定义为48,两者:quarter(number)
和quarter(n)
将适用于if / else语句。
要在除以 3 后得到 0 的余数,请使用% 3 === 0
.- 所以
if (quarter(number) % === 3 {
现在是定义的函数季度。number
是一个参数,你用变量分配参数的值,var number = n / 4
和var n = 48
.
- 所以
On that note, as I'm typing this, this would, in theory, also work and being shorter:
在这一点上,当我输入这个时,理论上,这也可以工作并且更短:
var quarter = function(number) { if (quarter(48) % 3 === 0) { console.log("This statement is true!"); return number = number / 4; } else { console.log("This statement is false."); }; };
回答by Aaron Erebus
Okay, this stupid thing is super finicky. I'll demystify it right now. Your code should look like this:
好吧,这个愚蠢的东西是超级挑剔的。我马上揭开它的神秘面纱。您的代码应如下所示:
var quarter = function (number) {
return number/4;
}
if (quarter(48) % 3 === 0 ) {
console.log("The statement is hella true");
} else {
console.log("The statement is hella false brah");
}
The reason you don't pass the module is because the stupid number in the quarter(n) bracket has to return with a true statement. Pick a number, any number and multiply that by (3) then by (4); which will in turn give you a value that makes the modulo % zero and making your if/else statement true.
你不通过模块的原因是因为四分之一(n)括号中的愚蠢数字必须返回一个真实的语句。选择一个数字,任何数字并将其乘以(3)然后乘以(4);这将反过来为您提供一个值,使模 % 为零并使您的 if/else 语句为真。
My contribution to the world, -Aaron
我对世界的贡献,-Aaron