一行任务:检查范围 - CodeWars(使用 JavaScript 的方法)
One Line Task: Check Range - CodeWars (using JavaScript's methods)
Task
You're given an array of integers a and two integers x and y. Count the number of elements in the array such that `x ≤ a[i] ≤ y,
where i is the 0-based index of the element.
Code Limit
Less than 48 characters.
Example
For a = [2, 5, 6, 7, 1, 3, 4, 11, 56, 49], x = 1 and y = 7,
the output should be 7.
elements 2, 5, 6, 7, 1, 3, 4 should be counted.
我已经厌倦了 filter
、reduce
,并且在不超过 48 个字符的情况下想不出任何其他可能的方法。
这里是通过使用 filter
checkRange=(a,x,y)=>a.filter(i=>i>=x&&i<=y).length
使用reduce
checkRange=(a,x,y)=>a.reduce((c,i)=>i>=x&&i<=y?++c:c,0);
函数调用示例[=23=]
a =[95,92,27,55,55,20,40,8,7,45,87,14,44,35,64,84,95,85,69,47,53,49,95,54,97,7,67,31,76,97,7,24,82,61,10,34,34,85,66,96,65,2,84,4,68,74,46,50]
x = 64
y = 76
checkRange(a,x,y) // Expected: 8
到目前为止,我收到了 50 多个字符....我需要将它们减少到 47 个。
到目前为止的一些提示...
- 使用另一种较短的方法
- 计算索引值,而不是使用
length
。
- 不需要
<= or >=
,只需要==
我从 https://www.codewars.com/kata/one-line-task-check-range/discuss/javascript
那里得到了这些
这个通过了codewars的所有测试(感谢@ggorlen的提示):
a = [2, 5, 6, 7, 1, 3, 4, 11, 56, 49];
x = 1;
y = 7;
checkRange=(a,x,y)=>a.map(v=>i+=x>v==v>y,i=0)|i;
console.log(checkRange(a,x,y));
它的工作原理是使用 thisValue
参数为 Array.map
设置一个计数器 (i
),它将 a
中的所有值替换为多少个的计数值通过测试 x>v==v>y
(通过使用 i+=x>v==v>y
- 在算术上下文中,布尔值被认为是 1
(true
)或 0
(false
)).因此对于示例数组,我们得到 [1,2,3,4,5,6,7,7,7,7]
(尽管我们真正感兴趣的唯一部分是 i
的值)。测试计算 x>v
和 v>y
是否相同,只有当它们都为假时才为真,这意味着 x<=v
和 v<=y
这是我们想要的结束条件.最后,|i
尝试按位或 i
的数组,这等同于 i
,因为该上下文中的数组 == NaN
,并且 NaN
按位与一个数字进行或运算 returns 那个数字。
请注意,在 a
中有一个数字的特殊情况下,map
的输出将是 [0]
或 [1]
,这将成功为了按位 OR
,转换为 0
或 1
。在这种情况下,该数字将与 i
相同(因为 map
的输出是一个 i
值的数组),因此按位 OR
的结果将再次根据需要 i
。例如:
a = [2];
x = 1;
y = 7;
checkRange=(a,x,y)=>a.map(v=>i+=x>v==v>y,i=0)|i;
console.log(checkRange(a,x,y));
a = [49];
console.log(checkRange(a,x,y));
Task
You're given an array of integers a and two integers x and y. Count the number of elements in the array such that `x ≤ a[i] ≤ y, where i is the 0-based index of the element.
Code Limit
Less than 48 characters.
Example
For a = [2, 5, 6, 7, 1, 3, 4, 11, 56, 49], x = 1 and y = 7, the output should be 7.
elements 2, 5, 6, 7, 1, 3, 4 should be counted.
我已经厌倦了 filter
、reduce
,并且在不超过 48 个字符的情况下想不出任何其他可能的方法。
这里是通过使用 filter
checkRange=(a,x,y)=>a.filter(i=>i>=x&&i<=y).length
使用reduce
checkRange=(a,x,y)=>a.reduce((c,i)=>i>=x&&i<=y?++c:c,0);
函数调用示例[=23=]
a =[95,92,27,55,55,20,40,8,7,45,87,14,44,35,64,84,95,85,69,47,53,49,95,54,97,7,67,31,76,97,7,24,82,61,10,34,34,85,66,96,65,2,84,4,68,74,46,50]
x = 64
y = 76
checkRange(a,x,y) // Expected: 8
到目前为止,我收到了 50 多个字符....我需要将它们减少到 47 个。
到目前为止的一些提示...
- 使用另一种较短的方法
- 计算索引值,而不是使用
length
。 - 不需要
<= or >=
,只需要==
我从 https://www.codewars.com/kata/one-line-task-check-range/discuss/javascript
那里得到了这些这个通过了codewars的所有测试(感谢@ggorlen的提示):
a = [2, 5, 6, 7, 1, 3, 4, 11, 56, 49];
x = 1;
y = 7;
checkRange=(a,x,y)=>a.map(v=>i+=x>v==v>y,i=0)|i;
console.log(checkRange(a,x,y));
它的工作原理是使用 thisValue
参数为 Array.map
设置一个计数器 (i
),它将 a
中的所有值替换为多少个的计数值通过测试 x>v==v>y
(通过使用 i+=x>v==v>y
- 在算术上下文中,布尔值被认为是 1
(true
)或 0
(false
)).因此对于示例数组,我们得到 [1,2,3,4,5,6,7,7,7,7]
(尽管我们真正感兴趣的唯一部分是 i
的值)。测试计算 x>v
和 v>y
是否相同,只有当它们都为假时才为真,这意味着 x<=v
和 v<=y
这是我们想要的结束条件.最后,|i
尝试按位或 i
的数组,这等同于 i
,因为该上下文中的数组 == NaN
,并且 NaN
按位与一个数字进行或运算 returns 那个数字。
请注意,在 a
中有一个数字的特殊情况下,map
的输出将是 [0]
或 [1]
,这将成功为了按位 OR
,转换为 0
或 1
。在这种情况下,该数字将与 i
相同(因为 map
的输出是一个 i
值的数组),因此按位 OR
的结果将再次根据需要 i
。例如:
a = [2];
x = 1;
y = 7;
checkRange=(a,x,y)=>a.map(v=>i+=x>v==v>y,i=0)|i;
console.log(checkRange(a,x,y));
a = [49];
console.log(checkRange(a,x,y));