Skip to content

Instantly share code, notes, and snippets.

@angusgrant
Last active November 7, 2022 19:53
Show Gist options
  • Save angusgrant/f05c09274f50a17f9d4a018b04f79513 to your computer and use it in GitHub Desktop.
Save angusgrant/f05c09274f50a17f9d4a018b04f79513 to your computer and use it in GitHub Desktop.
vanilla JS week 4 exersise 1
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Monsters!</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style type="text/css">
body {
margin: 0 auto;
max-width: 40em;
width: 88%;
}
/**
* A simple grid layout
*/
.row {
display: grid;
grid-template-columns: auto auto auto;
text-align: center;
}
.grid {
min-height: 6em;
padding: 1em;
}
/**
* Make sure images scale
*/
img {
height: auto;
max-width: 100%;
}
</style>
</head>
<body>
<h1>Monsters!</h1>
<div id="app"></div>
<footer>
<hr>
<p class="text-small text-muted">Icons by <a href="https://thenounproject.com/term/door/311732/">Jamie Dickinson</a>, <a href="https://thenounproject.com/term/monster/184225/">Nicky Knicky</a>, <a href="https://thenounproject.com/term/monster/1510400/">Alvaro Cabrera</a>, <a href="https://thenounproject.com/term/monster/28460/">Eliricon</a>, <a href="https://thenounproject.com/term/monster/82823/">April Yang</a>, <a href="https://thenounproject.com/term/monster/1062009/">tk66</a>, <a href="https://thenounproject.com/term/monster/24990/">Alex WaZa</a>, <a href="https://thenounproject.com/term/monster/37212/">Husein Aziz</a>, <a href="https://thenounproject.com/term/monster/2236082">iconcheese</a>, and <a href="https://thenounproject.com/term/socks/38451/">Yazmin Alanis</a>.</p>
</footer>
<script>
// The monsters and socks if I wanted to spend longer on this problem I would have converted it to an array of objects
let monsters = [
'<img src="monster1.svg" alt="yellow monster small one eye">',
'<img src="monster2.svg" alt="yellow monster large one eye">',
'<img src="monster3.svg" alt="green monster two eyes looks like a venus fly trap">',
'<img src="monster4.svg" alt="red monster four arms like a tree truck">',
'<img src="monster5.svg" alt="green monster small one eye">',
'<img src="monster6.svg" alt="green monster large kinda triangle shaped">',
'<img src="monster7.svg" alt="purple monster with one eye and two tentacles">',
'<img src="monster8.svg" alt="purple monster small one eye no arms!">',
'<img src="monster9.svg" alt="blue monster monster bug like with wings">',
'<img src="monster10.svg" alt="blue monster with eyes on stalks">',
'<img src="monster11.svg" alt="grey monster looks a bit like chewbacca from star wars">',
'<img src="sock.svg" alt="this is just a couple of silver / white socks">'
];
//put array of images in a random order and update the monsters var!
monsters = shuffle(monsters);
const siteAppendPoint = document.querySelector('#app');
siteAppendPoint.innerHTML = `<div class="row">
${monsters.map(function(monster) {
return `<div class="grid">${monster}</div>`
}).join('')};
</div>`
function shuffle (array) {
let currentIndex = array.length;
let temporaryValue, randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
// Your code goes here...
</script>
</body>
</html>
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment