Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"liveServer.settings.port": 5501
}
135 changes: 124 additions & 11 deletions arrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@ Expected Output:
*/

// ✍️ Solve it here ✍️
const inventory = ["Apples", "Bread", "Milk", "Eggs"];
// 1. Add "Oranges" and "Bananas" to the inventory
inventory.push("Oranges", "Bananas");

// 2. Remove the first item from the array
inventory.shift();



// 3. Print the updated inventory
console.log("Updated inventory:", inventory);

/*
Task 2: Student Attendance Checker 📚✅
Expand All @@ -39,11 +44,22 @@ Output: "Ali is present."
*/

// ✍️ Write your function here ✍️
// Array of student names
const students = ["Ali", "Fatima", "Hassan", "Layla"];

// 1. Function to check attendance
function isPresent(name) {
// 2. Check if the name exists in the array
if (students.includes(name)) {
return name + " is present";
} else {
return name + " is absent";
}
}




// Example usage:
console.log(isPresent("Ali"));
console.log(isPresent("Zara"));

/*
Task 3: Top Scorers Leaderboard 🏆⚽
Expand All @@ -66,13 +82,39 @@ Output: Sorted leaderboard with updated scores
*/

// ✍️ Write your functions here ✍️
// Initial leaderboard
const topScorers = [
{ name: "Messi", score: 5 },
{ name: "Ronaldo", score: 3 },
{ name: "Neymar", score: 4 },
];







// 1. Function to update or add a player's score
function updateScore(playerName, addedScore) {
const player = topScorers.find((p) => p.name === playerName);

if (player) {
player.score += addedScore;
} else {
topScorers.push({ name: playerName, score: addedScore });
}
}

// 2. Function to print sorted leaderboard
function printLeaderboard() {
// Sort in descending order of score
const sorted = [...topScorers].sort((a, b) => b.score - a.score);

// Print the leaderboard
console.log("🏆 Leaderboard:");
sorted.forEach((player, index) => {
console.log(`${index + 1}. ${player.name} - ${player.score}`);
});
}

// Example usage
updateScore("Ronaldo", 2);
printLeaderboard();
/*
STRETCH TASK: **The Ultimate Treasure Hunt** 🗺️💎🏴‍☠️

Expand Down Expand Up @@ -139,3 +181,74 @@ Final Output:
- "Congratulations! You found the ultimate treasure!" (if all conditions are met)

*/
// 1. Check if a clue exists
function findClue(clues, name) {
if (clues.includes(name)) {
return `Clue ${name} found!`;
} else {
return `Clue ${name} is missing, search again!`;
}
}

// 2. Decipher each clue message
function decipherMessage(clueMessages) {
const decoded = [];
for (let message of clueMessages) {
decoded.push(message.split("").reverse().join(""));
}
return decoded;
}

// 3. Follow the steps on the treasure map
function followSteps(steps) {
for (let i = 0; i < steps.length; i++) {
if (steps[i] === "Danger") {
console.log("Stopped at danger. Cannot continue.");
return false;
}
console.log(`Step ${i + 1}: ${steps[i]}`);
}
return true;
}

// 4. The ultimate treasure hunt logic
function ultimateTreasureHunt(clues, clueMessages, treasureMapSteps) {
let allCluesFound = true;

// Check every clue
for (let clue of clues) {
const result = findClue(clues, clue);
console.log(result);
if (result.includes("missing")) {
allCluesFound = false;
}
}

// Decode the clues
const decoded = decipherMessage(clueMessages);
console.log("Decoded messages:", decoded);

// Follow the map
const journeySuccess = followSteps(treasureMapSteps);

// Check the final step
const finalStep = treasureMapSteps[treasureMapSteps.length - 1];

// Final output
if (allCluesFound && journeySuccess && finalStep === "Treasure") {
console.log("🎉 Congratulations! You found the ultimate treasure!");
} else {
console.log("🪙 The treasure remains hidden. Try again!");
}
}

const clues = ["Map", "Compass", "Key", "Shovel"];
const clueMessages = ["paM", "ssapmoC", "yeK", "levohS"];
const treasureMapSteps = [
"Start at the beach",
"Cross the forest",
"Climb the mountain",
"Treasure",
];

ultimateTreasureHunt(clues, clueMessages, treasureMapSteps);
51 changes: 47 additions & 4 deletions callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,18 @@ Expected Output:
*/

// ✍️ Solve it here ✍️
// Step 1: Define the callback function
function welcomeMessage(name) {
console.log(`Welcome, ${name}!`);
}

