I’ve been working throught the introduction to JavaScript course on Codecademy https://www.codecademy.com/courses/introduction-to-javascript/lessons/javascript-iterators/exercises/find-index and thought I would try to extend one of their ideas to search for all strings within an array that start with the string ‘s’.
I define an example array called animals and populate it with some strings. First I use the .findIndex()
method to find the first animal name beginning with 's'
and save its value to a variable named sIndex
. Then I try to splice the animals array from sIndex + 1
, apply findIndex()
and then update sIndex
with the value returned.
Here is the code I have so far:
const animals = ['hippo', 'tiger', 'lion', 'seal', 'cheetah', 'monkey', 'salamander', 'elephant']; //Create a function that finds all animals in the array animals starting with the letter s. let sNameAnimals = []; let sIndex = animals.findIndex(animal => animal[0] === 's'); let numOfAnimals = animals.length while (sIndex !== -1){ // Push the current found animal onto the array sNameAnimals sNameAnimals.push(animals[sIndex]) // Find the next animal that starts with s in the animals array. animalSlice = animals.slice(sIndex+1); sIndex = animalSlice.findIndex(animal => animal[0] === 's'); }
However, when I run this in the console through node.js, I get the following error message:
<--- Last few GCs ---> [16999:0x103240000] 104105 ms: Mark-sweep 577.2 (588.5) -> 577.2 (581.5) MB, 1429.9 / 0.0 ms (average mu = 0.913, current mu = 0.000) last resort GC in old space requested [16999:0x103240000] 105504 ms: Mark-sweep 577.2 (581.5) -> 577.2 (581.5) MB, 1398.8 / 0.0 ms (average mu = 0.801, current mu = 0.000) last resort GC in old space requested <--- JS stacktrace ---> ==== JS stack trace ========================================= 0: ExitFrame [pc: 0x2e143204fb7d] Security context: 0x0d42a071d969 <JSObject> 1: /* anonymous */ [0xd427bf849e9] [/Users/cmg18/Documents/Javascript/Iterators/iterators.js:~1] [pc=0x2e14320f0b82](this=0x0d427bf84b19 <Object map = 0xd42f1a82521>,0x0d427bf84b19 <Object map = 0xd42f1a82521>,0x0d427bf84ad9 <JSFunction require (sfi = 0xd42db01b049)>,0x0d427bf84a51 <Module map = 0xd42f1acbc21>,0x0d42db021c49 <String[56]: /Users/cmg18/Doc... FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory 1: 0x10003a9d9 node::Abort() [/Users/cmg18/.nvm/versions/node/v11.0.0/bin/node] 2: 0x10003abe4 node::FatalTryCatch::~FatalTryCatch() [/Users/cmg18/.nvm/versions/node/v11.0.0/bin/node] 3: 0x10019ed17 v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [/Users/cmg18/.nvm/versions/node/v11.0.0/bin/node] 4: 0x10019ecb4 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [/Users/cmg18/.nvm/versions/node/v11.0.0/bin/node] 5: 0x1005a5882 v8::internal::Heap::FatalProcessOutOfMemory(char const*) [/Users/cmg18/.nvm/versions/node/v11.0.0/bin/node] 6: 0x1005aedd4 v8::internal::Heap::AllocateRawWithRetryOrFail(int, v8::internal::AllocationSpace, v8::internal::AllocationAlignment) [/Users/cmg18/.nvm/versions/node/v11.0.0/bin/node] 7: 0x10057d3c6 v8::internal::Factory::NewFixedArrayWithFiller(v8::internal::Heap::RootListIndex, int, v8::internal::Object*, v8::internal::PretenureFlag) [/Users/cmg18/.nvm/versions/node/v11.0.0/bin/node] 8: 0x1005238c4 v8::internal::(anonymous namespace)::ElementsAccessorBase<v8::internal::(anonymous namespace)::FastPackedObjectElementsAccessor, v8::internal::(anonymous namespace)::ElementsKindTraits<(v8::internal::ElementsKind)2> >::GrowCapacity(v8::internal::Handle<v8::internal::JSObject>, unsigned int) [/Users/cmg18/.nvm/versions/node/v11.0.0/bin/node] 9: 0x1007fec82 v8::internal::Runtime_GrowArrayElements(int, v8::internal::Object**, v8::internal::Isolate*) [/Users/cmg18/.nvm/versions/node/v11.0.0/bin/node] 10: 0x2e143204fb7d
I’m not sure what I’m doing that is causing the error? I would have thought my while loop should terminate due to the splicing.
Advertisement
Answer
So, basically yo have used slice
(even though you said splice
in question title).
I would advice you to use the following code, it basically does the same thing, with a little less steps:
const animals = ['hippo', 'tiger', 'lion', 'seal', 'cheetah', 'monkey', 'salamander', 'elephant']; const sNameAnimals = animals.filter(animal => animal[0] === "s");