Yh1234
Yh1234

Reputation: 151

Using array.map properly

I am not sure why my array.map function returns undefined but my for loop returns the result. When I console log map it displays:

Tom Dick Harry

and when console log for loop it shows

['Tom', 'Dick', 'Harry']

Not sure if this is the problem of showing undefiend.

var friends = ["Tom", "Dick", "Harry"];
var secondLevelFriends = ["Anne", "Harry", "Quinton"];
var allUsers = ["Tom", "Dick", "Harry", "Anne", "Quinton", "Katie", "Mary"];

function findPotentialFriends(existingFriends) {
  return function(x) {
    // existingFriends.map(function(elem) {
    //   if(elem === x) return false;
    //   else return true;
    // })
    for(var i = 0; i < existingFriends.length; i++) {
      if(existingFriends[i] === x) return false;
      else return true;
    }
  }
}

var isNotAFriend = findPotentialFriends( friends );
isNotAFriend(allUsers[0]); // false

Upvotes: 1

Views: 114

Answers (3)

Barmar
Barmar

Reputation: 781004

The problem with your code is that the return statements in the callback just return to map(), they don't return to the original caller. map() collects all these return values into an array and returns this, but you never do anything with the result of existingfriends.map().

Javascript has a built-in function to search an array for a matching element, Array.prototype.indexOf(). It returns -1 if no match is found.

So you can simply write:

var friends = ["Tom", "Dick", "Harry"];
var secondLevelFriends = ["Anne", "Harry", "Quinton"];
var allUsers = ["Tom", "Dick", "Harry", "Anne", "Quinton", "Katie", "Mary"];

function findPotentialFriends(existingFriends) {
  return function(x) {
   return existingFriends.indexOf(x) == -1;   
  }
}

var isNotAFriend = findPotentialFriends(friends);
console.log(isNotAFriend(allUsers[0])); // false

Upvotes: 1

theKidOfArcrania
theKidOfArcrania

Reputation: 498

First of all your for loop has some logic problems. it should be written as so:

var foundFriend = false;
for(var i = 0; i < existingFriends.length; i++) {
  if (existingFriends[i] === x)
  {
    foundFriend = true;
    break;
  }
}
return !foundFriend;

The reason why isNotAFriend evaluates to undefined is because you forgot a return statement. Furthermore, the Arrays.map just simply takes each element of the array, passes it into the function, then creates a new array based on what your function returns. What you might be searching for is perhaps Array.some see docs. Example:

var friends = ["Tom", "Dick", "Harry"];
var secondLevelFriends = ["Anne", "Harry", "Quinton"];
var allUsers = ["Tom", "Dick", "Harry", "Anne", "Quinton", "Katie", "Mary"];

function findPotentialFriends(existingFriends) {
  return function(x) {
    return !existingFriends.some(function(elem) {
      return elem === x;
    });
  }
}

var isNotAFriend = findPotentialFriends( friends );
console.log(isNotAFriend(allUsers[0]));

Upvotes: 1

prasanth
prasanth

Reputation: 22500

Simply add the return in map() function .Its one of the new function inside the return function .so apply with return this also.

var friends = ["Tom", "Dick", "Harry"];
var secondLevelFriends = ["Anne", "Harry", "Quinton"];
var allUsers = ["Tom", "Dick", "Harry", "Anne", "Quinton", "Katie", "Mary"];

function findPotentialFriends(existingFriends) {
  return function(x) {
   return existingFriends.map(function(elem) {
  if(elem === x) return false;
     else return true;
    })
   // for(var i = 0; i < existingFriends.length; i++) {
    //  if(existingFriends[i] === x) return false;
     // else return true;
    //}
  }
}

var isNotAFriend = findPotentialFriends(friends);
console.log(isNotAFriend(allUsers[0])); // false

Upvotes: 0

Related Questions