diff --git a/arrays.js b/arrays.js index 37faed2..0ff82ce 100644 --- a/arrays.js +++ b/arrays.js @@ -16,6 +16,13 @@ Expected Output: // ✍️ Solve it here ✍️ +const inventory =["milk", "apple","chocolate","bread","eggs"] + + inventory:push("orange","bananas"); + + inventory:finish(); + + console.log("Updated inventory",inventory) @@ -39,7 +46,17 @@ Output: "Ali is present." */ // ✍️ Write your function here ✍️ + const students = ["ahmed","ayaan","nimco","hassan"]; + function isPresent(name){ + if(students.includes(name)){ + return `${name} is present`; + }else{ + return `${name} is absent`; + } + } + console.log(students("nimco")); + console.log(student("ahmed")) @@ -66,7 +83,41 @@ Output: Sorted leaderboard with updated scores */ // ✍️ Write your functions here ✍️ - + + const topScorers = [ + {name: "messi", score: 10}, + {name:"Ronaldo", score: 8}, + {name: "Naymer", score: 6}, + ] + + function updateScore(playerName, points) { + let found = false; + + for (let i = 0; i < topScorers.length; i++) { + if (topScorers[i].name === playerName) { + topScorers[i].score += points; + found = true; + break; + } + } + if (!found) { + topScorers.push({ name: playerName, score: points }); + } + } + function printLeaderboard(){ + topScorers.sort(function(a,b){ + return b.score - a.score; + }); + } + + for (let i = 0; i < topScorers.length; i++){ + console.log(topScorers[i].name + ":" + topScorers[i].score); + } + + updateScore("Messi", 10) + printLeaderboard(); + + diff --git a/callbacks.js b/callbacks.js index 3fadb3f..c347ced 100644 --- a/callbacks.js +++ b/callbacks.js @@ -19,6 +19,14 @@ Expected Output: // ✍️ Solve it here ✍️ + function showWelcome(name) { + console.log("Welcome, " + name + "!"); + } + function sendMessage(userName, callbackFn) { + callbackFn(userName); +} + sendMessage("Ahmed", showWelcome); + /* @@ -47,6 +55,23 @@ Expected Output: */ // ✍️ Solve it here ✍️ + + function readTemperature(value) { + if (value > 30) { + console.log(value + "°C is Hot."); + } else if (value >= 15 && value <= 30) { + console.log(value + "°C is Warm."); + } else { + console.log(value + "°C is Cold."); + } +} + function checkTemperature(temp, callbackFn) { + callbackFn(temp); +} + + checkTemperature(35, readTemperature); + checkTemperature(22, readTemperature); + checkTemperature(10, readTemperature); @@ -73,3 +98,16 @@ Expected Output: */ // ✍️ Solve it here ✍️ + function checkUserAnswer(userAnswer, rightAnswer) { + if (userAnswer === rightAnswer) { + console.log("Correct!"); + } else { + console.log("Incorrect. The correct answer is " + rightAnswer + "."); + } +} + + function checkUserAnswer(){ + let userInput = prompt(question); + callbackFn(userInput, correctAnswer); + } + diff --git a/index.html b/index.html index 1104b64..d181b9d 100644 --- a/index.html +++ b/index.html @@ -5,5 +5,9 @@