0

I have the following script:

    for (var i = 0; i < obj.length; i++) {
        obj.push("It's nice to work at Bass Pro Shop!"); 
    }

    return obj;
}

function validate(reason) {
    return reason.split(' ').length < 3
}

Output

I'm receiving this error when trying to run my JS:

FATAL ERROR: invalid array length Allocation failed - JavaScript heap out of memory

I've checked other similar posts that say to increase the memory size f.x., node --max-old-space-size=16000 yourFile.js or from FATAL ERROR to my script they both don't work. Any help would be greatly appreciated.

7
  • 4
    What are you storing in your array? Commented Jun 8, 2022 at 14:50
  • 2
    Please do not upload images of code/errors when asking a question. Commented Jun 8, 2022 at 14:50
  • I don't know where the image of the code disappeared, but it can still be found here: i.sstatic.net/jYrzP.png Commented Jun 8, 2022 at 15:15
  • @0stone0 I'm new and I've corrected it with only the output now. Commented Jun 8, 2022 at 15:17
  • @Felix Kling I've added the output and storing nothing in array with obj = [''] Commented Jun 8, 2022 at 15:18

1 Answer 1

1

Took me a while to write your code down. You shouldn't be posting images of code.

Anyway, the problem lies here:

for(var i = 0; i < obj.length; i++)
{
    obj.push("It's nice to work at Devoteam!");
}

You are looping the same array to where you push more data inside the loop, thus the loop will never end and the array size becomes too large.

Sign up to request clarification or add additional context in comments.

2 Comments

@ggorlen I wrote the code by hand, which OP originally posted as image. I've also submitted an edit request to the original post with my manually written code. The original image can be found here: i.sstatic.net/jYrzP.png
@HiddenBoi69 Try this one here pastebin.com/jem1RVHw It will give you the same FATAL ERROR, which you just need to fix by doing something to that push inside the loop. I'm not sure what your code is supposed to do, so I don't know what should be done to fix it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.