Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

i have worked on it objects and callback function and Arrays #27

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"liveServer.settings.port": 5501
}
92 changes: 92 additions & 0 deletions arrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ Expected Output:

// ✍️ Solve it here ✍️

const inventory = ["Apples", "Bread", "Milk", "Eggs"];

inventory.push("Oranges","Bananas");
inventory.shift();
console.log(inventory)



Expand All @@ -40,6 +45,19 @@ Output: "Ali is present."

// ✍️ Write your function here ✍️

const students = ["Ali", "Fatima", "Hassan", "Layla"];
const isPresent=(name)=>{

if(students.includes(name)){
return`${name} is present`;
}
else
{
return`${name} is absent`;
}
};
const student_name = prompt("enter your name")
alert(isPresent(student_name));



Expand Down Expand Up @@ -67,10 +85,37 @@ Output: Sorted leaderboard with updated scores

// ✍️ Write your functions here ✍️

const topScorers = [
{ name: "Messi", score: 5 },
{ name: "Ronaldo", score: 3 },
{ name: "Neymar", score: 4 }
];
const updateScore = (playerName, scoreToAdd) => {

let player = topScorers.find(p => p.name === playerName);

if (player) {

player.score += scoreToAdd;
} else {

topScorers.push({ name: playerName, score: scoreToAdd });
}
};

const printLeaderboard = () => {

topScorers.sort((a, b) => b.score - a.score);


console.log("🏆 Soccer Leaderboard 🏆");
topScorers.forEach(player => {
console.log(`${player.name}: ${player.score} goals`);
});
};

updateScore("Ronaldo", 2);
printLeaderboard();


/*
Expand Down Expand Up @@ -139,3 +184,50 @@ Final Output:
- "Congratulations! You found the ultimate treasure!" (if all conditions are met)

*/
const clues = ["Map", "Compass", "Key", "Shovel"];
const clueMessages = ["ppaM", "ssapmoC", "yeK", "levohS"];
const treasureMapSteps = ["Start at the beach", "Cross the forest", "Climb the mountain", "Danger", "Treasure"];


const findClue=(clues, clueName)=>{

return clues.includes(clueName(`clue: ${clueName} found clue:${clueName}is missign,search again `));
// return clues.includes(clueName) ? `Clue ${clueName} found!` : `Clue ${clueName} is missing, search again!`;
}

const decipherMessage = (messages) => {
return messages.map(msg => msg.split("").reverse().join(""));
};

const followSteps=(steps)=>{
for(let i = 0; i < steps.length ; i++){
console.log(steps[i]);

if(steps[i]=="danger"){
console.log("Stopped at danger. Cannot continue.");
return false;
}
}
return steps[steps.length - 1] === "Treasure";
};

const findTreasure = () => {

let allCluesFound = clues.every(clue => clues.includes(clue));

// Decipher clue messages
let decodedMessages = decipherMessage(clueMessages);
console.log("Deciphered Clues:", decodedMessages);


let reachedTreasure = followSteps(treasureMapSteps);


if (allCluesFound && reachedTreasure) {
console.log(" Congratulations! You found the ultimate treasure ");
} else {
console.log(" The treasure remains hidden. Try again");
}
}

findTreasure();
47 changes: 45 additions & 2 deletions callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,14 @@ Expected Output:

// ✍️ Solve it here ✍️

// function sendMessage(name , callback){
// return callback (name)
// }


// const callback =(name)=>{
// console.log (name)
// }
// sendMessage("welcome amina",callback);

/*
Task 2: Temperature Checker 🌡️🌡️🌡️🌡️
Expand Down Expand Up @@ -48,7 +54,26 @@ Expected Output:

// ✍️ Solve it here ✍️


// const checkTemperature = (temperature , weatherCallback)=>{
//
// if(temperature > 30){
// return callback `${temperature}°C is hot`;
// }
// else if(temperature >= 15 && temperature <=30)
// {
// return callback `${temperature}°C is Warm`;
// }
// else
// {
// return callback `${temperature}°C is Cold`;
// }
// }
// const weatherCallback=(temperature)=>{

// console.log(temperature)
// }
// const temp = parseInt(prompt("Enter the temperature:"));
// checkTemperature(temp , weatherCallback);


/*
Expand All @@ -73,3 +98,21 @@ Expected Output:
*/

// ✍️ Solve it here ✍️


// const evaluateAnswer =(question, correctAnswer, callback)=>{

// return callback(question, correctAnswer);

// }

// const checkAnswer =(question,correctAnswer)=>{
// if (question === correctAnswer){
// console.log("correct")
// }
// else{
// console.log("Incorrect")
// }
// }
// const userAnswer = prompt("What is 5+5?");
// evaluateAnswer(userAnswer, "10", checkAnswer);
3 changes: 3 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@
<h1>Look at the console to see the results</h1>

<!-- Before starting, add the javascript files in this html fie -->
<script src="arrays.js"></script>
<script src="callbacks.js"></script>
<script src="objects.js"></script>
</body>
</html>
55 changes: 52 additions & 3 deletions objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,23 @@ Expected Output:

// ✍️ Solve it here ✍️


const gamerProfile = {
username: "ShadowSlayer",
level: 5,
isOnline: true,

function: updateOnlineStatus = (status)=>{
this.isOnline = status;
if (gamerProfile.isOnline)
{
console.log("ShadowSlayer is now online.")
}
else{
console.log("is not online")
}
}
};
updateOnlineStatus(gamerProfile, false);

/*
Task 2: Dress Inventory Checker 👗 👗 👗 👗 👗
Expand Down Expand Up @@ -64,8 +80,22 @@ Expected Output:

// ✍️ Solve it here ✍️



// const dress = {
// name: "Evening Gown",
// size: "M",
// inStock: true
// };

// const 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 🚗 🚗 🚗 🚗

Expand Down Expand Up @@ -104,3 +134,22 @@ Features:
*/

// ✍️ Solve it here ✍️

const supercar = {
model: "Ferrari SF90",
price: 500000,
features: {
color: "Red"
}
};

const addFeature = (car, featureName) => {
car.features[featureName] = true;
console.log(`${featureName} has been added to ${car.model}.`);

for (let key in car.features) {
console.log(`- ${key}: ${car.features[key]}`);
}
}

addFeature(supercar, "turbo");