forked from oroborous/javascript-array-hunt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray-hunt.js
More file actions
159 lines (130 loc) · 4.51 KB
/
Copy patharray-hunt.js
File metadata and controls
159 lines (130 loc) · 4.51 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
154
155
156
157
158
159
$(document).ready(function () {
var chineseFood = ["bao", "chow-mein", "dumplings", "egg-rolls",
"fortune-cookies", "fried-rice", "gyoza", "lo-mein", "mapo-tofu",
"ramen", "shumai", "wonton-soup"];
var dinosaurs = ["ankylosaurus", "brachiosaurus", "dilophosaurus",
"pachycelphalosaurus", "pterodactyl", "stegosaurus",
"styracosaurus", "triceratops", "tyrannosaurus-rex",
"velociraptor"];
var solarSystem = ["earth", "jupiter", "luna", "mars", "mercury",
"neptune", "saturn", "sol", "uranus", "venus"];
$("#imageSet").change(showAllImages);
$("#huntButton").click(arrayHunt);
showAllImages();
function showAllImages()
{
// What image set was selected? This is the directory name
var directoryName = $("#imageSet").val();
// Based on the selection, use the correct array
var arrayOfImagesNames = getSelectedArray();
// Empty out any children from the div
var imageDiv = $("#originalArray").empty();
// Make two rows of images, half in each row
var half = arrayOfImagesNames.length / 2;
// How many images are in the current row?
var count = 0;
// The current <div class="row">
var row;
for (var fileName of arrayOfImagesNames) {
// Time to make a new row?
if (count === 0 || count >= half) {
row = $("<div>").addClass("row");
imageDiv.append(row);
count = 0;
}
// append a <figure> with the image and its caption
row.append(createImage(directoryName, fileName));
count++;
}
}
function createImage(directory, fileName)
{
// Create a div with a Bootstrap class
var col = $("<div>").addClass("col");
// Create a figure (can have a caption)
var figure = $("<figure>").addClass("figure");
col.append(figure);
// Create the image itself
var img = $("<img>");
img.attr("src", `${directory}/${fileName}.png`);
img.attr("alt", fileName);
// Add the image to the figure
figure.append(img);
// Create a caption
var caption = $(`<figcaption>${fileName}</figcaption>`)
.addClass("figure-caption text-center");
figure.append(caption);
return col;
}
function getSelectedArray()
{
// Which image set was selected?
var selection = $("#imageSet").val();
// Return the array that corresponds to
// the selected string
if (selection === "chinese")
return chineseFood;
else if (selection === "solar")
return solarSystem;
else if (selection === "dinos")
return dinosaurs;
}
function arrayHunt()
{
var myArray = getSelectedArray();
/*
Find the first and last string in the array.
Output them to td#firstLast
*/
var first = myArray[0];
var last = myArray[myArray.length-1];
$("td#firstLast").text(myArray[0] + " " +
myArray[myArray.length - 1]);
/*
Find the first string that contains an 'n'.
Output it to td#firstEnn
*/
for (var i = 0; i < myArray.length; i++)
{
if(myArray[i].includes("n"))
{
$("td#firstEnn").text(myArray[i]);
break;
}
}
/*
Find all of the strings with less than 6 characters.
Output them to td#lessThanSix
*/
var lessThanSix = "";
for (var i = 0; i < myArray.length; i++)
{
if(myArray[i].length < 6)
{
lessThanSix += myArray[i] + " ";
}
}
$("td#lessThanSix").text(lessThanSix);
/*
Find the longest string in the array.
Output it to td#longName
*/
/*
Find all of the strings that do not contain the letter 's'.
Output them to td#noEss
*/
/*
Output all of the strings, but with all of their vowels
in uppercase, to td#upperVowels
*/
/*
Output all of the strings in reverse order and separated by
' - ' to td#reverseDash
*/
//myArray.reverse();
//$("td#reverseDash").text(myArray.join("-"));
//myArray.reverse();
//$("td#reverseDash").text(myArray.join("-"));
$("td#reverseDash").text(myArray.reverse().join("-"));
}
});