-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtowns.js
53 lines (48 loc) · 1.39 KB
/
towns.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
$(document).ready(function () {
$('#btnDelete').click(deleteTown);
$('#btnAdd').click(addTown);
$('#btnShuffle').click(shuffleTowns);
$('#btnAdd').click(addTown);
});
function deleteTown() {
let townName = $('#townName').val();
$('#townName').val('');
let removed = false;
for (let option of $('#towns option')) {
if (option.textContent == townName) {
removed = true;
option.remove();
}
}
if (removed)
showMessage(townName + " deleted.");
else
showMessage(townName + " not found.");
}
function addTown() {
let townName = $('#townNameForAdd').val();
$('#townNameForAdd').val('');
$('#towns').append($('<option>').text(townName));
$('#result').text(townName + " added.");
}
function showMessage(msg) {
$('#result').text(msg).css("display", "block");
setTimeout(function () {
$('#result').hide('blind', {}, 500);
}, 3000);
}
function shuffleTowns() {
let towns = $('#towns option').toArray();
$('#towns').empty();
shuffleArray(towns);
$('#towns').append(towns);
$('#result').text("Towns shuffled.");
function shuffleArray(array) {
for (var i = array.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var oldElement = array[i];
array[i] = array[j];
array[j] = oldElement;
}
}
}