Skip to content

Instantly share code, notes, and snippets.

@sirgalleto
Last active September 13, 2016 21:41
Show Gist options
  • Save sirgalleto/62e646e7b4fb885eea1b502de907170f to your computer and use it in GitHub Desktop.
Save sirgalleto/62e646e7b4fb885eea1b502de907170f to your computer and use it in GitHub Desktop.
Cat clicker for udacity javascript patterns course
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Cat clicker</title>
</head>
<body id="body">
<script src="index.js"></script>
</body>
</html>
var body = document.getElementById('body');
var catContainer = document.createElement('div');
var cats = [
{
name: 'Cattie',
counts: 0,
img: 'http://d39kbiy71leyho.cloudfront.net/wp-content/uploads/2016/05/09170020/cats-politics-TN.jpg'
},
{
name: 'Gut',
counts: 0,
img: 'https://www.royalcanin.com/~/media/Royal-Canin/Product-Categories/cat-adult-landing-hero.ashx'
},
{
name: 'Cute',
counts: 0,
img: 'https://d1wn0q81ehzw6k.cloudfront.net/additional/thul/media/4e34feee0acdc38a?w=400&h=400'
},
{
name: 'Pil',
counts: 0,
img: 'http://metrazhi.com/wp-content/uploads/2016/06/tttytyty.jpg'
},
{
name: 'Damn',
counts: 0,
img: 'http://2.bp.blogspot.com/-pATX0YgNSFs/VP-82AQKcuI/AAAAAAAALSU/Vet9e7Qsjjw/s1600/Cat-hd-wallpapers.jpg'
},
];
var list = document.createElement('ul');
cats.forEach((cat) => {
var li = document.createElement('li');
li.innerHTML = cat.name;
list.appendChild(li);
li.addEventListener('click', () => {
showCat(cat);
});
});
body.appendChild(list);
body.appendChild(catContainer);
function showCat(cat) {
removeCurrentCat();
var title = document.createElement('h1');
title.innerHTML = cat.name;
var image = new Image(500, 500);
image.src = cat.img;
var counts = document.createElement('p');
counts.innerHTML = cat.counts;
catContainer.appendChild(title);
catContainer.appendChild(image);
catContainer.appendChild(counts);
image.addEventListener('click', (event) => {
counts.innerHTML = ++cat.counts;
});
}
function removeCurrentCat() {
while(catContainer.firstChild){
catContainer.removeChild(catContainer.firstChild);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment