Public
Script
October 23, 2023
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
import { fetch } from "https://esm.town/v/std/fetch";
export async function spellCheckTest(
req: express.Request,
res: express.Response,
) {
let response = await fetch("https://www.dnd5eapi.co/api/spells");
let data = await response.json();
let countOfSpells = data["count"];
let randomSpellNumber = Math.floor(Math.random() * countOfSpells);
let randomSpell = data["results"][randomSpellNumber];
let response2 = await fetch(
`https://www.dnd5eapi.co/api/spells/${randomSpell["index"]}`,
);
let data2 = await response2.json();
let spell = data2;
let classes = "";
for (const c in spell["classes"]) {
classes += spell["classes"][c]["name"] + " ";
}
return res.send(`
<div style="text-align: center">
<h3>Material: ${spell["material"] || "No materials required!"}</h3><br>
<h3>Level: ${spell["level"]}</h3>
<h3>Classes: ${classes}</h3>
<h3>Range: ${spell["range"]}</h3>
<h3>Concentration: ${spell["concentration"]}</h3>
<h3>Components: ${spell["components"]}</h3>
<h3>School: ${spell["school"]["name"]}</h3>
<h3>Description: ${spell["desc"][0]}</h3>
<button onclick="myFunction()">Reveal Spell</button><br>
<div id="myDIV"></div>
<button type="button" onClick="window.location.reload();">Check a new Spell</button>
</div>
<script>
function myFunction() {
var x = document.getElementById("myDIV");
if (x.innerHTML === "") {
x.innerHTML = "${spell["name"]}";
} else {
x.innerHTML = "";
}
}
</script>
`);
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
October 23, 2023