-
Notifications
You must be signed in to change notification settings - Fork 449
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #148 from aakash-sharma25/English-Dictionary
English dictionary using Dictionary Api
- Loading branch information
Showing
3 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>English Dictionary</title> | ||
<link rel="stylesheet" href="style.css"> | ||
</head> | ||
<body> | ||
<div class="container"> | ||
<h1 class="heading">English Dictionary</h1> | ||
<input placeholder="Search a word" type="text" class="input" id="input" /> | ||
<p class="info-text" id="info-text">Type a word and press enter</p> | ||
<div class="meaning-container" id="meaning-container"> | ||
<p>Word Title: <span class="title" id="title">___</span></p> | ||
<p>Meaning: <span class="meaning" id="meaning">___</span></p> | ||
<audio src="" controls id="audio"></audio> | ||
</div> | ||
</div> | ||
<script src="index.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
const inputEl = document.getElementById("input"); | ||
const infoTextEl = document.getElementById("info-text"); | ||
const meaningContainerEl = document.getElementById("meaning-container"); | ||
const titleEl = document.getElementById("title"); | ||
const meaningEl = document.getElementById("meaning"); | ||
const audioEl = document.getElementById("audio"); | ||
|
||
async function fetchAPI(word) { | ||
try { | ||
infoTextEl.style.display = "block"; | ||
meaningContainerEl.style.display = "none"; | ||
infoTextEl.innerText = `Searching the meaning of "${word}"`; | ||
const url = `https://api.dictionaryapi.dev/api/v2/entries/en/${word}`; | ||
const result = await fetch(url).then((res) => res.json()); | ||
|
||
if (result.title) { | ||
meaningContainerEl.style.display = "block"; | ||
infoTextEl.style.display = "none"; | ||
titleEl.innerText = word; | ||
meaningEl.innerText = "N/A"; | ||
audioEl.style.display = "none"; | ||
} else { | ||
infoTextEl.style.display = "none"; | ||
meaningContainerEl.style.display = "block"; | ||
audioEl.style.display = "inline-flex"; | ||
titleEl.innerText = result[0].word; | ||
meaningEl.innerText = result[0].meanings[0].definitions[0].definition; | ||
audioEl.src = result[0].phonetics[0].audio; | ||
} | ||
} catch (error) { | ||
console.log(error); | ||
infoTextEl.innerText = `an error happened, try again later`; | ||
} | ||
} | ||
|
||
inputEl.addEventListener("keyup", (e) => { | ||
if (e.target.value && e.key === "Enter") { | ||
fetchAPI(e.target.value); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
body{ | ||
margin: 0; | ||
display: flex; | ||
min-height: 100vh; | ||
justify-content: center; | ||
align-items: center; | ||
/* background-color:royalblue; */ | ||
background-color: #AA77FF; | ||
font-family: 'Courier New', Courier, monospace; | ||
} | ||
|
||
.container{ | ||
background-color: #62CDFF; | ||
padding: 28px; | ||
border-radius: 7px; | ||
box-shadow: 0 10px 10px rgba(2, 2, 2, 0.3); | ||
width: 90%; | ||
margin: 10px; | ||
max-width: 450px; | ||
text-align: center; | ||
font-size: 18px; | ||
font-weight: 500; | ||
} | ||
|
||
.heading{ | ||
font-size: 28px; | ||
} | ||
|
||
.input{ | ||
height: 53px; | ||
width: 300px; | ||
background-color: #fff; | ||
border-color: rgba(255,255,255, .4); | ||
font-size: 16px; | ||
padding: 0 42px; | ||
border-radius: 5px; | ||
} | ||
|
||
.meaning-container{ | ||
display: none; | ||
color:navy; | ||
font-size: 1.5rem; | ||
} |