forked from gabischool/Week5_JS_Assignment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharrays.js
141 lines (92 loc) · 4.06 KB
/
arrays.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
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
/*
Task 1: Grocery Store Restock 🛒🍎
You manage a grocery store. The array `inventory` shows the items currently in stock.
1. Add "Oranges" and "Bananas" to the inventory.
2. Remove the first item from the array.
3. Print the updated inventory.
Array:
const inventory = ["Apples", "Bread", "Milk", "Eggs"];
Expected Output:
- Updated inventory
*/
// ✍️ Solve it here ✍️
/*
Task 2: Student Attendance Checker 📚✅
You are a teacher tracking attendance. Use the `students` array to check if specific students are present.
1. Write a function called `isPresent` that takes a student's name as input.
2. Use an if statement to check if the name exists in the `students` array.
- If present, return "[name] is present."
- If not present, return "[name] is absent."
Array:
const students = ["Ali", "Fatima", "Hassan", "Layla"];
Example:
Input: isPresent("Ali")
Output: "Ali is present."
*/
// ✍️ Write your function here ✍️
/*
Task 3: Top Scorers Leaderboard 🏆⚽
You are creating a leaderboard for a soccer game. The array `topScorers` contains the names of players and their scores.
1. Write a function called `updateScore` that takes a player's name and a score to add. If the player exists in the leaderboard, add the score to their total. If not, add the player to the array with the given score.
2. Write another function called `printLeaderboard` that sorts the leaderboard in descending order of scores and prints it.
Array:
const topScorers = [
{ name: "Messi", score: 5 },
{ name: "Ronaldo", score: 3 },
{ name: "Neymar", score: 4 }
];
Example:
Input: updateScore("Ronaldo", 2)
Output: Sorted leaderboard with updated scores
*/
// ✍️ Write your functions here ✍️
/*
STRETCH TASK: **The Ultimate Treasure Hunt** 🗺️💎🏴☠️
You are a legendary adventurer searching for the ultimate treasure!
The treasure is hidden in a remote island, and you have a map with a series
of clues stored in arrays. Your task is to navigate through the clues,
decode them, and uncover the treasure.
Here’s the plan:
1. **Clue Checkpoint**:
- Write a function called `findClue` that takes a `clues` array and a clue name (string).
- Check if the clue exists in the array:
- If it exists, return "Clue [name] found!"
- If it doesn’t exist, return "Clue [name] is missing, search again!"
2. **Decipher Hidden Messages**:
- Each clue is a scrambled message stored in the `clueMessages` array.
- Write a function called `decipherMessage` that uses a loop to reverse each message in the `clueMessages` array and return the updated array.
3. **Follow the Treasure Map**:
- You are given an array of steps to reach the treasure: `treasureMapSteps`.
- Write a function called `followSteps` that:
- Uses a loop to log each step in the journey.
- Tracks your current position in the array.
- Stops if the step says "Danger" and logs: "Stopped at danger. Cannot continue."
4. **Final Treasure Hunt**:
- Use all the above functions to:
- Check if all the clues exist.
- Decipher the messages.
- Follow the map.
- If all clues are found, all steps are completed without danger, and the final step is "Treasure," log:
"Congratulations! You found the ultimate treasure!"
- Otherwise, log: "The treasure remains hidden. Try again!"
---
### Input Data
const clues = ["Map", "Compass", "Key", "Shovel"];
const clueMessages = ["ppaM", "ssapmoC", "yeK", "levohS"]; // scrambled clues
const treasureMapSteps = ["Start at the beach", "Cross the forest", "Climb the mountain", "Danger", "Treasure"];
---
### Example Usage
findClue(clues, "Map");
// Output: "Clue Map found!"
decipherMessage(clueMessages);
// Output: ["Map", "Compass", "Key", "Shovel"]
followSteps(treasureMapSteps);
// Output:
// "Step 1: Start at the beach"
// "Step 2: Cross the forest"
// "Step 3: Climb the mountain"
// "Stopped at danger. Cannot continue."
Final Output:
- "The treasure remains hidden. Try again!" (if danger is encountered)
- "Congratulations! You found the ultimate treasure!" (if all conditions are met)
*/