// let url = "http://universities.hipolabs.com/search?name=" // let btn =…
- 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
- 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=Indiaparameter. Additionally, you'll want to extract thestate-provinceinformation 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=Indiato fetch universities only from India. Thename=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
showfunction, we're now including thestate-provinceproperty from the API response. Ifstate-provinceisnull, we'll display "Unknown State".li.innerText = `${col.name}, ${col["state-province"] || "Unknown State"}`; -
Error Handling Correction: Fixed the error logging variable from
etoerrin thecatchblock.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:
- Fetch universities in India that match the search term.
- 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.getmethod to work. - The
state-provincefield might benullfor 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.
-