Skip to content
TheChatArchiveThe Chat Archive
o1-preview-2024-09-12· OpenAIAi2 WildChat-4.8M (AI2)

// let url = "http://universities.hipolabs.com/search?name=" // let btn =…

  1. User

    // let url = "http://universities.hipolabs.com/search?name="

    // let btn = document.querySelector("button") // btn.addEventListener("click", async()=> { // let country = document.querySelector("input").value; // console.log(country); // let collArr = await getcolleges(country); // show(collArr); // });

    // function show(collArr){ // let list = document.querySelector("#list"); // list.innerText = ""; // for (col of collArr){ // console.log(col.name); // let li = document.createElement("li") // li.innerText = col.name; // list.appendChild(li); // } // }

    // async function getcolleges(country) { // try { // let res = await axios.get(url + country) // return res.data; // } catch(err) { // console.log("error :" , e); // return []; // }

    // } i want to use a simiplar code but now i want to print the name of colleges and there state for india

  2. Assistant

    To modify your code to print the names of colleges and their states for India, you'll need to adjust the API endpoint you're using to include the country=India parameter. Additionally, you'll want to extract the state-province information from the API response and display it alongside the college names.

    Here's the modified code:

    let url = "http://universities.hipolabs.com/search?country=India&name=";
    
    let btn = document.querySelector("button");
    btn.addEventListener("click", async () => {
        let searchTerm = document.querySelector("input").value;
        console.log(searchTerm);
        let collArr = await getcolleges(searchTerm);
        show(collArr);
    });
    
    function show(collArr) {
        let list = document.querySelector("#list");
        list.innerText = "";
        for (let col of collArr) {
            console.log(col.name);
            let li = document.createElement("li");
            li.innerText = `${col.name}, ${col["state-province"] || "Unknown State"}`;
            list.appendChild(li);
        }
    }
    
    async function getcolleges(searchTerm) {
        try {
            let res = await axios.get(url + encodeURIComponent(searchTerm));
            return res.data;
        } catch (err) {
            console.log("error:", err);
            return [];
        }
    }
    

    Explanation:

    • API Endpoint Modification: The URL now includes country=India to fetch universities only from India. The name= parameter is used to search universities by name based on the user's input.

      let url = "http://universities.hipolabs.com/search?country=India&name=";
      
    • Getting User Input: The user's input is now considered a search term for the university name rather than a country.

      let searchTerm = document.querySelector("input").value;
      
    • Displaying the State: In the show function, we're now including the state-province property from the API response. If state-province is null, we'll display "Unknown State".

      li.innerText = `${col.name}, ${col["state-province"] || "Unknown State"}`;
      
    • Error Handling Correction: Fixed the error logging variable from e to err in the catch block.

      catch (err) {
          console.log("error:", err);
      }
      

    Usage:

    When the user enters a search term (e.g., a part of a university's name) and clicks the button, the script will:

    1. Fetch universities in India that match the search term.
    2. Display a list of universities with their names and states.

    Example:

    If the user searches for technology, the list might include:

    • Indian Institute of Technology Bombay, Maharashtra
    • National Institute of Technology Karnataka, Karnataka
    • ...

    Note:

    • Ensure you have included the Axios library in your HTML file for the axios.get method to work.
    • The state-province field might be null for some universities, hence the use of the || "Unknown State" fallback.

    HTML Setup (for completeness):

    Make sure your HTML file has the necessary elements:

    <input type="text" placeholder="Enter college name" />
    <button>Search</button>
    <ul id="list"></ul>
    
    <!-- Include Axios from a CDN -->
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
    <!-- Include your script here -->
    <script src="your-script.js"></script>
    

    This setup will allow users to input a college name or part of it, click the search button, and see a list of matching colleges in India along with their states.