Created
September 2, 2022 18:58
-
-
Save ali-aka-ahmed/a527f9c9f4448ba97256cd2319fe830b to your computer and use it in GitHub Desktop.
Edge cases when learning about 'search' functions within array operations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* ============================================= | |
* =========== SEARCH EDGE CASES =============== | |
* ============================================= | |
*/ | |
// NOT IN LIST | |
const listOfDogs = ["pomeranian", "shih tzu"]; | |
const isCorgiInList = (listOfDogs) => { | |
var searchFor = "corgi" | |
for (var i = 0; i < dogs.length; i++) { | |
const currentDog = dogs[i] | |
if (currentDog === searchFor) { | |
return true | |
} | |
} | |
return false | |
} | |
isCorgiInList(listOfDogs) // false | |
// NOT IN LIST | |
const listOfDogs = ["pomeranian", "shih tzu"]; | |
const isCorgiInList = (listOfDogs) => { | |
var searchFor = "corgi" | |
for (var i = 0; i < dogs.length; i++) { | |
const currentDog = dogs[i] | |
if (currentDog === searchFor) { | |
return true | |
} | |
} | |
} | |
isCorgiInList(listOfDogs) // undefined | |
// point here: we need to be careful when writing functions to make sure | |
// we're returning what we intend at the edge case. Do we want to make | |
// the function return false or undefined if we're searching for an item | |
// and it's not in the list? Depends on what we're doing. | |
// MULTIPLE IN LIST | |
const listOfDogs = ["pomeranian", "corgi", "shih tzu", "corgi"]; | |
const isCorgiInList = (listOfDogs) => { | |
var searchFor = "corgi" | |
for (var i = 0; i < dogs.length; i++) { | |
const currentDog = dogs[i] | |
if (currentDog === searchFor) { | |
return true | |
} | |
} | |
return false | |
} | |
isCorgiInList(listOfDogs) // true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment