Skip to content

Instantly share code, notes, and snippets.

@autioch
Created November 27, 2015 22:34
Show Gist options
  • Save autioch/19872e45ee6d83ccb32a to your computer and use it in GitHub Desktop.
Save autioch/19872e45ee6d83ccb32a to your computer and use it in GitHub Desktop.
Charades - JS example.
<!DOCTYPE html>
<html>
<head>
<title>Kalambury (filmy)</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<style type="text/css">
body {
font-family: Verdana;
color: #333;
}
button,
input {
width: 10em;
margin: 0.5em;
padding: 0.25em 0.5em;
font: 16px Verdana;
border: solid 1px #aaa;
}
</style>
</head>
<body>
<section>
<button class="js-random">Losuj</button>
<span class="js-result"></span>
</section>
<section>
<button class="js-clear">Wyczyść</button>
</section>
<section>
<button class="js-add">Dodaj</button>
<input class="js-new" type="text" value="">
</section>
<section>
<span>Zostało:</span>
<span class="js-total"></span>
</section>
<script type="text/javascript">
passwords = [
"300",
"50 pierwszych randek",
"Adwokat diabła",
"American beauty",
"Dzień Niepodległości",
"Galerianki",
"Niepamięć",
"Obcy Przebudzenie",
"Od zmierzchu do świtu",
"Odyseja kosmiczna",
"Polowanie na Czerwony Październik",
"Star Wars",
"Zaginiony świat",
"Zmierzch",
"Anakonda",
"Czterech jeżdżców apokalipsy",
"Cztery wesela i pogrzeb",
"Duma i uprzedzenie",
"Epoka lodowcowa",
"Imię róży",
"Iron man",
"Król lew",
"Kwiat pustyni",
"Lśnienie",
"Piąty element",
"Pod mocnym aniołem",
"Podziemny krąg",
"Powrót do przyszłości",
"Raport mniejszości",
"Sami swoi",
"Seksmisja",
"Skrzydełko, czy nóżka",
"Spiderman",
"Stowarzyszenie umarłych poetow",
"Upior w operze",
"Złoto dla zuchwałych"
];
var el = ['random', 'result', 'clear', 'add', 'new', 'total'].reduce(function(obj, item) {
obj[item] = document.querySelector(`.js-${item}`);
return obj;
}, {});
function updateTotal() {
el.total.textContent = passwords.length;
}
el.random.addEventListener('click', function() {
el.result.textContent = passwords.splice(Math.round(Math.random() * passwords.length), 1)[0];
updateTotal();
});
el.clear.addEventListener('click', function() {
el.result.textContent = '';
});
el.add.addEventListener('click', function() {
passwords.push(el.new.value);
el.new.value = '';
updateTotal();
});
window.save = JSON.stringify.bind(null, passwords);
updateTotal();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment