I have a search function that I am creating, in which I am trying to achieve in performing a search through several objects to perform a search on an object's name property to return a result. I am also trying to make the code case insensitive, so if a user were to type in 'CaT' or 'cat' in a search field, they would still return the "Cat" object. I am a new user to JS, and am familiar with toLowerCase/toUpperCase but reg/exp I struggle with. If anyone can supply any hints as to what I am doing wrong with this code?
function search(animals, name) {
if (animals.toLowerCase() === name.toLowerCase()) {
return animals;
}
}
// new edit Prompt question and my original code was as followed:
Implement a function called search
with a signature of search(animals, name) { //... }
that:
- Takes a paramater representing an Array of animals
.
- Takes a paramater representing a String, the name of an animal on which to perform a search.
- Looks through the animals
Array, and returns the animal's Object if an animal with that name exists.
- Returns null
if no animal with that name exists
function search(animals, name) {
for (var i = 0; i < animals.length-1; i++) {
if (animals[i].name === name) {
return animals[i];
} else {
console.log(null)
}
}
}
this worked in matching to the property of the object if it was typed in the EXACT casing it was created. All I'm trying to do is make the function work with whatever casing.