-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
153 lines (102 loc) · 4.46 KB
/
script.js
File metadata and controls
153 lines (102 loc) · 4.46 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// // let random = () => {
// // return Math.floor(Math.random() * 11) * 100;
// // };
// let ingredient;
// $(".submitRecipe").click(function(){
// ingredient = $(".Ingredients").val();
// });
// function letSee(){
// let request = new XMLHttpRequest();
// // previous api link that stopped working: currently using Abrar's sample
// // https://api.spoonacular.com/recipes/findByIngredients?apiKey=1fe4c16603534b98934d2702e0e711c4&ingredients=apples,+flour,+sugar&number=2
// // url = "https://api.spoonacular.com/recipes/findByIngredients?apiKey=1fe4c16603534b98934d2702e0e711c4&ingredients=" + ingredient +",+flour,+sugar&number=2";
// Api that we are using
// url = "https://api.spoonacular.com/recipes/findByIngredients?apiKey=1fe4c16603534b98934d2702e0e711c4&ingredients=apples,+flour,+sugar&number=2";
// request.open("GET", url, true);
// request.onload = function() {
// let data = JSON.parse(this.response);
// if (request.status >= 200 && request.status < 400) {
// $("#result").text(data.title);
// }
// };
// request.send();
// }
// // let counter = 0;
// // fetch(url)
// // .then((response) => response.json())
// // .then((data) => {
// // for (pokemon of data.results) {
// // if (counter % 4 == 0) {
// // row = document.createElement("div");
// // row.className = "row";
// // $("#pokeList").append(row);
// // }
// // let avatar = document.createElement("div");
// // avatar.className = "col-3 pokemon";
// // fetch(pokemon.url)
// // .then((response) => response.json())
// // .then((pokemon) => {
// // let text = document.createElement("h1");
// // text.textContent = pokemon.name;
// // let img = document.createElement("img");
// // img.src = pokemon.sprites.front_default;
// // avatar.onclick = () => {
// // img.src = pokemon.sprites.front_shiny;
// // }
// // avatar.appendChild(text);
// // avatar.appendChild(img);
// // row.appendChild(avatar);
// // counter++;
// // });
// // }
// // });
// // };
// // main();
// update this
let url = "https://api.spoonacular.com/recipes/random?apiKey=a1dc2f1731664ae1b0fa347c34c67223&tags=vegan";
//let url1 = "https://api.spoonacular.com/recipes/findByIngredients?apiKey=a1dc2f1731664ae1b0fa347c34c67223&ingredients=orange,+flour,+sugar&number=1";
function letSee(){
// update the ing1/2/3 name
let ing1 = $(".Ingredient1").val();
let ing2 = $(".Ingredient2").val();
let ing3 = $(".Ingredient3").val();
let temp = Math.floor((Math.random()*3)+1); // Used for showcase - Not needed for original.
//let url2 = "https://api.spoonacular.com/recipes/findByIngredients?apiKey=a1dc2f1731664ae1b0fa347c34c67223&ingredients="+ing1+","+ing2+","+ing3+"&number=1; - ORIGINAL
let url2 = "https://api.spoonacular.com/recipes/findByIngredients?apiKey=832e510dd89744d781b5896ecfcc3a56&ingredients="+ing1+","+ing2+","+ing3+"&number="+temp;
console.log(url2);
let request = new XMLHttpRequest();
request.open("GET", url2, true);
request.onload = function() { //Once we recieve the response do this vvvv
let data = JSON.parse(this.response);
if (request.status >= 200 && request.status < 400) {
// console.log(data); - Educational Purposes
resImg = data[0].image;
resTitle = data[0].title;
resId = data[0].id;
console.log(resId);
console.log(resTitle);
$(".result").text(resTitle)
$('.playerChoice').attr('src',resImg);
//OPTIONAL EXTENTIAL - BUT SEEMS USEFUL
let urlN = "https://api.spoonacular.com/recipes/"+resId+"/information?apiKey=a1dc2f1731664ae1b0fa347c34c67223";
let newRequest = new XMLHttpRequest();
newRequest.open("GET", urlN, true);
newRequest.onload = function() { //Once we recieve the response do this vvvv
let newData = JSON.parse(this.response);
if (newRequest.status >= 200 && newRequest.status < 400) {
let resIns = newData.instructions;
console.log(resIns);
if(resIns == null){
$(".instro").text("No instructions are available at this time");
}
else{
$(".instro").text(resIns);
}
}
};
newRequest.send();
//$("#advice").text(data.recipes[0].instructions);
}
};
request.send();
}