function flipPairs(input){
var text = '';
for(var i = 0; i < input.length; i = i + 2){
text += input[i + 1] + input[i];
}
return text;
}
console.log(flipPairs('HI'));
我在运行该文件时将其保存在一个名为flipper.js的文件中
node flipper.js
我得到这个错误
node flipper.js
<--- Last few GCs --->
[20506:0x102aac000] 29029 ms: Scavenge 2024.1 (2031.1) -> 2023.8 (2042.3) MB, 9.6 / 0.0 ms (average mu = 0.272, current mu = 0.259) allocation failure
[20506:0x102aac000] 29049 ms: Scavenge 2031.3 (2042.3) -> 2032.2 (2043.1) MB, 16.5 / 0.0 ms (average mu = 0.272, current mu = 0.259) allocation failure
[20506:0x102aac000] 29069 ms: Scavenge 2032.2 (2043.1) -> 2031.2 (2066.1) MB, 19.8 / 0.0 ms (average mu = 0.272, current mu = 0.259) allocation failure
<--- JS stacktrace --->
==== JS stack trace =========================================
0: ExitFrame [pc: 0x100972e59]
Security context: 0x1a3a9d5008d1 <JSObject>
1: flipPairs [0x1a3ac10bfe51] [/Users/aneeshakella/Desktop/tests/flipper.js:~1] [pc=0x113893c44259](this=0x1a3ac10bfe31 <JSGlobal Object>,0x1a3a440d5a21 <String[#69]: check out how interesting this problem is, it's insanely interesting!>)
2: /* anonymous */ [0x1a3ac10bfe91] [/Users/aneeshakella/Desktop/tests/flipper.js:10] [bytecode=0x1a3a440d5ca1 offse...
FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
1: 0x101018fd1 node::Abort() (.cold.1) [/usr/local/bin/node]
2: 0x10008632b node::FatalError(char const*, char const*) [/usr/local/bin/node]
3: 0x10008646c node::OnFatalError(char const*, char const*) [/usr/local/bin/node]
4: 0x100187727 v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [/usr/local/bin/node]
5: 0x1001876c7 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [/usr/local/bin/node]
6: 0x100312e75 v8::internal::Heap::FatalProcessOutOfMemory(char const*) [/usr/local/bin/node]
7: 0x1003146ca v8::internal::Heap::RecomputeLimits(v8::internal::GarbageCollector) [/usr/local/bin/node]
8: 0x10031114e v8::internal::Heap::PerformGarbageCollection(v8::internal::GarbageCollector, v8::GCCallbackFlags) [/usr/local/bin/node]
9: 0x10030ef00 v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [/usr/local/bin/node]
10: 0x10030df51 v8::internal::Heap::HandleGCRequest() [/usr/local/bin/node]
11: 0x1002d36f1 v8::internal::StackGuard::HandleInterrupts() [/usr/local/bin/node]
12: 0x1006389ac v8::internal::Runtime_StackGuard(int, unsigned long*, v8::internal::Isolate*) [/usr/local/bin/node]
13: 0x100972e59 Builtins_CEntry_Return1_DontSaveFPRegs_ArgvOnStack_NoBuiltinExit [/usr/local/bin/node]
zsh: abort node flipper.js
这应该是一个非常简单的运行。是否有失败的原因。我的节点版本是12.16.3
问题是输入的长度,请尝试以下操作:
function flipPairs(input) {
var text = '';
for (var i = 0; i < input.length - 1; i = i + 2) {
text += input[i + 1] + input[i];
}
return text;
}
console.log(flipPairs('HI'));
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句