diff --git a/arrays.js b/arrays.js index 37faed2..b7aff00 100644 --- a/arrays.js +++ b/arrays.js @@ -17,7 +17,12 @@ Expected Output: // ✍️ Solve it here ✍️ +const inventory = ["Apples", "Bread", "Milk", "Eggs"]; + +inventory.push("Oranges", "Bananas"); +inventory.shift(); +console.log("Updated inventory:", inventory); /* @@ -41,7 +46,15 @@ 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.`; + } +} @@ -67,7 +80,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 playerFound = false; + for (let i = 0; i < topScorers.length; i++) { + if (topScorers[i].name === playerName) { + topScorers[i].score += scoreToAdd; + playerFound = true; + break; + } + } + if (!playerFound) { + topScorers.push({ name: playerName, score: scoreToAdd }); + } +} + +function printLeaderboard() { + topScorers.sort((a, b) => b.score - a.score); + console.log("Top Scorers Leaderboard:"); + topScorers.forEach(player => { + console.log(`${player.name}: ${player.score}`); + }); +} diff --git a/callbacks.js b/callbacks.js index 3fadb3f..12e76bb 100644 --- a/callbacks.js +++ b/callbacks.js @@ -18,7 +18,13 @@ Expected Output: // ✍️ Solve it here ✍️ +function welcomeCallback(name) { + console.log(`Welcome, ${name}!`); +} +function sendMessage(userName, callback) { + callback(userName); +} /* @@ -49,7 +55,19 @@ Expected Output: // ✍️ Solve it here ✍️ +function evaluateTemperature(temperature) { + if (temperature > 30) { + console.log(`${temperature}°C is Hot.`); + } else if (temperature >= 15 && temperature <= 30) { + console.log(`${temperature}°C is Warm.`); + } else { + console.log(`${temperature}°C is Cold.`); + } +} +function checkTemperature(temperatureValue, callback) { + callback(temperatureValue); +} /* STRETCH: Task 3: Quiz Evaluator 📚📚📚📚 @@ -73,3 +91,27 @@ Expected Output: */ // ✍️ Solve it here ✍️ +function evaluateQuizAnswer(question, correctAnswer, userAnswer) { + if (userAnswer === correctAnswer) { + console.log("Correct!"); + } else { + console.log(`Incorrect. The correct answer is ${correctAnswer}.`); + } +} + +function evaluateAnswer(question, correctAnswer, callback) { + // The problem description implies a user's answer needs to be passed to the callback. + // Since there's no mechanism for user input in this context, + // I'll demonstrate with two hardcoded user answers as per the example. + // In a real scenario, 'userAnswer' would come from actual user interaction. + + // Example for correct answer + const userAnswerCorrect = "10"; + console.log(`Question: ${question}`); // Added to clarify output + callback(question, correctAnswer, userAnswerCorrect); + + // Example for incorrect answer + const userAnswerIncorrect = "15"; + console.log(`Question: ${question}`); // Added to clarify output + callback(question, correctAnswer, userAnswerIncorrect); +} \ No newline at end of file diff --git a/objects.js b/objects.js index e2426e9..9ac11cd 100644 --- a/objects.js +++ b/objects.js @@ -30,7 +30,20 @@ Expected Output: // ✍️ Solve it here ✍️ +const gamerProfile = { + username: "ShadowSlayer", + level: 5, + isOnline: false +}; +function updateOnlineStatus(profile, status) { + profile.isOnline = status; + if (profile.isOnline) { + console.log(`${profile.username} is now online.`); + } else { + console.log(`${profile.username} is now offline.`); + } +} /* Task 2: Dress Inventory Checker 👗 👗 👗 👗 👗 @@ -64,7 +77,19 @@ Expected Output: // ✍️ Solve it here ✍️ +const dress = { + name: "Evening Gown", + size: "M", + inStock: true +}; +function checkAvailability(dressObject) { + if (dressObject.inStock) { + console.log(`${dressObject.name} is available in size ${dressObject.size}.`); + } else { + console.log(`${dressObject.name} is out of stock.`); + } +} /* Task 3: Supercar Feature Adder 🚗 🚗 🚗 🚗 @@ -104,3 +129,22 @@ Features: */ // ✍️ Solve it here ✍️ +const supercar = { + model: "Ferrari SF90", + price: 500000, + features: { + color: "Red" + } +}; + +function addFeature(car, featureName) { + car.features[featureName] = true; + console.log(`${featureName} has been added to ${car.model}.`); +} + +// After calling addFeature (e.g., addFeature(supercar, "turbo");) +// Use this loop to print features: +// console.log("Features:"); +// for (const feature in supercar.features) { +// console.log(`- ${feature}: ${supercar.features[feature]}`); +// }