我正在freecodecamp上进行算法挑战,他们提供的解决方案非常整洁:
ucrtd.lib
不幸的是,由于是菜鸟,并且没有考虑该高阶函数,所以我创建了这个冗长的怪物:
/OUT:"C:\Projects\BuildTest\x64\Debug\Build.dll" /MANIFEST /NXCOMPAT /PDB:"C:\Projects\BuildTest\x64\Debug\Build.pdb" /DYNAMICBASE "kernel32.lib" "user32.lib" "gdi32.lib" "winspool.lib" "comdlg32.lib" "advapi32.lib" "shell32.lib" "ole32.lib" "oleaut32.lib" "uuid.lib" "odbc32.lib" "odbccp32.lib" /DEBUG /DLL /MACHINE:X64 /INCREMENTAL /PGD:"C:\Projects\BuildTest\x64\Debug\Build.pgd" /SUBSYSTEM:CONSOLE /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /ManifestFile:"x64\Debug\Build.dll.intermediate.manifest" /ERRORREPORT:PROMPT /NOLOGO /LIBPATH:"C:\Projects\cimgui_build\Debug" /LIBPATH:"C:\Projects\SDL2-2.0.8\build\Debug" /TLBID:1
/OUT:"C:\Projects\BuildTest\x64\Debug\Build.exe" /MANIFEST /NXCOMPAT /PDB:"C:\Projects\BuildTest\x64\Debug\Build.pdb" /DYNAMICBASE "kernel32.lib" "user32.lib" "gdi32.lib" "winspool.lib" "comdlg32.lib" "advapi32.lib" "shell32.lib" "ole32.lib" "oleaut32.lib" "uuid.lib" "odbc32.lib" "odbccp32.lib" /DEBUG /MACHINE:X64 /INCREMENTAL /PGD:"C:\Projects\BuildTest\x64\Debug\Build.pgd" /SUBSYSTEM:CONSOLE /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /ManifestFile:"x64\Debug\Build.exe.intermediate.manifest" /ERRORREPORT:PROMPT /NOLOGO /LIBPATH:"C:\Projects\cimgui_build\Debug" /LIBPATH:"C:\Projects\SDL2-2.0.8\build\Debug" /TLBID:1
在以前的测试中似乎可以完成工作。但是,NaN并不配合。
function bouncer(arr) {
return arr.filter(Boolean);
}
返回:
function bouncer(arr) {
let truthyArray = [];
let falsyArray = [];
for (let i = 0; i < arr.length; i++) {
if (arr[i] == false || arr[i] === null || arr[i] === NaN || arr[i] === undefined) {
falsyArray.push(arr[i]);
} else {
truthyArray.push(arr[i]);
}
}
return truthyArray;
}
是否也有关于如何测试NaN的想法?
答案 0 :(得分:4)
问题是NaN !== NaN
:
console.log(NaN === NaN);
(这是有道理的,因为NaN
只是一个占位符,用于表示 一个数字,但无法计算出数字值,例如{{1} }(0 / 0
)NaN
(!== ('foo' * 3)
))
相反,您可以使用NaN
,它将检查传递的值是否为Number.isNaN
和Number
。
NaN
请确保使用console.log(Number.isNaN(NaN));
function bouncer(arr) {
let truthyArray = [];
let falsyArray = [];
for (let i = 0; i < arr.length; i++) {
if (arr[i] == false || arr[i] === null || Number.isNaN(arr[i]) || arr[i] === undefined) {
falsyArray.push(arr[i]);
} else {
truthyArray.push(arr[i]);
}
}
return truthyArray;
}
console.log(bouncer([false, null, 0, NaN, undefined, ""]));
而不是简单的Number.isNaN
,这会不必要地排除真实的非数字。