-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
121 lines (104 loc) · 3.7 KB
/
app.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
function outputDrink(drinkarray , val)
{
var Cont = 0;
var drink = drinkarray
var text = '';
console.log(drink);
let index = 1;
let ingredientArray = [];
while (drink['strIngredient' + index]) {
ingredientArray.push({name: drink['strIngredient' + index], amount: (drink['strMeasure' + index]||'').trim() ? drink['strMeasure' + index]: "A dash "});
index++;
}
text += `<br/><br/>`
text += `<b>Drink: </b><br/>${drink.strDrink}<br/><br/>`;
text += `<b>Glass: </b><br/>${drink.strGlass}<br/><br/>`;
text += '<b>Ingredients:</b></br>';
ingredientArray.forEach((ingredient) => {
text += `<li>${ingredient.amount} of ${ingredient.name}</li>`;
});
text += `</br><b>Instructions: </b></br>${drink.strInstructions}<br/>`;
$( "#result"+ val ).html(text);
}
function downloadCocktail(drink, val){
let cocktailName = drink;
console.log('Downloading details for: ', cocktailName);
var cocktail = encodeURIComponent(cocktailName);
$.ajax({
type: 'GET',
url: 'https://www.thecocktaildb.com/api/json/v1/1/search.php?s=' + cocktail,
timeout:5000,
crossDomain: true,
dataType:'json',
success: function(result, status){
if (!result.drinks || result.drinks.length <= 0) {
return;
}
// Get the first drink.
var drinks = result.drinks[0];
outputDrink(drinks, val)
return drinks;
},
error: function (errorMessage) {
console.error(errorMessage);
}
});
}
function main(){
var c;
for (c = 0; c < 4 ; c++) {
var val = 3-c;
$( "#result"+ val ).html('No drinks found !');
}
let cocktailName = $('#cocktail').val();
console.log('Downloading details for: ', cocktailName);
var cocktail = encodeURIComponent(cocktailName);
$.ajax({
type: 'GET',
url: 'https://www.thecocktaildb.com/api/json/v1/1/filter.php?i=' + cocktail,
timeout:5000,
crossDomain: true,
dataType:'json',
success: function(result, status){
if (!result.drinks || result.drinks.length <= 0) {
return;
}
// Get the first drink.
var drink1 = downloadCocktail(result.drinks[0].strDrink, 1);
var drink2 = downloadCocktail(result.drinks[1].strDrink, 2);
var drink3 = downloadCocktail(result.drinks[2].strDrink, 3);
},
error: function (errorMessage) {
console.error(errorMessage);
}
});
}
function mainAl(){
var c;
for (c = 0; c < 4 ; c++) {
var val = 3-c;
$( "#result"+ val ).html('No drinks found !');
}
let cocktailName = $('#cocktail').val();
console.log('Downloading details for: ', cocktailName);
var cocktail = encodeURIComponent(cocktailName);
$.ajax({
type: 'GET',
url: 'https://www.thecocktaildb.com/api/json/v1/1/search.php?s=' + cocktail,
timeout:5000,
crossDomain: true,
dataType:'json',
success: function(result, status){
if (!result.drinks || result.drinks.length <= 0) {
return;
}
// Get the first drink.
var drink1 = downloadCocktail(result.drinks[0].strDrink, 1);
var drink2 = downloadCocktail(result.drinks[1].strDrink, 2);
var drink3 = downloadCocktail(result.drinks[2].strDrink, 3);
},
error: function (errorMessage) {
console.error(errorMessage);
}
});
}