-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
86 lines (76 loc) · 2.76 KB
/
index.js
File metadata and controls
86 lines (76 loc) · 2.76 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
const endpoint = "https://pokeapi.co/api/v2"
const betterEndpoint = "https://titanic-survival-predictor-fkrl.onrender.com/predict"
const searchBox = document.getElementById("search-box");
const errorDisplay = document.getElementById("error-display");
const teamDisplay = document.getElementById("team-display");
document.getElementById("addButton")
.addEventListener("click", async function (e) {
e.preventDefault();
//
// const pokemon = await getPokemonAsync(searchBox.value);
const thingsInSearchBoxOrSomething = searchBox.value.split(',')
const maybeASuccessfulResponseIdk = await getSurvivalChanceAsync(thingsInSearchBoxOrSomething)
console.log(maybeASuccessfulResponseIdk);
displaySurvivalChance(maybeASuccessfulResponseIdk)
// addPokemonCard(pokemon)
})
function displaySurvivalChance(successResponseMaybeIWontCheck) {
const card = document.createElement("div");
card.innerHTML = `
<div>
<p>Survival Chance: ${successResponseMaybeIWontCheck.predicted_survival_chance}</p>
<p>${successResponseMaybeIWontCheck.prediction == 1 ? "Survived" : "AHAHAJ LOSER YOUR DEAD HAGAHHSADHSA H"}</p>
</div>
`
teamDisplay.appendChild(card)
}
async function getSurvivalChanceAsync(theThings) {
const requestBody = {
'Pclass': theThings[0],
'Sex': theThings[1],
'Age': theThings[2],
'SibSp': theThings[3],
'Parch': theThings[4],
'Fare': theThings[5],
'Embarked': theThings[6]
}
console.log(JSON.stringify(requestBody));
return await (await fetch(betterEndpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(requestBody)
})).json();
}
document.getElementById("clearButton")
.addEventListener("click", async function (e) {
e.preventDefault();
teamDisplay.textContent = "";
})
async function getPokemonAsync(pokemon) {
try {
const response = await fetch(`${endpoint}\\pokemon\\${pokemon}`);
if (!response.ok) {
throw new Error("Unable to fetch Pokemon.");
}
return await response.json();
} catch (error) {
errorDisplay.textContent = "Something went wrong fetching the Pokemon, are you sure its real???";
}
}
function addPokemonCard(pokemon) {
const card = document.createElement("div");
let content = `
<div>
<p>${pokemon.name}</p>
<img src="${pokemon.sprites.front_default}">
`
for (let i = 0; i < pokemon.types.length; i++) {
console.log()
content += `\n<p>${pokemon.types[i].type.name}</p>`
}
content += '\n</div>'
card.innerHTML += content
teamDisplay.appendChild(card);
}