-3

i have two arrays

newArray1 = [104,101,108,108,111]; 

and

newArray2 = [104,101,121]; 

im simply checking to see if certain numbers in array1 are in array2.

for(i=0;i<newArray2.length;i++) { 
if(newArray.indexOf(newArray2[i]) > -1) { 
  console.log(newArray2[i]); 
} else { 

 }
}

when looping through newArray2 the loop only iterates through position 0 and 1 , and does not get to the last number. Im just curious as to why the entire array is not being looped through entirely. When logging to the console in the body of the for loop

console.log(newArray2[i]); 

I recieve

//104
//101

Not...

//104
//101
//121 
jalen201
  • 193
  • 14
  • `the loop only iterates through position 0 and 1` - no, only `0`, because a `return` tends to return from a function immediately – Jaromanda X Dec 30 '16 at 01:02
  • 3
    The code is doing exactly what you tell it to do. You don't get `121` because it is not contained in `newArray`. You are only logging the value if it is contained in `newArray`. That's what the `if(newArray.indexOf(newArray2[i]) > -1) { ` part does. What else did you expect the condition to do? *To be clear:* The whole array is iterated over but you are not calling `console.log` in every iteration. – Felix Kling Dec 30 '16 at 01:05
  • i removed the return statements in the question. In this example the if statement is true. when i console.log(newArray2[i]) i only recieve 0,and 1. – jalen201 Dec 30 '16 at 01:06
  • 1
    Well, now that you've changed the code to get rid of the `return`s you've made my comment look absurd - however, now your question looks equally absurd as you only console.log `newArray2[i]` if the value exists in newArray .... clearly 121 does not, hence your code, quite correctly, does not output 121 to the console – Jaromanda X Dec 30 '16 at 01:07
  • You get a reference error because you never defined `newArray`. Try providing a [live demo](https://stackoverflow.blog/2014/09/introducing-runnable-javascript-css-and-html-code-snippets/) and *testing* that it does what you think it does. – Quentin Dec 30 '16 at 01:07
  • I'm sure that typo will be fixed in another edit @Quentin - because the code as is wouldn't output anything to the console, except errors – Jaromanda X Dec 30 '16 at 01:09
  • i see the issue forget this question exists – jalen201 Dec 30 '16 at 01:12
  • I'm voting to close this question as off-topic because lacks minimal understanding – Oriol Dec 30 '16 at 01:15
  • This question has been answered here: [A for-loop that compares two arrays looking for matching values](http://stackoverflow.com/questions/9639065/a-for-loop-that-compares-two-arrays-looking-for-matching-values) – RogueThinking Dec 30 '16 at 01:23

1 Answers1

1

First of all, you name your variables newArray1 and newArray2at the top and newArray and newArray2 in the loop... I assume that's just you mis-copying your code.

The problem is you're using return, which ends the for loop... It looks like you just edited your code to fix that, and you are now getting the results you SHOULD get! 121 is not in newArray1, so the console.log inside the if statement never executes. Move it before if(newArray.indexOf(newArray2[i]) > -1) { and you'll see 121 in your console log.

Max F
  • 121
  • 1
  • 8