mercredi 26 septembre 2018

JavaScript if statement with multiple conditions and checking to see if input is empty

I have a JavaScript exercise where you have 3 different ways to search for a business listing. I'm trying to do an if statement that says if the 1st option is equal to 1 or higher (meaning not empty) and the other 2 search options are set to 0 (meaning empty) then something happens. However, the code isn't running my if statement. I know this because of me checking the console. What's wrong with my if statement?

JS

    <script>
    var listOfBusinesses = {
    "Food": "Leroy's Restaurant",
    "Hair": "Angie's Hair Salon",
    "Beauty": "Nora - MakeUp Artist",
    "Hardware": "Harry and Blu",
    "Food": "Elijah's Market",
    "Beauty": "Flair Nail Salon",
    "Retail": "Tiffany's Boutique",
    "Retail": "Charisma Home Decor",
    "Hair": "Lady Luck Salon",
    "Food": "Charlie's Produce"
    };

    function getBusinessResults(databaseOfBusinesses) {
    var searchByName = $("#inputByName").val();
    console.log(searchByName);
    var searchByFirstL = $("#inputFirstLetter").val();
    console.log(searchByFirstL);
    var searchByCategory = $("#inputByCategory").val();
    console.log(searchByCategory);
    event.preventDefault();

    if (searchByName > 0 && searchByFirstL == 0 && searchByCategory 
    == 0) {
    console.log("apple");
    for (i = 0; i < databaseOfBusinesses.length; i++) {
    if (searchByName == databaseOfBusinesses[i]) {
    console.log("apple");
    $("#businessResults").html("Yes " + searchByName + "is a 
    business listed with us.");
    } // line closes if statement 
    } // line closes for loop
    } // line closes if statement
    } // line closes function 
    </script>

HTML:

    <form action="javascript-exercise-15.html" method="post">
    <p id="byName">Search by Business Name</p>
    Business Name: <input id="inputByName" type="text" 
    name="businessName" value="">
    <br/><br/>
    <p id="byFirstL">Search by 1st Letter of Business Name</p>
    First Letter of Business Name: <input id="inputFirstLetter" 
    type="text" name="firstLetter" value="">
    <br/><br/>
    <p id="byCategory">Search by Category of Business</p>
    Category of Business: <input id="inputByCategory" type="text" 
    name="businessCategory" value="">
    <br/><br/></br/>
    <button class="businessBtn" 
    onclick="getBusinessResults(listOfBusinesses)">Search 
    Business</button>
    </form>
    <p id="businessResults"></p>

Aucun commentaire:

Enregistrer un commentaire