-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
83 lines (73 loc) · 2.02 KB
/
utils.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
const fs = require('fs')
const path = require('path')
module.exports = {
getMealData,
getIngredientsData
}
function getMealData (callback) {
const filename = path.join(__dirname, 'data.json')
fs.readFile(filename, 'utf8', (err, contents) => {
if (err) {
console.error(err.message)
callback(new Error('Unable to load the file'))
return
}
try {
const parsedData = JSON.parse(contents)
callback(null, parsedData)
} catch (parseError) {
console.error(parseError)
callback(new Error('Unable to parse the data file'))
}
})
}
function getIngredientsData (callback) {
const filename = path.join(__dirname, 'ingredients.json')
fs.readFile(filename, 'utf8', (err, contents) => {
if (err) {
console.error(err.message)
callback(new Error('Unable to load the file'))
return
}
try {
const parsedData = JSON.parse(contents)
matchMealsToIngredients(dummyData)
callback(null, parsedData)
} catch (parseError) {
console.error(parseError)
callback(new Error('Unable to parse the data file'))
}
})
}
const dummyData = ['Spaghetti', 'Ground Beef', 'Tomato', 'Cheese', 'Onion']
function matchMealsToIngredients (userInput, callback) {
getMealData((err, mealData) => {
if (err) {
res.status(500).send(err.message)
return
}
mealData.meals.forEach(item => console.log(item.ingredients))
mealData.meals.forEach(meal => {
findCommonIngredients(meal.ingredients, userInput)
})
})
}
function findCommonIngredients (mealData, userInput) {
// Loop for array1
for (let i = 0; i < mealData.length; i++) {
// Loop for array2
for (let j = 0; j < userInput.length; j++) {
// Compare the element of each and
// every element from both of the
// arrays
if (mealData[i] === userInput[j]) {
// Return if common element found
console.log('true')
return true
}
}
}
// Return if no common element exist
console.log('false')
return false
}