Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added dictionary app #44

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Web Devlopment Project/Dictionary App/book.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 39 additions & 0 deletions Web Devlopment Project/Dictionary App/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="shortcut icon" href="book.png" type="image/x-icon">
<title>Dictionary App</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css">
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/all.min.css">

</head>
<body>
<div class="container mt-3">
<div class="row">
<div class="col-lg-6 mx-auto">
<div class="d-flex gap-2 align-items-center justify-content-center mb-3">
<img src="book.png" alt="logo" width="50px" class="">
<h2>Dictionary App</h2>
</div>
<div class="d-flex gap-2">
<input type="text" class="form-control" placeholder="Enter a word..." id="input">
<button class="btn btn-success" id="btn">Search</button>

</div>
<h2 id="word" class="mt-3 text-center"></h2>

<ul id="meaning" class="p-3">

</ul>
<div id="lower">

</div>
</div>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
58 changes: 58 additions & 0 deletions Web Devlopment Project/Dictionary App/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const input = document.getElementById('input');
const btn = document.getElementById('btn');
const meaning = document.getElementById('meaning');
const word = document.getElementById('word');
const lower = document.getElementById('lower')

btn.addEventListener('click', () => {
if (input.value == "") {
alert("Input field is empty");
} else {
word.innerHTML = input.value;
fetchWord(input.value);
}
})


const fetchWord = async (word) => {
meaning.innerHTML = ""
lower.innerHTML = ""
try {

const res = await fetch("https://api.dictionaryapi.dev/api/v2/entries/en/" + word)
const data = await res.json()
console.log(data)
let sound = data[0].phonetics[0].audio
const meanArr = data[0].meanings[0].definitions;

meanArr.forEach((ele, index) => {
const li = document.createElement('li')
li.innerHTML = `

${ele.definition}
`
meaning.appendChild(li)

})

if (sound) {
const soundBtn = document.createElement('button');
soundBtn.classList.add('btn','btn-dark')
soundBtn.innerHTML = `<i class="fa-solid fa-microphone"></i>`
lower.appendChild(soundBtn)

soundBtn.addEventListener('click', () => {
let audio = new Audio(sound)
audio.play(sound)
})
}


} catch (err) {
console.log(err)
meaning.innerHTML = `<p>Sorry We can not find this word!!</p>`
}


}

Empty file.
Binary file added book.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 39 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="shortcut icon" href="book.png" type="image/x-icon">
<title>Dictionary App</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css">
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/all.min.css">

</head>
<body>
<div class="container mt-3">
<div class="row">
<div class="col-lg-6 mx-auto">
<div class="d-flex gap-2 align-items-center justify-content-center mb-3">
<img src="book.png" alt="logo" width="50px" class="">
<h2>Dictionary App</h2>
</div>
<div class="d-flex gap-2">
<input type="text" class="form-control" placeholder="Enter a word..." id="input">
<button class="btn btn-success" id="btn">Search</button>

</div>
<h2 id="word" class="mt-3 text-center"></h2>

<ul id="meaning" class="p-3">

</ul>
<div id="lower">

</div>
</div>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
58 changes: 58 additions & 0 deletions script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const input = document.getElementById('input');
const btn = document.getElementById('btn');
const meaning = document.getElementById('meaning');
const word = document.getElementById('word');
const lower = document.getElementById('lower')

btn.addEventListener('click', () => {
if (input.value == "") {
alert("Input field is empty");
} else {
word.innerHTML = input.value;
fetchWord(input.value);
}
})


const fetchWord = async (word) => {
meaning.innerHTML = ""
lower.innerHTML = ""
try {

const res = await fetch("https://api.dictionaryapi.dev/api/v2/entries/en/" + word)
const data = await res.json()
console.log(data)
let sound = data[0].phonetics[0].audio
const meanArr = data[0].meanings[0].definitions;

meanArr.forEach((ele, index) => {
const li = document.createElement('li')
li.innerHTML = `

${ele.definition}
`
meaning.appendChild(li)

})

if (sound) {
const soundBtn = document.createElement('button');
soundBtn.classList.add('btn','btn-dark')
soundBtn.innerHTML = `<i class="fa-solid fa-microphone"></i>`
lower.appendChild(soundBtn)

soundBtn.addEventListener('click', () => {
let audio = new Audio(sound)
audio.play(sound)
})
}


} catch (err) {
console.log(err)
meaning.innerHTML = `<p>Sorry We can not find this word!!</p>`
}


}