-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpizza.js
64 lines (53 loc) · 1.49 KB
/
pizza.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
61
62
63
64
(function() {
var toppings;
$.get("scraper/toppings.json", "json")
.done(function(data) {
toppings = data;
})
.fail(function(xhr) {
console.error('There was a problem getting the topping map!');
});
function getRandomPizza(options) {
var myPizza = {};
for (var key in options) {
var count = options[key];
myPizza[key] = [];
for (var i=0; i<count; i++) {
var choice = Math.floor(Math.random() * toppings[key].length);
myPizza[key].push(toppings[key][choice]);
}
}
return myPizza;
}
function capitalize(string) {
return string.charAt(0).toUpperCase() + string.substring(1);
}
function unpluralize(string) {
return string.substring(0, string.length - 1);
}
function format(string, count) {
string = capitalize(string);
if (count === 1) {
string = unpluralize(string);
}
return string;
}
$('#options').submit(function(e) {
e.preventDefault();
var options = {
doughs: 1,
cheeses: 1,
sauces: 1
};
$(e.currentTarget).serializeArray().forEach(function(option) {
options[option.name] = option.value;
});
var pizza = getRandomPizza(options);
var table = '<h1>Your Pizza!</h1><table class="table">';
for (var key in pizza) {
table += '<tr><td style="width: 30%">' + format(key, pizza[key].length) + '</td><td>' + pizza[key].join('<br>') + '</td></tr>';
}
table += '</table>';
$('#result').html(table);
});
})();