<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<main class="main">
<h1 class="main-heading">Сейчас пьет:</h1>
<h3 class="main-player js-playerNow"></h3>
<div class="input-group">
<label for="repeat">Повторять игрока</label>
<input class="repeat-chechbox js-repeat" type="checkbox" id="repeat">
</div>
<div class="input-group">
<input class="input-add-player js-inputAdd" type="text" placeholder="Введите имя игрока">
<button class="btn btn-add-player js-addPlayer" type="button">Добавить игрока</button>
</div>
<button class="btn btn-role js-doRole">Крутить</button>
<ul class="players-list">
<li class="players-item"></li>
</ul>
</main>
</body>
</html>
/* common */
.btn {
padding: 10px 15px;
background-color: #000;
color: #fff;
font-weight: bold;
border-radius: 10px;
border: 2px solid transparent;
cursor: pointer;
transition: all .3s;
}
/* main */
.main {
position: relative;
text-align: center;
}
.input-group {
margin: 10px 0;
}
.input-group label {
cursor: pointer;
}
.main-btn:hover {
background-color: transparent;
color: #000;
border: 2px solid #000;
}
.input-add-player {
margin-right: 10px;
padding: 10px;
display: inline-block;
border: 1px solid #000;
border-radius: 5px;
}
.input-add-player:focus {outline: none;}
.btn-add-player {
padding: 6px 10px;
background-color: transparent;
color: #000;
border: 2px solid #000;
}
.btn-add-player:hover {
background-color: #000;
color: #fff;
border: 2px solid transparent;
}
.btn-role:focus {outline: none;}
.players-list {
position: absolute;
left: 0;
top: 50%;
}
window.onload = function(){
var playerNow = document.querySelector('.js-playerNow'),
role = document.querySelector('.js-doRole'),
repeat = document.querySelector('.js-repeat'),
inputAdd = document.querySelector('.js-inputAdd'),
addPlayerBtn = document.querySelector('.js-addPlayer'),
playersList = document.querySelector('.players-list');
// const players = ['Ваня', 'Валера', 'Влад', 'Бодя'];
var players = [];
mainFunc(); // display player name for start screen
role.addEventListener('click', mainFunc);
addPlayerBtn.addEventListener('click', addNewPlayer);
// add to list by clicking on enter
inputAdd.addEventListener('keyup', function(event){
if(event.keyCode == 13){
addNewPlayer();
}
});
function mainFunc() {
var rand = Math.floor(Math.random() * players.length);
var res = players[rand];
// if repeat player is true
if ( repeat.checked ) {
playerNow.innerHTML = res;
console.log('Repeat: ' + res);
}
// if repeat player is false
else {
if ( playerNow.innerHTML == res ) {
mainFunc(); // get new player name if this name is equal playerNow
console.log('No repeat: ' + playerNow.innerHTML);
}
else {
playerNow.innerHTML = res; // get player name if this name is not equal playerNow
console.log('No repeat: ' + playerNow.innerHTML);
}
}
}
function addNewPlayer() {
players.push(inputAdd.value);
inputAdd.value = '';
console.log(players);
renderNewPlayer();
}
function renderNewPlayer() {
for (var i = 0; i < players.length; i++) {
playersList.insertAdjacentHTML('afterBegin', `<li>${players[i]}</li>`);
}
}
}
Output
This bin was created anonymously and its free preview time has expired (learn why). — Get a free unrestricted account
Dismiss xKeyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |