简体   繁体   English

在以下Javascript数组方案中,parseInt()和Number.isInteger()之间的功效差异?

[英]Difference in efficacy between parseInt() and Number.isInteger() in following Javascript array scenario?

I am working through freecodecamp.org's Javascript ES6 coding problems and one of them tasked me with using arrow function notation to: 我正在研究freecodecamp.org的Javascript ES6编码问题,其中一个任务要求我使用箭头功能符号来:

  1. Take an array of real numbers. 取一个实数数组。
  2. Filter only the positive integers into a new array, and 仅将正整数过滤到新数组中,然后
  3. Square those positive integers. 平方那些正整数。

I have successfully completed the problem but built my code for Step 2 by filtering the original array with Numbers.isInteger(). 我已经成功完成了该问题,但是通过使用Numbers.isInteger()过滤原始数组来构建了步骤2的代码。 Freecodecamp.org's provided answer utilizes parseInt(). Freecodecamp.org提供的答案利用parseInt()。

I do not see why we would need to parse integers if they are already integers, nor why parseInt() does not throw an error since its parameter asks for a string. 我看不到为什么如果整数已经是整数就需要解析整数,也为什么parseInt()不会因为参数要求输入字符串而引发错误。

My primary question: Are both equally acceptable? 我的主要问题:两者是否都可以接受? Is one going to get me into more trouble down the road? 有人会让我在途中遇到更多麻烦吗?

The only closely relevant stackoverfow I found was here (which was vaguely helpful). 我发现的唯一紧密相关的stackoverfow在这里 (对您有所帮助)。 Below is my code followed by the answer code provided by freecodecamp.org. 以下是我的代码,然后是freecodecamp.org提供的答案代码。 NOTE: I am aware my code has a few extra steps in it. 注意:我知道我的代码中还有一些额外的步骤。 I am not a huge fan of arrow notation and am still improving my code-organization! 我不是箭头表示法的忠实拥护者,并且仍在改善我的代码组织!


MY CODE:: 我的代码::

const realNumberArray = [4, 5.6, -9.8, 3.14, 42, 6, 8.34, -2];

const squareList = (arr) => {

"use strict";

// dictates what numbers are filter()'d out of original array

const checkElement = (value) => value > 0 && Number.isInteger(value) == true;

const integersOnly = arr.filter(checkElement); //filters ONLY positive integers into new array

PROVIDED ANSWER CODE:: 提供的答案代码::

const realNumberArray = [4, 5.6, -9.8, 3.14, 42, 6, 8.34, -2];

const squareList = (arr) => {

"use strict";

const squaredIntegers = arr.filter( (num) => num > 0 && num % parseInt(num) === 0 ).map( (num) => Math.pow(num, 2) );

The docs are a little bit misleading - the first parameter to parseInt doesn't have to be a string. 该文档是有点误导-第一个参数parseInt 不必是一个字符串。 Looking at the specification , the first argument gets cast to a string as the first step: 看一下规范 ,第一步将第一个参数转换为字符串:

When the parseInt function is called, the following steps are taken: 调用parseInt函数时,将执行以下步骤:

  1. Let inputString be ToString(string). 令inputString为ToString(string)。

One of the problems with parseInt is that, if it has a leading zero, it may be interpreted as octal in some (old) browsers, and as base 10 in other (newer) browsers. parseInt的问题之一是,如果它具有前导零,则在某些(旧)浏览器中可能被解释为八进制 ,而在其他(较新)浏览器中则可能被解释为以10为底。 For this reason, if you want to support said old browsers, it's best to either always provide a radix (as the second parameter) or use Number instead. 因此,如果要支持上述旧浏览器,最好总是提供一个基数(作为第二个参数)或改用Number

That said, in this particular situation, it makes no difference, because calling parseInt on a number will never result in a leading zero. 就是说,在这种特殊情况下,这没有什么区别,因为对数字调用parseInt永远不会导致前导零。

Still, I think the recommended answer is a bit misleading - since you know that the numbers used in the test will always be positive, it would be a bit more appropriate to use Math.floor than parseInt for this purpose, since you're just trying to get the floored value, not trying to turn a string into a number. 不过,我认为建议的答案还是有误导性的-因为您知道测试中使用的数字始终为正,因此使用Math.floor不是parseInt会更适合于此目的,因为您只是尝试获取下限值,而不是尝试将字符串转换为数字。

parseInt tries to convert the value you pass to it into an integer, if you call it with this parameter parseInt("30") it will return to you the value 30 as a number. parseInt尝试将您传递给它的值转换为整数,如果使用此参数parseInt("30")调用,它将返回给您一个数字30。 If you call it with a value that can't be converted to a integer like parseInt("hello world") it will return NaN. 如果使用无法转换为parseInt("hello world")之类的整数的值进行调用,它将返回NaN。

Number.isInteger validates if the given value is an integer returning a true or false boolean value. Number.isInteger验证给定值是否为返回真布尔值或假布尔值的整数。 Number.isInteger(1) will return true, Number.isInteger("hello world") or Number.isInteger(3.14) will return false. Number.isInteger(1)将返回true, Number.isInteger("hello world")Number.isInteger(3.14)将返回false。

I do not see why we would need to parse integers if they are already integers. 我不明白为什么如果整数已经是整数,我们为什么需要解析整数。

You don't need to. 不用了 This is abuse of parseInt . 这是对parseInt滥用。 They should have used Math.floor instead for their intented purpose. 他们应该将Math.floor用作其预期目的。

Why parseInt() does not throw an error since its parameter asks for a string? 为什么parseInt()不会因为参数要求输入字符串而引发错误?

Because it's an old API, and it's very lenient. 因为它是一个旧的API,而且非常宽松。 Instead of throwing errors, it simply coerces its argument to a string, then tries to parse that. 它不会抛出错误,而只是将其参数强制转换为字符串,然后尝试解析该字符串。

My primary question: Are both equally acceptable? 我的主要问题:两者是否都可以接受?

No, parseInt is absolutely inacceptable. 不, parseInt是绝对不能接受的。 You found a much better solution with isInteger . 您使用isInteger找到了更好的解决方案。 The reason why they didn't use it probably is that isInteger is a relatively new function, added with ES6. 他们不使用它的原因可能是isInteger是ES6中新增的一个相对较新的函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM