-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
60 lines (56 loc) · 1.58 KB
/
main.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
54
55
56
57
58
59
60
$(function() {
$('.resultSection').hide()
$('h1').addClass('in')
$('.inputSection').addClass('in')
$('.submit').addClass('in')
$('.focus').focus()
var options = []
var add_option = function() {
$('.focus').removeClass('focus')
$('.inputs').append('<input class="focus fade"></input>')
$('.focus').focus()
setTimeout(function() {
$('input.focus.fade').addClass('in')
}, 100)
}
var start_over = function() {
options = []
$('h1').removeClass('in')
$('.inputSection').removeClass('in')
$('.submit').removeClass('in')
$('h1').text('Help me choose between..')
$('.resultSection').hide()
$('.inputSection').show()
$('.inputs').html('<input class="focus" placeholder="Something"></input>')
$('.focus').focus()
$('button').text('Go!')
$('button').click(submit)
$('h1').addClass('in')
$('.inputSection').addClass('in')
$('.submit').addClass('in')
}
var submit = function() {
$('.inputSection').hide()
$('.resultSection').css('display', 'flex')
$('h1').text('The winner is:')
$('input').each(function() {
if ($(this).val() !== '') {
options.push($(this).val())
}
})
var winner = options[Math.floor(Math.random() * options.length)]
$('.resultSection').text(winner)
$('button').text('New round')
$('button').click(start_over)
setTimeout(function() {
$('.resultSection').addClass('in')
}, 300)
}
$('.addOption').click(add_option)
$('button').click(submit)
$('body').keypress(function(e) {
if (e.which == 13) {
add_option()
}
})
})