我在Codewars上做了一个基本的Javascript Kata,如果它们不是Number
原语,那么挑战就是过滤数组中的所有项目。
我写了以下内容:
const list = [ '123', 123, 2, 'aasf', '1', 1 ];
function filter_list(l) {
const filteredArray = l.filter((item) => {
if ( Number(item) !== NaN ) return item;
});
return filteredArray;
}
console.log(filter_list(list));
我是编程新手,无法理解为什么这不起作用。你的解决方案是什么?
非常感谢
答案 0 :(得分:1)
This answer永远不等于
isNaN
本身!NaN
使用[Ref]测试值。 NaN
函数确定值是否为const list = ['123', 123, 2, 'aasf', '1', 1];
function filter_list(l) {
const filteredArray = l.filter((item) => {
return isNaN(item);
});
//could be written as l.filter(isNaN);
return filteredArray;
}
console.log(filter_list(list));
。
package mergesort;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
// 18s
public class Main {
public static final ExecutorService ex = new ThreadPoolExecutor(100, 100, 5, TimeUnit.SECONDS,
new ArrayBlockingQueue<>(10000), new ThreadPoolExecutor.CallerRunsPolicy());
public static void main(String[] args) throws InterruptedException, ExecutionException {
int n = 1_000_000;
Future<int[]> T1 = ex.submit(new Callable<int[]>() {
@Override
public int[] call() throws Exception {
// TODO Auto-generated method stub
return mergesort(generate(n));
}
});
int[] ret = T1.get();
for (int i : ret) {
System.out.println(i);
}
System.out.println("done");
ex.shutdownNow();
}
public static int[] generate(int n) {
int[] nums = new int[n];
for (int i = 0; i < n; i++) {
nums[i] = (int) Math.floor(Math.random() * n);
}
return nums;
}
public static int[] mergesort(int[] nums) throws InterruptedException, ExecutionException {
final int[] B;
if (nums.length < 2) {
return nums;
}
final int[] A = new int[nums.length / 2];
if (nums.length % 2 == 0) {
B = new int[nums.length / 2];
} else {
B = new int[nums.length / 2 + 1];
}
for (int i = 0; i < nums.length; i++) {
if (i < nums.length / 2) {
A[i] = nums[i];
} else {
B[i - nums.length / 2] = nums[i];
}
}
Future<int[]> T2 = ex.submit(new Callable<int[]>() {
@Override
public int[] call() throws Exception {
// TODO Auto-generated method stub
return mergesort(B);
}
});
Future<int[]> T1 = ex.submit(new Callable<int[]>() {
@Override
public int[] call() throws Exception {
// TODO Auto-generated method stub
return mergesort(A);
}
});
Future<int[]> T3 = ex.submit(new Callable<int[]>() {
@Override
public int[] call() throws Exception {
return merge(T1.get(), T2.get());
}
});
return T3.get();
}
public static int[] merge(int[] A, int[] B) {
int[] ret = new int[A.length + B.length];
int i = 0;
int j = 0;
int k = 0;
while (i < A.length && j < B.length) {
if (A[i] < B[j]) {
ret[k] = A[i];
i++;
} else {
ret[k] = B[j];
j++;
}
k++;
}
while (j < B.length) {
ret[k] = B[j];
j++;
k++;
}
while (i < A.length) {
ret[k] = A[i];
i++;
k++;
}
return ret;
}
}
答案 1 :(得分:0)
您可以使用const list = [ '123', 123, 2, 'aasf', '1', 1 ];
function filter_list(l) {
const filteredArray = l.filter((item) => {
if ( isNaN(Number(item)) ) return item;
});
return filteredArray;
}
console.log(filter_list(list));
isNaN
NaN
功能的必要性
与JavaScript中的所有其他可能值不同,不可能依赖等于运算符(==和===)来确定值是否为NaN == NaN
,因为NaN === NaN
都是isNaN
并且varstocases /make DX from DX1 to DX25.
freq DX.
评估为假。因此,dataset name OrigData.
dataset copy ForRestr.
dataset activate ForRestr.
varstocases .....
freq ....
dataset activate OrigData.
函数的必要性。
来源:isNaN