diff --git a/arrays.js b/arrays.js index 37faed2..9bbb8d7 100644 --- a/arrays.js +++ b/arrays.js @@ -14,12 +14,6 @@ Expected Output: - Updated inventory */ -// ✍️ Solve it here ✍️ - - - - - /* Task 2: Student Attendance Checker 📚✅ @@ -40,10 +34,28 @@ Output: "Ali is present." // ✍️ Write your function here ✍️ +//Question # 1 + const inventory = ["Apples", "Bread", "Milk", "Eggs"]; +inventory.push("Oranges","Banana"); +inventory.shift(); +console.log(inventory); +//end of question 1 +//Q2 +const students = ["Ali", "Fatima", "Hassan", "Layla"]; - +function isPresent(name){ + if (students.includes(name)){ + return console.log(`${name} is present`) + } + else { + return console.log(`${name} is absent`); + } +}; +(isPresent("Fatima")); +(isPresent("Zainab")); +//end of Q2 /* Task 3: Top Scorers Leaderboard 🏆⚽ @@ -67,7 +79,34 @@ 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) { + const 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`); + }); +} + +updateScore("Ronaldo", 2); +printLeaderboard(); diff --git a/callbacks.js b/callbacks.js index 3fadb3f..9aba77b 100644 --- a/callbacks.js +++ b/callbacks.js @@ -17,6 +17,15 @@ Expected Output: */ // ✍️ Solve it here ✍️ +function welcomeMessage(name) { + console.log(`Welcome, ${name}!`); +} + +function sendMessage(userName, callback) { + callback(userName); +} + +sendMessage("Amina", welcomeMessage); @@ -48,6 +57,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(tempValue, callback) { + callback(tempValue); +} + +checkTemperature(35, evaluateTemperature); +checkTemperature(22, evaluateTemperature); +checkTemperature(10, evaluateTemperature); + diff --git a/index.html b/index.html index 1104b64..101735a 100644 --- a/index.html +++ b/index.html @@ -4,6 +4,8 @@

Look at the console to see the results

- + + + \ No newline at end of file diff --git a/objects.js b/objects.js index e2426e9..af25bb2 100644 --- a/objects.js +++ b/objects.js @@ -29,7 +29,24 @@ Expected Output: */ // ✍️ Solve it here ✍️ +const gamerProfile = { + username: "ShadowSlayer", + level: 5, + isOnline: false +}; + +function updateOnlineStatus (gamerProfile, status){ + gamerProfile.isOnline = status; + if (status === true) {console.log (`${gamerProfile.username} is now online`) + } + else {console.log(`${gamerProfile.username} is offline`) + + } +} + +updateOnlineStatus(gamerProfile, true); +updateOnlineStatus(gamerProfile, false); /* @@ -64,8 +81,25 @@ Expected Output: // ✍️ Solve it here ✍️ +const dress = { + name: "Evening Gown", + size: "M", + inStock: true +}; +function checkAvailability(dress) { + if (dress.inStock) + { + console.log(`${dress.name} is available in size ${dress.size}.`); + } + else { + console.log(`${dress.name} is out of stock.`); + } +} + +checkAvailability(dress); + /* Task 3: Supercar Feature Adder 🚗 🚗 🚗 🚗 @@ -74,7 +108,7 @@ You are building a configurator for a supercar. Steps: 1. Create an object named `supercar` with the following properties: - `model` (string): The car's model. - - `price` (number): The base price. + - `price` (number): The base price.+ - `features` (object): An object with a `color` property. 2. Write a function `addFeature` that: @@ -82,8 +116,11 @@ Steps: - Adds the feature to the `features` object and sets it to `true`. - Logs: "[featureName] has been added to [model]." + + 3. Use a **for...in loop** to log all the features of the `supercar` object. + Example: Input: const supercar = { @@ -104,3 +141,39 @@ Features: */ // ✍️ Solve it here ✍️ + +const supercar = { + model: "Ferrari SF90", + price: 500000, + features: { + color: "Red" + } +}; + +const supercar = { + model: "Ferrari SF90", + price: 500000, + features: { + color: "Red" + } +}; + +function addFeatures(supercar, newFeatures) { + for (let i = 0; i < newFeatures.length; i++) { + const feature = newFeatures[i]; + + if (!supercar.features[feature]) { + supercar.features[feature] = true; + console.log(`${feature} has been added to ${supercar.model}`); + } else { + console.log(`${feature} is already present in ${supercar.model}`); + } + } +} + +const newFeatures = ["Turbo", "GPS", "Bluetooth"]; +addFeatures(supercar, newFeatures); + + + +