2021年1月28日星期四

Creating a function to go through an array and if it finds a specific numeric value it returns true

I'm creating a function to sort through an array to return a boolean indicating whether or not the number 7 is anywhere in the array.

Reading my code out loud I feel like it should do exactly what I am asking it to do. Although I fully realize I am a beginner and I'm going to make mistakes. I cannot figure this one out.

If I input includesSeven([1, 7, 8, 10]); this should return true, however I am getting false as my return

    function includesSeven(array) {    for (var i = 0; i <= array.length; i++) {      if (array[i] === 7) {        return true;      } else {        return false;      }    };  };  

EDIT: I do apologize for the image and not the code.

My code for this

https://stackoverflow.com/questions/65947769/creating-a-function-to-go-through-an-array-and-if-it-finds-a-specific-numeric-va January 29, 2021 at 09:55AM

没有评论:

发表评论