-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
91 lines (67 loc) · 3.57 KB
/
script.js
File metadata and controls
91 lines (67 loc) · 3.57 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
console.log("Hello World!");
// 1. Create an array called favoriteFoods with at least 6 foods you love.
let favoriteFoods = ["Sushi", "Pasta", "Sandwiches", "Steak", "Asparagus", "Ice Cream"];
// 2. Loop through the list and print: "One of my favorite foods is ______."
function printFavoriteFoods() {
for (let i = 0; i < favoriteFoods.length; i++) {
console.log("One of my favorite foods is " + favoriteFoods[i]);
}
}
printFavoriteFoods();
// 3. Print out the rating for each food with a ranking like:
// "My #1 favorite food is Ramen" (copy/paste for all items)
// "My #2 favorite food is Sushi"
// ...etc.
for (let i = 0; i < favoriteFoods.length; i++) {
console.log("My #" + (i + 1) + " favorite food is " + favoriteFoods[i]);
}
// 4a. Create a function printFoodRecommendation(foodName) that prints out the following for the foodName provided
// "Have you ever tried ____?"
// "I always recommend ____ to friends."
// "Trust me — ____ is delicious."
function printFoodRecommendation(foodName) {
console.log("Have you ever tried " + foodName + "?");
console.log("I always recommend " + foodName + " to friends.");
console.log("Trust me — " + foodName + " is delicious.");
}
// 4b. Call the function at least 3 times
printFoodRecommendation("Sushi");
printFoodRecommendation("Pasta");
printFoodRecommendation("Steak");
// Here's a list of 50 friends' favorite foods:
let friendFavorites = [
"Pizza", "Sushi", "Pasta", "Falafel", "Burgers", "Ramen", "Pad Thai", "Curry", "Pho", "Nachos", "Gnocchi", "Donuts", "Steak", "Lasagna", "Biryani", "Tacos", "Croissant", "Churros", "Fried Rice", "Shawarma", "Miso Soup", "BBQ Ribs", "Hotpot", "Enchiladas", "Baklava", "Gyros", "Hummus", "Empanadas", "Pancakes", "Muffins", "Samosas", "Macarons", "Quiche", "Pierogi", "Arepas", "Okonomiyaki", "Ceviche", "Brisket", "Bao Buns", "Poutine", "Clam Chowder", "Fajitas", "Canelé", "Kimchi", "Tamales", "Omelette", "Biscuits", "Tempura", "Spring Rolls", "Crepes"
];
// 5. Print out only foods that have an "a" in the name. For example, "Pizza" would not be included, but "Donuts" would be.
for (let i = 0; i < friendFavorites.length; i++) {
if (friendFavorites[i].includes("a")) {
console.log(friendFavorites[i]);
}
}
// 6. Store the result in an array called foodsWithA. Print out the array.
let foodsWithA = friendFavorites.filter(food => food.includes("a"));
console.log(foodsWithA);
// 7. Create a new array longFoodNames for foods with names longer than 6 characters.
let longFoodNames = friendFavorites.filter(food => food.length > 6);
console.log(longFoodNames);
// 8. Create another array shortFoodNames for foods 6 characters or shorter.
let shortFoodNames = friendFavorites.filter(food => food.length <= 6);
console.log(shortFoodNames);
// 9. Print both arrays and compare:
// "There are more long-named foods." OR "There are more short-named foods."
if (longFoodNames.length > shortFoodNames.length) {
console.log("There are more long-named foods.");
} else if (longFoodNames.length < shortFoodNames.length) {
console.log("There are more short-named foods.");
} else {
console.log("There are an equal number of long-named and short-named foods.");
}
// 10. STRETCH: Find the longest food name and print:
// "The longest food name in the list is ______ with ___ characters."
let longestFoodName = "";
for (let i = 0; i < friendFavorites.length; i++) {
if (friendFavorites[i].length > longestFoodName.length) {
longestFoodName = friendFavorites[i];
}
}
console.log("The longest food name in the list is " + longestFoodName + " with " + longestFoodName.length + " characters.");