-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscript.js
151 lines (135 loc) · 5 KB
/
script.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
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
const foodButton = document.getElementById('random-food-button');
const recipeContent = document.getElementById('recipe-display');
const recipeInstructions = document.getElementById('recipe-instructions');
const recipePicture = document.getElementById('recipe-picture');
const ingredients = document.querySelector('ul');
const movieTitle = document.getElementById('movie-display');
const moviePicture = document.getElementById('movie-picture');
const movieDescription = document.getElementById('movie-description');
const movieExtra = document.getElementById('movie-extra');
const movieSectionBorder = document.querySelector('.movie-container');
const foodSectionBorder = document.querySelector('.food-container');
const buttonContainer = document.getElementById('btn-cont');
const infoTxt = document.querySelector('.intro');
const pluss = document.querySelector('.pluss');
const foodSectionContainers = document.querySelector('.food-container');
const movieSectionContainers = document.querySelector('.movie-container');
foodButton.addEventListener('click', function() {
foodApi();
movieApi();
movieSectionBorder.classList.add('container-border');
foodSectionBorder.classList.add('container-border');
buttonContainer.classList.remove('button-container');
buttonContainer.querySelector('.intro').remove();
pluss.removeAttribute('style');
foodSectionContainers.removeAttribute('style');
movieSectionContainers.removeAttribute('style');
});
const a = document.getElementById('foodSelector');
a.addEventListener(
'change',
function() {
console.log(this.value);
},
false
);
const b = document.getElementById('movieSelector');
b.addEventListener(
'change',
function() {
console.log(this.value);
},
false
);
function foodApi() {
const url = 'https://www.themealdb.com/api/json/v1/1/random.php';
const xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
const recipeObj = JSON.parse(xhr.responseText);
//recipe name
const recipeName = recipeObj.meals[0].strMeal;
//recipe instructions
const recipeInstructionsContent = recipeObj.meals[0].strInstructions;
//recipe picture
const recipePic = recipeObj.meals[0].strMealThumb;
if (a.value === recipeObj.meals[0].strCategory) {
console.log(true);
} else if (a.value === '0') {
alert('choose option');
} else {
foodApi();
}
recipeContent.textContent = recipeName;
recipeInstructions.textContent = recipeInstructionsContent;
recipePicture.src = recipePic;
console.log(recipeObj.meals[0].strCategory);
console.log(a.value);
//recipe ingredients
const ingGroup = recipeObj.meals[0];
const ingredient = Object.entries(ingGroup).slice(9, 29);
const measure = Object.entries(ingGroup).slice(29, 49);
const ingredientArr = [];
const measureArr = [];
const finArr = [];
while (ingredients.firstChild) {
ingredients.removeChild(ingredients.firstChild);
}
ingredient.map(x => {
if (x[1] !== '') {
if (x[1] !== null) {
ingredientArr.push(x[1]);
}
}
});
measure.map(x => {
if (x[1] !== '') {
if (x[1] !== null) {
measureArr.push(x[1]);
}
}
});
for (let i = 0; i < ingredientArr.length; i++) {
finArr.push(ingredientArr[i] + ': ' + measureArr[i]);
}
finArr.map(x => {
const ingredientsLi = document.createElement('LI');
const ingredientText = document.createTextNode(x);
ingredientsLi.appendChild(ingredientText);
ingredients.appendChild(ingredientsLi);
});
}
};
xhr.open('GET', url, true);
xhr.send();
}
const movieApi = () => {
const randomNumber = Math.floor(Math.random() * 100 + 1);
const url = `https://api.themoviedb.org/3/movie/top_rated?api_key=e0cf38b285b4edbae61d3cb5b6086614&language=en-US&page=${randomNumber}`;
const xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
const movieObj = JSON.parse(xhr.responseText);
console.log(movieObj.results[0]);
console.log(typeof movieObj.results[0].genre_ids[0]);
console.log(typeof parseInt(b.value));
console.log(movieObj.results[0].genre_ids.includes(10749));
if (movieObj.results[0].genre_ids.includes(parseInt(b.value))) {
console.log(true);
} else if (b.value === '0') {
alert('choose option');
} else {
movieApi();
}
const movieTitleEl = movieObj.results[0].original_title;
const moviePictureEl = movieObj.results[0].poster_path;
const moviePath = `http://image.tmdb.org/t/p/w500/${moviePictureEl}`;
const movieDescriptionEl = movieObj.results[0].overview;
movieTitle.textContent = movieTitleEl;
movieDescription.textContent = movieDescriptionEl;
moviePicture.src = moviePath;
}
};
xhr.open('GET', url, true);
xhr.send();
};