Skip to content

Commit

Permalink
Merge pull request #148 from aakash-sharma25/English-Dictionary
Browse files Browse the repository at this point in the history
English dictionary using Dictionary Api
  • Loading branch information
Ayu-hack authored Oct 8, 2024
2 parents d1abefc + 4bfa118 commit 3032bb7
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
23 changes: 23 additions & 0 deletions dictionary web app/index.html
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>
40 changes: 40 additions & 0 deletions dictionary web app/index.js
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);
}
});
43 changes: 43 additions & 0 deletions dictionary web app/style.css
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;
}

0 comments on commit 3032bb7

Please sign in to comment.