For loops a bit confusing

im here again, seeking for help :confused:

Look at this code:


function pairwise(arr, arg) {
  

  return arr.reduce(function(acc, next, index, array){
    
   for (var i = index + 1; i<arr.length; i++); {

    if(array[index] + array[i] === arg) {

        acc += index + i; 
    
        array[index] = array[i] = NaN; 
    
}

  }

      return acc; 


}, 0);


}

pairwise([1,4,2,3,0,5], 7);

THE RESULT IS 11…I was told, that for loops stops, when the IF statement is true, so in this case 3+4 = 7 ,which is true, so how come the loop continues to loop?

Here is the case, when it stops looping:

function findElement(arr, func) {
  
  for (i = 0; i < arr.length; i++) {
    if (func(arr[i])) {
      return arr[i];
    }
   
  
}  
 }

findElement([1, 2, 3, 4], function(num){ return num % 2 === 0; });

THE RESULT IS 4

Add a break; statement inside the if block. The break statement (as the name suggests) breaks out of a loop prematurely.

By the way, the program logic seems slightly weird. Can you tell us what the challenge is?

#1 EXAMPLE:

Given an array arr, find element pairs whose sum equal the second argument arg and return the sum of their indices.

If multiple pairs are possible that have the same numeric elements but different indices, return the smallest sum of indices. Once an element has been used, it cannot be reused to pair with another.

For example pairwise([7, 9, 11, 13, 15], 20) returns 6. The pairs that sum to 20 are [7, 13] and [9, 11]. We can then write out the array with their indices and values.

7 + 13 = 20 → Indices 0 + 3 = 3
9 + 11 = 20 → Indices 1 + 2 = 3
3 + 3 = 6 → Return 6

#2 EXAMPLE:

Create a function that looks through an array (first argument) and returns the first element in the array that passes a truth test (second argument).
findElement([1, 3, 5, 8, 9, 10], function(num) { return num % 2 === 0; }) should return 8.

Just to make everything clearer

hm yeah but in the #2 example, we have an array [1, 2, 3, 4] and only 4 is returned and not 2? how come?

findElement([1, 2, 3, 4], function(num){ return num % 2 === 0; });

Both 2 and 4 are equal to 0

yes thats what i was thinking, and thats why i was comparing those two example, but i didnt know it has to do with the RETURN statement, so i was confused why the #1 example keeps on looping, when it already found a truthiness, but now it goes on and finds another one. Okay thank you, it makes a bit more clearer.

Okay one more question…For example i want to return both numbers (2, 4) how could i rewrite this?
I tried it like this, but it says undefined -->

function findElement(arr, func) {
  var nums = [];
  for (i = 0; i < arr.length; i++) {
    if (func(arr[i])) {
      nums.push(arr[i]);
    }

return nums;
   
  
}  
 }

findElement([1, 2, 3, 4], function(num){ return num % 2 === 0; });

it returns an empty array basically

ok i did it! had to move my “return nums” a bit down…

1 Like

it works now

function findElement(arr, func) {
  var nums = [];
  for (i = 0; i < arr.length; i++) {
    if (func(arr[i])) {
      nums.push(arr[i]);
      
    }
    
      
   
  
}  

 return nums;
 }

findElement([1, 2, 3, 4], function(num){ return num % 2 === 0; });