964 views
4 votes
Javascript

Define a function, myIncludes, that accepts an array and a searchValue as arguments. myIncludes should return true if the searchValue is an element in the array. Otherwise, myIncludes should return false. Do not use the built-in .includes array method during this problem. Feel free to use it on any future problem though! Note that strings have an .includes method, too.

User Jim Flood
by
7.4k points

1 Answer

4 votes
Sure! Here's an implementation of myIncludes function in JavaScript:


This function accepts an array and a searchValue as arguments. It then loops through the array and checks if each element in the array is equal to the searchValue. If it finds a match, it immediately returns true. If it loops through the entire array without finding a match, it returns false.

Note that we're using a for loop to iterate over the array and we're checking each element in the array for a match with the searchValue using the strict equality operator (===). If a match is found, we immediately return true. If no match is found by the end of the loop, we return false.

This implementation does not use the built-in .includes() method, as requested in the problem statement.
Javascript Define a function, myIncludes, that accepts an array and a searchValue-example-1
User Cynthia Simiyu
by
7.7k points