Why this happend?

why in the code blow the nothing is the same got/get printed 6 times ? and how to prevent it ?

const myArray = [6, 3, 20];
const yourArray = [19, 81, 2];
for (let i = 0; i < myArray.length; i++) {
  for (let j = 0; j < yourArray.length; j++) {
    if (myArray[i] === yourArray[j]) {
      console.log('Both arrays have the number: ' + yourArray[j]);
    } else {
      console.log('nothing is the same')
    }
  }
}
1 Like

Whenever the two numbers being accessed in your loop are not equal to each other, your code logs ‘nothing is the same’.

To change that, it depends on what you’re trying to do. Do you truly want it to indicate that there are no matches? Or do you want it to simply say when the numbers currently being accessed aren’t equal?

2 Likes

Yep @taylorsabbag true if we keep the Nothing is the same prompt inside this nested loop will keep on printing this when the [i] != [j]. So for this code it will be printed not just 6 times but 3^3 times. I think the better approach would be created a empty array and push the same elements inside that array one by one. Once the loop is done print that array or print Nothing is the same.

My approach would be

function compareArrays(array1, array2) {
  let similarNumbers = [];

  for (let i = 0; i < array1.length; i++) {
    for (let j = 0; j < array2.length; j++) {
      if (array1[i] === array2[j]) {
        similarNumbers.push(array1[i]);
        break;
      }
    }
  }

  if (similarNumbers.length > 0) {
    console.log("Similar numbers:", similarNumbers);
  } else {
    console.log("Nothing is the same");
  }
}

const array1 = [16, 13, 10];
const array2 = [19, 81, 22, 13, 10];
compareArrays(array1, array2);

Now at last coming to your doubt why it’s printing nothing is the same 9 times I will elaborate step by step:

  • Your loop starts with the first index of myArray i.e. 6
  • It moves to the next loop and starts with the first index of the yourArray i.e. 19
  • Now if statement compares the elements and notices they are not equal to it moves to the else statement and prints nothing is the same.
  • now we do j+1 now move to the next index i.e. 81
  • If statement again compares 6 with 81 which is not equal so we move to the else statement and print nothing is the same
  • This sequence happen for each element of myArray and as none of them matches with yourArray elements so your output will look like nothing is the same printed 9 times.

Hope this helps you @ariannazer. If you have any further doubts let us know in the reply.

2 Likes

i want it to say that : the numbers currently being accessed aren’t equal?
because its better than saying nothing

1 Like

dude the code you wrote is beyond of my level im a beginner not a code crew like you , but appreciate it that you helped me

i think i got my answer here for why it gets printed 6(9) times to the console on my example , but can you explain it again

i think i kinnda undrestand what you said but can you please , also say it agian that how you maked it so it wont print 6 (9) times to the console but only 1 time if there is no match

1 Like

Alright Wait I have an idea I will show you on a page how it works. Let me write it and upload it here.

2 Likes

ok thank you so much

1 Like


So the main culprit behind you code printing “nothing is same” 9 times in output is your print statements to be present inside the loops. Its clear if you keep the print conditions inside the loops then they will execute them once it satisfies the conditions. In your code case they are:

 if (myArray[i] === yourArray[j]) {
      console.log('Both arrays have the number: ' + yourArray[j]);
    } else {
      console.log('nothing is the same')
}

So as shown in the pic above your code will iterate 9 times means your conditions will run 9 times so 9 outputs will be shown in the terminal. In your case the loop iterated 9 times and everytime the numbers in the arrays were not equal so you are getting nothing is same 9 times in the terminal.

Now talking about my code it’s not at all that tough. What I did was to create 1 empty array. Inside the loop if the numbers in the arrays are equal then they use to get pushed inside that array.
What I did different from your code is bringing the printing conditions outside of the loop so it won’t print again and again. Right now you have only 6 elements to compare lets say I give you [1,2,3,4,5,6,7,8,9] and [11,22,33,44,55,66,77,88,99] for your code it will have 9^9 outputs as nothing is same.

Now if you still don’t understand my solution or the explanation why your code was showing nothing is same 9 times then come on Codecademy discord server voice channel and I will show you debugging the code live in my IDE it will make it more clear and I’m 100% sure you won’t have the doubt.

2 Likes

Feel free to tag me I’m online I can come on Voice chat on Codecademy discord and show you debugging the code live. :slight_smile:

2 Likes

Also @ariannazer from next time never mark a answer as solution until you are not 100% sure you got your answer.

2 Likes

actually that was my answer
thanks , you are one of the dedicated person i saw here

the second picture you wrote , i cant read 10% of what you have wrote . can you type it please

1 Like

This means a lot thanks for the compliment @ariannazer. I can relate with you and don’t worry something which might be easy from someone else can be tough for you and vice versa. I felt the same way when I started development and DSA (data structures and algorithms) these kind of problems are meant to force you to think. Never give up brute-force your way. The problem you are stuck right now I was stuck at the same place and someone helped me back them too. Understanding Loops, functions and classes can be tricky at first it’s all good keep asking questions. Good Luck.

1 Like

Sure. So in second picture I was trying to show you the iterations that the loops will do in your code. This will show why you were getting 9 outputs as nothing is same

Iteration 1

i=0 j=0
value of i=6 j=19

As the elements are not equal so the next step compiler will is to move in your if/else conditions that you’ve defined below. As elements are not equal else statement will run and print nothing is the same in the terminal.

Iteration 2

i=0 j=1
value of i=6 j=81

As elements are not equal else statement will run and print nothing is the same in the terminal.

Iterations will keep happening like this till Iteration 9

Iteration 9

i=3 j=3
value of i=20 j=2

As elements are not equal else statement will run and print nothing is the same in the terminal.

So as you can see your loop will iterate 9 times and as per your conditions inside the loop it will keep printing nothing is the same for every iteration.

1 Like