diff --git a/arrays.js b/arrays.js index 37faed2..c4dae34 100644 --- a/arrays.js +++ b/arrays.js @@ -15,9 +15,10 @@ Expected Output: */ // ✍️ Solve it here ✍️ - - - +const inventory = ["Apple", "Bread", "Milk", "Eggs"]; +inventory.push("Oranges","Bananas"); +inventory.shift(); +console.log(inventory); /* @@ -39,9 +40,16 @@ Output: "Ali is present." */ // ✍️ Write your function here ✍️ - - - + const students = ["Ali", "Fatima", "Hassan", "Layla"]; +function isPresent(name){ + if(name==students.at()){ + return `${[name]} is present`; + } + else{ + return `${[name]} is not present`; + } +} +console.log(isPresent("Ali")); @@ -50,8 +58,10 @@ 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. +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 = [ @@ -66,7 +76,23 @@ Output: Sorted leaderboard with updated scores */ // ✍️ Write your functions here ✍️ - +const topScorers = [ + { name: "Messi", score: 5 }, + { name: "Ronaldo", score: 3 }, + { name: "Neymar", score: 4 } +] +let i; +function updateScore(name, Score){ + if(name==topScorers.at().name){ + topScorers.at().score=topScorers.at().score+Score; + } + else{ + topScorers.push({name, Score}); + } + +} +updateScore("Messi", 5); +console.log(topScorers); diff --git a/callbacks.js b/callbacks.js index 3fadb3f..0443829 100644 --- a/callbacks.js +++ b/callbacks.js @@ -17,7 +17,13 @@ Expected Output: */ // ✍️ Solve it here ✍️ - +function sendMessage(name, callback){ + callback(name); +} +function call(names){ + console.log(`welcome, ${names}`); +} +sendMessage("Amina", call); @@ -47,7 +53,21 @@ Expected Output: */ // ✍️ Solve it here ✍️ - +function checkTemperature(temp, Callback){ + Callback(temp); +} +function checkTempcallback(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`); + } +} +checkTemperature(35, checkTempcallback); diff --git a/index.html b/index.html index 1104b64..ce27378 100644 --- a/index.html +++ b/index.html @@ -5,5 +5,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..0fb830b 100644 --- a/objects.js +++ b/objects.js @@ -29,7 +29,20 @@ Expected Output: */ // ✍️ Solve it here ✍️ - +const gamerProfile = { + username: "ShadowSlayer", + level: 5, + isOnline: false +}; +function updateOnlineStatus(gamerProfile, status){ + if(gamerProfile.isOnline==status){ + console.log(`${gamerProfile.username} is now online`); + } + else{ + console.log(`${gamerProfile.username} is now offline`) + } +} +updateOnlineStatus(gamerProfile, true); /* @@ -63,8 +76,20 @@ 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 🚗 🚗 🚗 🚗 @@ -86,14 +111,6 @@ Steps: Example: Input: -const supercar = { - model: "Ferrari SF90", - price: 500000, - features: { - color: "Red" - } -}; - addFeature(supercar, "turbo"); Expected Output: @@ -104,3 +121,18 @@ Features: */ // ✍️ Solve it here ✍️ +const supercar = { + model: "Ferrari SF90", + price: 500000, + features: { + color: "Red" + } +}; +function addFeature(supercar, feature){ + supercar.features.feature=true; + console.log(`${feature} has been added`); +} +addFeature(supercar, "turbo"); +for(let key in supercar.features){ + console.log(`${key}: ${supercar.features[key]}`); +} \ No newline at end of file