// // Step 2: Define the main function that takes name and callback
function sendMessage(userName, callback) {
callback(userName);
}


// Step 3: Call the main function with name and callback
sendMessage("Amina", welcomeMessage);

/*
Task 2: Temperature Checker 🌡️🌡️🌡️🌡️
Expand Down Expand Up @@ -47,9 +56,26 @@ Expected Output:
*/

// ✍️ Solve it here ✍️



// Step 1: Define the callback function
function evaluateTemperature(temp) {
if (temp > 30) {
console.log(`${temp}°C is Hot.`);
} else if (temp >= 15 && temp <= 30) {
console.log(`${temp}°C is Warm.`);
} else {
console.log(`${temp}°C is Cold.`);
}
}

// Step 2: Define the main function that accepts temp and callback
function checkTemperature(temperature, callback) {
callback(temperature);
}

// Step 3: Test with different inputs
checkTemperature(35, evaluateTemperature);
checkTemperature(22, evaluateTemperature);
checkTemperature(10, evaluateTemperature);

/*
STRETCH: Task 3: Quiz Evaluator 📚📚📚📚
Expand All @@ -73,3 +99,20 @@ Expected Output:
*/

// ✍️ Solve it here ✍️
// Step 1: Define the callback function
function checkUserAnswer(userAnswer, correctAnswer) {
if (userAnswer === correctAnswer) {
console.log("Correct!");
} else {
console.log(`Incorrect. The correct answer is ${correctAnswer}.`);
}
}

// Step 2: Define the main function
function evaluateAnswer(question, correctAnswer, callback) {
const userResponse = prompt(question);
callback(userResponse, correctAnswer);
}

// Step 3: Use the function
evaluateAnswer("What is 5 + 5?", "10", checkUserAnswer);
17 changes: 11 additions & 6 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
<!DOCTYPE html>
<html>
<head>Week 5 Assignment</head>
<body>
<h1>Look at the console to see the results</h1>
<head>
<title>Week 5 Assignment</title>
</head>
<body>
<h1>Look at the console to see the results</h1>

<!-- Before starting, add the javascript files in this html fie -->
</body>
</html>
<!-- Before starting, add the javascript files in this html fie -->
<script src="arrays.js"></script>
<script src="callbacks.js"></script>
<script src="objects.js"></script>
</body>
</html>
63 changes: 63 additions & 0 deletions objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,27 @@ Expected Output:
*/

// ✍️ Solve it here ✍️
// Step 1: Create the gamerProfile object
const gamerProfile = {
username: "ShadowSlayer",
level: 5,
isOnline: false,
};

// Step 2: Define the function to update online status
function updateOnlineStatus(profile, status) {
profile.isOnline = status;

if (status) {
console.log(`${profile.username} is now online.`);
} else {
console.log(`${profile.username} is now offline.`);
}
}

// Step 3: Call the function with a new status
updateOnlineStatus(gamerProfile, true);
updateOnlineStatus(gamerProfile, false);

/*
Task 2: Dress Inventory Checker 👗 👗 👗 👗 👗
Expand Down Expand Up @@ -64,7 +83,24 @@ Expected Output:

// ✍️ Solve it here ✍️

// Step 1: Create the dress object
const dress = {
name: "white t-shirt",
size: "M",
inStock: true,
};

// Step 2: Define the function to check availability
function checkAvailability(dressObj) {
if (dressObj.inStock) {
console.log(`${dressObj.name} is available in size ${dressObj.size}.`);
} else {
console.log(`${dressObj.name} is out of stock.`);
}
}

// Step 3: Call the function
checkAvailability(dress);

/*
Task 3: Supercar Feature Adder 🚗 🚗 🚗 🚗
Expand Down Expand Up @@ -104,3 +140,30 @@ Features:
*/

// ✍️ Solve it here ✍️
const supercar = {
model: "Ferrari SF90",
price: 500000,
features: {
color: "Red",
},
};

// Step 2: Define the addFeature function
function addFeature(car, featureName) {
car.features[featureName] = true; // Add new feature
console.log(`${capitalize(featureName)} has been added to ${car.model}.`);
}

// Optional helper to capitalize first letter
function capitalize(word) {
return word.charAt(0).toUpperCase() + word.slice(1);
}

// Step 3: Call the function to add a feature
addFeature(supercar, "turbo");

// Step 4: Use for...in to log all features
console.log("Features:");
for (let key in supercar.features) {
console.log(`- ${key}: ${supercar.features[key]}`);
}