vendredi 5 juin 2020

If statement not returning anything inside of two for loops

I'm trying to return an array inside of an if statement, but nothing is returning. When I console.log( [ i, j ] ), it works fine.

const twoSum = function(nums, target) {
   for (let i = 0; i < nums.length; i++) {
       for (let j = 0; j < nums.length; j++) {
           if (nums[i] + nums[j] === target) {
               return [ i, j ];
           }
       }
   }
};

twoSum([ 2, 7, 11, 15 ], 9);

I understand that this is not the most efficient way to solve this problem, but I'm just learning the basics and I'm very confused as to why this is not returning anything.

Aucun commentaire:

Enregistrer un commentaire