FAQ: Loops - Nested Loops

2 posts were split to a new topic: [typo] What is wrong with my nested loop?

2 posts were merged into an existing topic: FAQ: Why can’t I console log mutualfollowers.push()

4 posts were split to a new topic: [typo] Is there a typo in my nested loop?

A post was merged into an existing topic: FAQ: Why can’t I console log mutualfollowers.push()

3 posts were split to a new topic: Formatting for loops and variable declarations

2 posts were split to a new topic: Do I need to declare my variables inside a for loop?

4 posts were split to a new topic: [typo] My nested loop won’t work?

2 posts were merged into an existing topic: Why do the loops work? (How do they ever match?)

2 posts were merged into an existing topic: Why do the loops work? (How do they ever match?)

4 posts were merged into an existing topic: Why do the loops work? (How do they ever match?)

14 posts were split to a new topic: How deep can nested loops go?

why does this just keep running without stopping?
let bobsFollowers = [‘s’,‘m’,‘t’,‘z’];
let tinasFollowers = [‘s’,‘m’,‘a’,];
let mutualFollowers = ;
for (let i = 0; i < bobsFollowers.length; i++) {
for (let j = 0; j < tinasFollowers.length; i++) {
if (bobsFollowers[i] == tinasFollowers[j]) {
mutualFollowers.push (bobsFollowers[i])
}
}
};

Hello, welcome to the forum.

You never change the value of j, so the inner loop is infinite :slight_smile:

1 Like

Hi guys,

Could someone explain why when logging bobsFollowers[i] or tinasFollowers[j] to the console we get [ ‘Marta’ ]
[ ‘Marta’, ‘Sam’ ] instead of [ ‘Marta’, ‘Sam’ ][ ‘Marta’, ‘Sam’ ].

Hello @rubypro32114. Welcome to the forum. We will have to see your code in order to explain the output. If you’ll paste it into a reply using the </> button, we’ll take a look. Thanks!

Note: Click the </> button first then paste your code in the space indicated:

mutualFollowers only has a single entry pushed to it on finding the match. A single list will be the outcome.

My code appears correct, but I get a very strange error:

My code:

let bobsFollowers = ['Bab', 'Bib', 'Bob', 'Boop'];

let tinasFollowers = ['Bib', 'Tim', 'Bab'];

let mutualFollowers = [];

for (let i=0; i=bobsFollowers.length; i++) {
  for (let j=0; j=tinasFollowers.length; j++) {
    if (bobsFollowers[i] === tinasFollowers[j]) {
        mutualFollowers.push(tinasFollowers[j]);
        }
  } 
}

console.log(mutualFollowers);

The error I am receiving:

<--- Last few GCs --->

[200:0x3949670]     1018 ms: Scavenge 178.7 (183.6) -> 178.7 (183.6) MB, 0.0 / 0.0 ms  allocation failure incremental marking delaying mark-sweep
[200:0x3949670]     1056 ms: Mark-sweep 178.7 (183.6) -> 108.0 (112.2) MB, 38.5 / 0.0 ms  (+ 4.3 ms in 1 steps since start of marking, biggest step 4.3 ms, walltime since start of marking 345 ms) last resort 
[200:0x3949670]     1094 ms: Mark-sweep 108.0 (112.2) -> 108.0 (111.7) MB, 37.5 / 0.0 ms  last resort 


<--- JS stacktrace --->

==== JS stack trace =========================================

Security context: 0x1e2e7b793471 <JS Object>
    2: /* anonymous */ [/home/ccuser/workspace/learn-javascript-loops_loops_for-loops-iii/main.js:~1] [pc=0x236c5aebf620](this=0xf0a7a8c5d61 <an Object with map 0x1edfd4484931>,exports=0xf0a7a8c5d61 <an Object with map 0x1edfd4484931>,require=0xf0a7a8c5f39 <JS Function require (SharedFunctionInfo 0x5a3fd4b62a9)>,module=0xf0a7a8c5eb1 <a Module with map 0x1edfd44ba4e9>,__filename=0xf0a7a8c5e49 <S...

FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
 1: node::Abort() [/usr/bin/node]
 2: 0x134e91c [/usr/bin/node]
 3: v8::Utils::ReportOOMFailure(char const*, bool) [/usr/bin/node]
 4: v8::internal::V8::FatalProcessOutOfMemory(char const*, bool) [/usr/bin/node]
 5: v8::internal::Factory::NewUninitializedFixedArray(int) [/usr/bin/node]
 6: 0xe1f0f5 [/usr/bin/node]
 7: v8::internal::Runtime_GrowArrayElements(int, v8::internal::Object**, v8::internal::Isolate*) [/usr/bin/node]
 8: 0x236c5ae040c7
/home/ccuser/.bin/node: line 3:   200 Aborted                 (core dumped) /usr/bin/node --max-old-space-size=200 $@

In each of your for loops you have an issue.
If I were iterating through an array I might do this:

const myArray = [1, 2, 3];
for(let i = 0; i < myArray.length; i++) {
  console.log(myArray[i]);
}

Here’s a link to the lesson on for loops to review if necessary.

1 Like

Yes, I see it now. I don’t know how I missed that! Thank you!

1 Like

You’re very welcome!