Skip to content
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
57 changes: 45 additions & 12 deletions arrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ Expected Output:
*/

// ✍️ Solve it here ✍️

let inventory = ["Apples", "Bread", "Milk", "Eggs"];
let first = inventory.shift();
console.log(inventory);



Expand All @@ -39,12 +41,15 @@ Output: "Ali is present."
*/

// ✍️ Write your function here ✍️






let students = ["Ali", "Fatima", "Hassan", "Layla"];
function isPresent(name) {
if (students.includes(name)) {
return name + " is present.";
} else {
return name + " is absent.";
}
}
console.log(isPresent("Ali"));
/*
Task 3: Top Scorers Leaderboard 🏆⚽

Expand All @@ -66,11 +71,30 @@ Output: Sorted leaderboard with updated scores
*/

// ✍️ Write your functions here ✍️





let topScorers = [
{ name: "Messi", score: 5 },
{ name: "Ronaldo", score: 3 },
{ name: "Neymar", score: 4 }
];

function updateScore(playerName, scoreToAdd) {
let player = topScorers.find(p => p.name === playerName);
if (player) {
player.score += scoreToAdd;
} else {
topScorers.push({ name: playerName, score: scoreToAdd });
}
}

function printLeaderboard() {
topScorers.sort((a, b) => b.score - a.score);
console.log("Leaderboard:");
topScorers.forEach(player => {
console.log(`${player.name}: ${player.score}`);
});
}
updateScore("Ronaldo", 2);
printLeaderboard();


/*
Expand All @@ -89,6 +113,15 @@ Here’s the plan:
- If it exists, return "Clue [name] found!"
- If it doesn’t exist, return "Clue [name] is missing, search again!"

let clues = ["Map", "Compass", "Key", "Shovel"];
function findClue(clues, name) {
if (clues.includes(name)) {
return `Clue ${name} found!`;
} else {
return `Clue ${name} is missing, search again!`;
}
} console.log(findClue(clues, "Map"));

2. **Decipher Hidden Messages**:
- Each clue is a scrambled message stored in the `clueMessages` array.
- Write a function called `decipherMessage` that uses a loop to reverse each message in the `clueMessages` array and return the updated array.
Expand Down
49 changes: 25 additions & 24 deletions callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ Expected Output:
*/

// ✍️ Solve it here ✍️
let sendMessage = (name, callback) => {
callback(name);
};

let theCallBackFunction = (name) => {
console.log(`Welcome, ${name}!`);
};

sendMessage("Amina", theCallBackFunction);




Expand Down Expand Up @@ -47,29 +57,20 @@ Expected Output:
*/

// ✍️ Solve it here ✍️




/*
STRETCH: Task 3: Quiz Evaluator 📚📚📚📚

You are building a quiz system. Write a function called `evaluateAnswer` that:
1. Takes a question, a correct answer, and a callback function as arguments.
2. The callback function should compare the user's answer with the correct answer and log whether the answer is correct or not.

Steps:
- Create the callback function to evaluate:
- If the user's answer matches the correct answer, log: "Correct!"
- Otherwise, log: "Incorrect. The correct answer is [correctAnswer]."

Example:
Input:
evaluateAnswer("What is 5 + 5?", "10", TheCallBackFunction);

Expected Output:
- If user's input is "10": "Correct!"
- If user's input is "15": "Incorrect. The correct answer is 10."
*/
let checkTemperature = (temperature, callback) => {
callback(temperature);
};
let temperatureEvaluator = (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, temperatureEvaluator);

// ✍️ Solve it here ✍️

3 changes: 3 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
<head>Week 5 Assignment</head>
<body>
<h1>Look at the console to see the results</h1>
<script src="arrays.js"></script>
<script src="callbacks.js"></script>
<script src="objects.js"></script>

<!-- Before starting, add the javascript files in this html fie -->
</body>
Expand Down
46 changes: 46 additions & 0 deletions objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,22 @@ Expected Output:
*/

// ✍️ Solve it here ✍️
let gamerProfile = {
username: "ShadowSlayer",
level: 5,
isOnline: false
};

let updateOnlineStatus = (profile, status) => {
profile.isOnline = status;
if (status) {
console.log(`${profile.username} is now online.`);
} else {
console.log(`${profile.username} is now offline.`);
}
};

updateOnlineStatus(gamerProfile, true);

/*
Task 2: Dress Inventory Checker 👗 👗 👗 👗 👗
Expand Down Expand Up @@ -63,8 +77,21 @@ Expected Output:
*/

// ✍️ Solve it here ✍️
let dress = {
name: "Evening Gown",
size: "M",
inStock: true
};

let 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 +131,22 @@ Features:
*/

// ✍️ Solve it here ✍️
let supercar = {
model: "Ferrari SF90",
price: 500000,
features: {
color: "Red"
}
};

let addFeature = (car, featureName) => {
car.features[featureName] = true;
console.log(`${featureName.charAt(0).toUpperCase() + featureName.slice(1)} has been added to ${car.model}.`);
};

addFeature(supercar, "turbo");

console.log("Features:");
for (let feature in supercar.features) {
console.log(`- ${feature}: ${supercar.features[feature]}`);
}