-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomRecipeGenerator.js
More file actions
72 lines (59 loc) · 2.19 KB
/
RandomRecipeGenerator.js
File metadata and controls
72 lines (59 loc) · 2.19 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
// ============================
// Allow for a cached result
// ============================
var csvRows = [];
function buildTable(csvRows) {
var $table = $("<table cellpadding=\"2\" cellspacing=\"0\"></table>");
$table = csvRows.reduce(function ($table, csvRow) {
var csvRowCells = csvRow.split(",");
// Base table row
var $tr = $("<tr>/tr>");
// For each item in the array, append a new column to the html table
$tr = csvRowCells.reduce(function ($tr, csvRowCell) {
var lastIndex = csvRowCells.indexOf(csvRowCells[csvRowCells.length-1]);
var tempStr = csvRowCells[lastIndex];
var ingredientCount = parseInt(tempStr.replace('\r',''));
if (csvRowCells.indexOf(csvRowCell) == 0) {
return $tr.append($("<b><tr>/</tr></b>").text(csvRowCell));
}
if (csvRowCells.indexOf(csvRowCell) == 1){
$tr.append($("<br><br><b><tr>Ingredients:</tr></br></b></br>"));
$tr.append($("<tr>/</tr>").text(csvRowCell));
return $tr;
} else if (csvRowCells.indexOf(csvRowCell) == ingredientCount+1){
$tr.append($("<br><br><b><tr>Directions:</tr></br></b></br>"));
$tr.append($("<tr>/</tr>").text(csvRowCell));
return $tr;
}
else {
return $tr.append($("<tr>/</tr>").text(csvRowCell));
}
}, $tr);
return $table.append($tr);
}, $table);
return $table;
}
// Randomly select one row from the .csv as an array and build a table with it.
function fillContainerWithTable(csvRows, $container) {
var randomRow = [csvRows[Math.floor(Math.random() * csvRows.length)]];
var $table = buildTable(randomRow);
$container.append($table);
}
function generator() {
// link to the .csv in our bucket
var url = "https://akramvocs383bucket.s3.amazonaws.com/Recipes.csv";
var $container = $("#recipe");
$container.empty();
// If we have the data locally already just use it.
if (csvRows.length !== 0) {
console.log("using local data...");
fillContainerWithTable(csvRows, $container);
return;
}
console.log("fetching remote data...");
$.get(url, function (data) {
csvRows = data.split("\n");
fillContainerWithTable(csvRows, $container);
});
}
// ============================