0

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.

Nisal Edu
  • 7,237
  • 4
  • 28
  • 34
Ashton
  • 63
  • 7
  • 1
    `animals.toLowerCase() === name.toLowerCase("name")` - that you are using the same method here in two completely different ways does not make you stop and go, huh? Especially since you are doing the same thing differently on the next line then, for the apparently totally superfluous `equal` variable ... – CBroe Nov 09 '17 at 08:33
  • 1
    what is the function supposed to do? searching or compairing? please name accordingly. – Nina Scholz Nov 09 '17 at 08:34
  • The function is suppose to search through an array of objects and return the correct object once the function identifies the typed in string matches the value on the property of the object. – Ashton Nov 09 '17 at 08:49
  • Is `animals` an array? Use the [browser console (dev tools)](https://webmasters.stackexchange.com/q/8525) (hit `F12`) and read any errors. – Sebastian Simon Nov 09 '17 at 08:58
  • please add some more data, like the one you are taking for comparison. – Nina Scholz Nov 09 '17 at 08:59
  • animals is an array, and name is the argument that is pass through to compare to the object's property. – Ashton Nov 09 '17 at 09:03
  • You need to show a [mcve] with the emphasis on *complete* – Quentin Nov 09 '17 at 09:09
  • I am updating my question with as much detail as I can provide. Keep in mind, I am new to programing and to Stack Overflow, and learning and trying to understand all these concepts. So I apologize in advance if my question comes across as undetailed/choppy language or it seems like I don't know what I'm talking about. – Ashton Nov 09 '17 at 09:17

2 Answers2

0

Try this

var animals = ["cat", "dog", "cow"];

function search(animal, name) {
  
  if (animal.toLowerCase() === name.toLowerCase()) {
    console.log("Matchs with : " + animal);
  } else {
    console.log("Not match with : " + animal);
  }
}

function myFunction() {
  console.clear();
  var nameInput = document.getElementById("myText").value;
  animals.forEach(function(entry) {
    search(entry, nameInput);
  });

}
Animal Name: <input type="text" id="myText" />
<button onclick="myFunction()">Search</button>
Nisal Edu
  • 7,237
  • 4
  • 28
  • 34
-3

function search(animals, name) {
if (animals.toLowerCase() === name.toLowerCase()) {
    var equal = animals.toLocaleLowerCase() === name.toLocaleLowerCase();
    console.log(equal); 
 }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<button onclick="search('abc','AbC')">Click me</button>
Zeus234
  • 119
  • 2
  • 12