-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray-method.js
More file actions
64 lines (55 loc) · 1.81 KB
/
array-method.js
File metadata and controls
64 lines (55 loc) · 1.81 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
// Method Push
const lunches = [];
function addLunchToEnd(arr, str) {
arr.push(str);
console.log(`${str} added to the end of the lunch menu.`);
return arr;
}
addLunchToEnd(lunches, "Tacos");
// Method unshift
function addLunchToStart (arr, str) {
arr.unshift(str);
console.log(`${str} added to the start of the lunch menu.`);
return arr;
}
addLunchToStart(lunches, "Sushi");
// Method pop
function removeLastLunch (arr) {
if( arr.length === 0 ) {
console.log("No lunches to remove.");
} else {
console.log(`${arr[arr.length - 1]} removed from the end of the lunch menu.`);
}
arr.pop();
return arr;
}
removeLastLunch(["Stew", "Soup", "Toast" ]);
// Method shift
function removeFirstLunch (arr) {
if( arr.length === 0 ) {
console.log("No lunches to remove.");
} else {
console.log(`${arr[0]} removed from the start of the lunch menu.`);
}
arr.shift();
return arr;
}
// Array Randomly
removeFirstLunch(["Salad", "Eggs", "Cheese"]);
function getRandomLunch (arr) {
if( arr.length === 0 ) {
console.log("No lunches available.");
}
const randomLunches = Math.floor(Math.random() * (arr.length - 0) + 0);
console.log(`Randomly selected lunch: ${arr[randomLunches]}`);
}
getRandomLunch(["Salad", "Eggs", "Cheese"]);
// Method Join
function showLunchMenu (arr) {
if( arr.length === 0 ) {
console.log("The menu is empty.");
} else {
console.log(`Menu items: ${arr.join(", ")}`);
}
}
showLunchMenu(["Greens", "Corns", "Beans"])