-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
34 lines (25 loc) · 1010 Bytes
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
var translateButton = document.querySelector("#translate-btn");
translateButton.addEventListener("click", buttonClickHandler)
// read input
var translateInput = document.querySelector("#translate-input");
// show output
var translateOutput = document.querySelector("#translate-output");
// var url = "https://lessonfourapi.tanaypratap.repl.co/translate/yoda.json"
var url = "https://api.funtranslations.com/translate/dolan.json"
// REPL URL: https://repl.it/@tanaypratap/lessonfourapi
function buttonClickHandler(event) {
console.log("button clicked");
var input = translateInput.value;
var finalURL = constructURL(input);
console.log(finalURL);
fetch(finalURL)
.then(response => response.json())
.then(json => {
translateOutput.innerText = json.contents.translated;
})
.catch(() => alert("some error occured"))
}
function constructURL(inputText) {
var encodedURI = encodeURI(inputText);
return `${url}?text=${encodedURI}`;
}