'first two case working in switch statement why last case is not worked?
I am making dictionary website, for the proper word search within the API, I used there one switch statement (3rd case) which is not working , remaining cases running properly but 3rd case only given loader not output please give me proper solution.
// get element const input = document.querySelector('#input'); const searchBtn = document.getElementById('button-addon2'); const loader = document.getElementById('loader'); const iword = document.querySelector('.iword'); const word = document.querySelector('.word'); const mean = document.querySelector('.meaning'); const audio = document.querySelector('audio');
// Event listner for search button searchBtn.addEventListener('click', getData); function getData(e) { e.preventDefault(); iword.textContent = ''; word.textContent = ''; mean.textContent = ''; // get input data let inputWord = input.value; // call api get data if (inputWord === '') { alert ('Word is Required') return; } getApiData(inputWord); } // get data from api async function getApiData(inputWord) { loader.style.display = 'block'; const apiKey = `https://www.dictionaryapi.com/api/v3/references/learners/json/${inputWord}?key=a6d01f54-9cfd-4941-957b-55935e9f4c5d`; try { let response = await fetch(apiKey); let data = await response.json(); switch (true) { case (!data.length) : loader.style.display = 'none'; iword.textContent = inputWord; word.textContent = 'No Result Found'; break; case (typeof data[0] === 'string') : loader.style.display = 'none'; iword.textContent = inputWord; let sHeading = document.createElement('h3'); sHeading.textContent = 'Did You Mean ?' mean.appendChild(sHeading); data.forEach(element => { let suggetion = document.createElement('span'); suggetion.classList.add('suggested'); suggetion.textContent = element; mean.appendChild(suggetion); }); break ; case data[0].shortdef[0] : loader.style.display = 'none'; iword.textContent = inputWord; word.textContent = meaning; console.log('hey'); break ; }} catch (error) { // catch error here } }
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
