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
<svg width="92" height="81" xmlns="http://www.w3.org/2000/svg" aria-labelledby="title-monster5"><title id="title-monster5">Monster 5</title><g fill-rule="nonzero" fill="#34ac52"><circle cx="56.662" cy="8.302" r="2.287"/><circle cx="29.405" cy="8.302" r="2.287"/><path d="M91.746 22.946c-.449-1.198-3.191-3.454-5.092-4.254a4.717 4.717 0 0 0-1.727-2.777c-7.371-4.193-14.234 5.973-9.531 15.376 3.68 7.36-3.639 10.173-8.621 11.013-1.816-9.788-9.121-23.191-12.188-27.711 3.24 2.097 10.865-2.668 8.387-9.149-2.479-6.481-8.768-5.528-12.01-4.384-3.238 1.144-4.192 9.531-2.668 12.771l.068.143c-.473.297-.979.772-1.551 1.408 1.006.218 1.834 1.374 2.524 2.023-.613.378-1.402.614-1.916 1.051 1.59.935 1.398 1.578-.463 2.548 1.069-.098 1.799.38 2.76.493.041.792-1.094 1.467-1.604 2.019 1.117.332 2.656-.006 3.773.672-1.668.775-2.488 2.013-3.758 3.382.994.194 2.172-.295 3.244-.199-.021.552-.252.483-.549.885-.912 1.228-2.283 1.813-2.768 3.449.934-.366 5.568-.063 6.262.839a87.3 87.3 0 0 0-5.193 3.437c1.816.072 3.406-.066 4.312 1.915.408-.113.766-.077 1.186-.129-2.072.398-3.67 1.706-5.631 2.554 1.734-.074 5.123 1.443 6.439 2.511-2.004.305-4.797.465-6.562 1.397 1.418.173 2.719 1.836 3.732 2.699 0 .003.002.004.004.005-1.619 1.08-3.83 1.727-6.789 1.779-2.275.04-4.191-.271-5.795-.862 1.246-1.025 2.66-2.041 3.563-3.121-.778-.408-1.807-.216-2.666-.493-1.806-.582-2.757-.755-4.795-.741 1.526-.48 4.253-1.855 4.939-3.434-1.185-.534-2.521-.823-3.655-1.462-.675-.381-1.25-1.026-1.946-1.285 1.846-.628 3.402-2.271 5.356-2.483-.684-1.294-3.065-2.241-4.419-2.802 1.446-.689 2.874-1.442 4.299-2.131-1.346-1.481-3.098-2.973-4.91-3.94 1.2-.759 2.782-1.435 3.654-2.544-1.115-.303-2.162-.901-3.295-1.166-.03-1.288 2.688-2.146 3.392-3.12a18.632 18.632 0 0 0-2.811-2.326l.15-.311c2.951-.594 2.302-1.142 1.306-2.137 2.908-4.033-.854-10.716-5.709-13.294-2.487-1.321-9.149-2.669-10.865 4.384-1.715 7.052 4.766 12.58 9.912 10.483-.963 2.34-4.807 11.832-7.014 20.588-1.941-6.092-5.445-12.73-10.747-14.246C6.646 20.235-6.19 32.562 3.468 43.618c2.687 2.901 6.554 2.35 7.531-.81 1.626.247 3.913.309 5.092-.207 1.355-.593.509-1.44-.338-.848-.712.5-2.924.519-4.573.21.046-.356.061-.736.04-1.142-.042-.831-.266-1.535-.614-2.111 1.676-.986 2.646-2.16 3.368-2.802.763-.679.339-1.355-.423-.431-.57.691-2.154 1.852-3.438 2.582-.786-.838-1.894-1.265-3.005-1.275.238-1.539.918-3.293 1.444-4.039.679-.96.508-1.243-.423-.48-.779.637-1.557 2.575-1.792 4.581-1.343.226-2.58 1.08-3.121 2.578-4.067-6.679 3.121-15.406 9.766-14.106 5.296 1.036 8.781 10.592 10.364 16.745-.213 1.344-.354 2.619-.399 3.789-.175 4.452 1.975 19.885 18.271 22.205-.418 3.382-.524 8.158.217 10.719.859 2.966.836.51.667 0-.745-2.23-.953-6.587-.164-10.627.932.107 1.906.177 2.928.195 1.417.028 2.74-.012 3.978-.106-.398 3.37-.488 8.022.24 10.538.859 2.966.836.51.666 0-.742-2.227-.953-6.57-.168-10.604 15.266-1.511 17.062-12.281 17.586-20.985.027-.461.031-.94.014-1.438 10.215-1.134 14.297-7.725 10.375-15.094-4.193-7.878.127-12.2 2.16-12.581-.949.317-1.637 3.087.705 4.672-.434 1.477-.52 3.354-.281 4.605.34 1.779.547.763.443-.254-.084-.814.064-2.399.604-3.929.316.143.674.268 1.072.37.973.25 1.814.153 2.496-.179.451 1.114.891 2.362.891 3.314 0 1.355.848 1.44.764-.169-.059-1.12-.488-2.527-.914-3.651.855-.78 1.307-2.019 1.264-3.298 1.383.693 3.861 2.184 4.225 3.762.422 1.832 1.27.985.76-.371zM29.405 12.877a4.575 4.575 0 1 1 0-9.15 4.575 4.575 0 0 1 0 9.15zm27.257-9.149a4.574 4.574 0 0 1 4.574 4.575 4.574 4.574 0 1 1-9.148 0 4.575 4.575 0 0 1 4.574-4.575z"/></g></svg>
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