Skip to content

Instantly share code, notes, and snippets.

@janchor
Created May 20, 2017 01:33
Show Gist options
  • Save janchor/5063f56da41d3e32c825ff154c6bd3be to your computer and use it in GitHub Desktop.
Save janchor/5063f56da41d3e32c825ff154c6bd3be to your computer and use it in GitHub Desktop.
Stackoverflow example: how to store javascript in an array?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Stackoverflow example</title>
<script>
var y=0
adventure = new Array
adventure[y++]={
//notes:
d:"Adventure zero, choose an equipment. Bug: for some reason the script crashes when you click the Adventure button",
i:"<img src='cave_entrance.jpg' alt='cave entrance' /><figcaption></figcaption>",
b:"<button type=&quot;button&quot; onclick=&quot;window.alert&#40;'Gotten'&#41;&quot;>Get equipment</button>"
}
adventure[y++]={
//notes:
d:"Adventure 1",
i:"<img src='cave_entrance.jpg' alt='cave entrance' /><figcaption></figcaption>",
b:"<button type=&quot;button&quot; onclick=&quot;window.alert('You gain 100 gold')&quot;>Gold</button>"
}
adventure[y++]={
//notes:
d:"Adventure 2",
i:"<img src='cave_entrance.jpg' alt='cave entrance' /><figcaption></figcaption>",
b:"<button type=&quot;button&quot; onclick=&quot;window.alert('You gain 200 gold')&quot;>Gold</button>"
}
adventure[y++]={
//notes:
d:"Adventure 3",
i:"<img src='cave_entrance.jpg' alt='cave entrance' /><figcaption></figcaption>",
b:"<button type=&quot;button&quot; onclick=&quot;window.alert('You gain 300 gold')&quot;>Gold</button>"
}
adventure[y++]={
d:"Adventure 4",
i:"<img src='cave_entrance.jpg' alt='cave entrance' /><figcaption></figcaption>",
b:"<button type=&quot;button&quot; onclick=&quot;window.alert('You gain 400 gold')&quot;>Gold</button>"
}
</script>
</head>
<body>
<main>
<figure id="picture">
<img src="logo.png" alt="logo" />
<figcaption></figcaption> <!-- is this necessary? Who knows. -->
</figure>
<p id="description">RogueMine feels like a rogue-like as you wander around through randomly generated encounters, fighting monsters and mining resources in order to build a better robot.</p>
<button type="button" onclick="highLevel = y-1; while (y >highLevel){ y = Math.round(Math.random() * 5)};document.getElementById('description').innerHTML = adventure[y].d;document.getElementById('picture').innerHTML = adventure[y].i;document.getElementById('buttons').innerHTML = adventure[y].b">Adventure</button>
<div id="buttons">
<button id="choice3" type="button" onclick="window.alert('Credits: James. Otis. The Internet.')">Credits</button>
</div>
</main>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment