From ea0b931bcf684815ffac4181b01510d2e1c06691 Mon Sep 17 00:00:00 2001 From: zakariahasanabdiali Date: Wed, 16 Jul 2025 16:44:53 +0300 Subject: [PATCH] Completed week 5.js --- arrays.js | 50 +++++++++++++++++++++++++++++++++++++++ callbacks.js | 28 ++++++++++++++++++++++ objects.js | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 144 insertions(+) diff --git a/arrays.js b/arrays.js index 37faed2..b041424 100644 --- a/arrays.js +++ b/arrays.js @@ -16,6 +16,14 @@ Expected Output: // ✍️ Solve it here ✍️ +const inventory = ["Apples", "Bread", "Milk", "Eggs"]; + +inventory.push("Oranges", "Bananas"); + +inventory.shift(); + +console.log("Updated inventory:", inventory); + @@ -40,6 +48,21 @@ Output: "Ali is present." // ✍️ Write your function here ✍️ +const students = ["Ali", "Fatima", "Hassan", "Layla"]; + +function isPresent(name) { + if (students.includes(name)) { + return `${name} is present.`; + } else { + return `${name} is absent.`; + } +} + +// Tusaale: +console.log(isPresent("Ali")); // Ali is present. +console.log(isPresent("Mohamed")); // Mohamed is absent. + + @@ -67,6 +90,33 @@ Output: Sorted leaderboard with updated scores // ✍️ Write your functions here ✍️ +const topScorers = [ + { name: "Messi", score: 5 }, + { name: "Ronaldo", score: 3 }, + { name: "Neymar", score: 4 } +]; + +function updateScore(playerName, scoreToAdd) { + let player = topScorers.find(p => p.name === playerName); + if (player) { + player.score += scoreToAdd; + } else { + topScorers.push({ name: playerName, score: scoreToAdd }); + } +} + +function printLeaderboard() { + const sorted = [...topScorers].sort((a, b) => b.score - a.score); + console.log("🏆 Leaderboard:"); + sorted.forEach((player, index) => { + console.log(`${index + 1}. ${player.name} - ${player.score} goals`); + }); +} + +// Tusaale: +updateScore("Ronaldo", 2); +printLeaderboard(); + diff --git a/callbacks.js b/callbacks.js index 3fadb3f..ec54bc5 100644 --- a/callbacks.js +++ b/callbacks.js @@ -18,6 +18,16 @@ Expected Output: // ✍️ Solve it here ✍️ +function welcomeMessage(name) { + console.log(`Welcome, ${name}!`); +} + +function sendMessage(name, callback) { + callback(name); +} + +// Tusaale: +sendMessage("Amina", welcomeMessage); // Output: Welcome, Amina! @@ -48,6 +58,24 @@ Expected Output: // ✍️ Solve it here ✍️ +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.`); + } +} + +function checkTemperature(temp, callback) { + callback(temp); +} + +// Tusaalooyin: +checkTemperature(35, evaluateTemperature); // Output: 35°C is Hot. +checkTemperature(22, evaluateTemperature); // Output: 22°C is Warm. +checkTemperature(10, evaluateTemperature); // Output: 10°C is Cold. diff --git a/objects.js b/objects.js index e2426e9..c93e94a 100644 --- a/objects.js +++ b/objects.js @@ -30,6 +30,26 @@ Expected Output: // ✍️ Solve it here ✍️ +const gamerProfile = { + username: "ShadowSlayer", + level: 5, + isOnline: false +}; + +function updateOnlineStatus(profile, status) { + profile.isOnline = status; + + if (status) { + console.log(`${profile.username} is now online.`); + } else { + console.log(`${profile.username} is now offline.`); + } +} + +// Tusaale: +updateOnlineStatus(gamerProfile, true); // Output: ShadowSlayer is now online. +updateOnlineStatus(gamerProfile, false); // Output: ShadowSlayer is now offline. + /* @@ -64,6 +84,22 @@ Expected Output: // ✍️ Solve it here ✍️ +const dress = { + name: "Evening Gown", + size: "M", + inStock: true +}; + +function checkAvailability(item) { + if (item.inStock) { + console.log(`${item.name} is available in size ${item.size}.`); + } else { + console.log(`${item.name} is out of stock.`); + } +} + +// Tusaale: +checkAvailability(dress); // Output: Evening Gown is available in size M. /* @@ -104,3 +140,33 @@ Features: */ // ✍️ Solve it here ✍️ + +// 1. Create the supercar object +const supercar = { + model: "Ferrari SF90", + price: 500000, + features: { + color: "Red" + } +}; + +function addFeature(car, featureName) { + car.features[featureName] = true; + console.log(`${featureName.charAt(0).toUpperCase() + featureName.slice(1)} has been added to ${car.model}.`); + + console.log("Features:"); + for (let key in car.features) { + console.log(`- ${key}: ${car.features[key]}`); + } +} + +// Tusaale: +addFeature(supercar, "turbo"); +/* +Output: +Turbo has been added to Ferrari SF90. +Features: +- color: Red +- turbo: true +*/ +