Use the some Method to Check that Any Elements in an Array Meet a Criteria-print-out

How can we print out the element(s) that meets the criteria with if ...some()

Your code so far


function checkPositive(arr) {
  // Add your code below this line
  if (arr.some(function(x) {
    console.log(arr[x]);
  }))
  
  // Add your code above this line
}
console.log(checkPositive([1, 2, 3, -4, 5]));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:65.0) Gecko/20100101 Firefox/65.0.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/functional-programming/use-the-some-method-to-check-that-any-elements-in-an-array-meet-a-criteria

Well you have learned that the some method returns a Boolean value. However, you must also return a Boolean value when you check at least one of arr’s values is positive.

Hint: You do not need an if statement to pass this challenge.

1 Like

Yes, you are right but I wanted to use if and some print out the met element;
I have already passed the challenge

So first, you really need an understanding of what the .some(...) is expecting for parameters. the first parameter, the only one you will almost always provide, is a function. This function should return true or false, depending on some criteria within the array. For example, if we wanted to check if a value was greater than zero, then we would simply create a function to check if a value is greater than zero, return true if it is and false if it isn’t, and assign that as the default (first) parameter to .some()

What might that look like? How about a hypothetical? Let’s say we were asked to write a function that checks if any member of an array is a string longer than five characters. How would that work? To start, let’s create that function:

let isLongerThanFive = function(stringValue){
  // note that the following expression is either true, or it isn't. Our function doesn't return
  //   the length, it simply returns true or false.
  return stringValue.length > 5;
}

so now, if we wanted, we could use something like that:

function checkLongerThanFive(arr){
  /**
   * In this, we want to return true if any member's length is greater than five chars. How can
   *  we? We can use arr.some(...), and pass it a function similar to the one we defined above!
   **/
  return arr.some(function(stringValue){
    // Here, if we wanted, we could print something:
    console.log(stringValue+" is "+stringValue.length+" characters long.");

    // So long as our function finally returns something, we can do whatever we like
    //  in here!
    return stringValue.length > 5
  });
}

The same rule applies for .map(), .reduce() or .filter() – you can output whatever, you can update elements (though this could be considered bad practice), so long as you return a value at the end. In the case of .filter(), you want to return a true or false. In the case of .map(), you are returning something that will be pushed onto the end of an array.

Each of these, including .some(), are higher-order functions. They are functions that take another function, and perform that action on something.

Note that I also have a neat option for a easier-to-read version of the above code:

function checkLongerThanFive(arr){
  function isLongerThanFiveChars(stringValue){
    console.log(stringValue+" is "+stringValue.length+" characters long.");
    return stringValue.length > 5;
  }

  // the following line uses the above named function -- making my code more readable.
  //  the function still returns true or false for EACH member of the array, and works exactly
  //  the same as the above in-line anonymous function.
  return arr.some(isLongerThanFiveChars);
}
2 Likes

Thanks it was really informative. :star:

Glad it helped. I tend to slide into “professor mode” sometimes. But it also helped that you specified what it was you were looking for – how to do other things (like debugging your code) within the callback function.

A lot of this is a matter of practice and experience, but most of it is research. If you read on the MDN pages about Array.prototype.some(), it’s a very well-written description of what it expects, and what it actually does.

also, I highly recommend bookmarking http://devdocs.io/ – it contains teh MDN docs and SO much more. Very useful stuff.

Best of luck!

1 Like

Yes, I passed the challenge but I tried to mess around and do more thing that might be don by that, which means exploring and remembering

1 Like

ALWAYS good practice. I learn more by venturing off the beaten path, and breaking something. Keep up the curiousity. :wink:

2 Likes

That is the best way :+1::+1:
Best of lucks

Thank you, @snowmonkey. This is super helpful. Your notes are clear and direct to the point. I dig it.

1 Like

Lol glad it helped. I tend to babble, but i try to babble with purpose.

1 Like