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
36 changes: 32 additions & 4 deletions arrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ Expected Output:

// ✍️ Solve it here ✍️

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

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

console.log("Updated inventory:", inventory);



Expand All @@ -40,10 +45,17 @@ Output: "Ali is present."

// ✍️ Write your function here ✍️

const 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"));
console.log(isPresent("faarax")); "

/*
Task 3: Top Scorers Leaderboard 🏆⚽
Expand All @@ -67,7 +79,23 @@ Output: Sorted leaderboard with updated scores

// ✍️ Write your functions here ✍️


function updateScore(name, score) {
const player = topScorers.find(p => p.name === name);
if (player) {
player.score += score;
} else {
topScorers.push({ name, score });
}
printLeaderboard();
}

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



Expand Down
30 changes: 30 additions & 0 deletions callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,16 @@ Expected Output:

// ✍️ Solve it here ✍️

function welcomeMessage(name) {
console.log(`Welcome, ${name}!`);
}

function sendMessage(name, callback) {
callback(name);
}

// Example usage:
sendMessage("Amina", welcomeMessage);


/*
Expand Down Expand Up @@ -73,3 +82,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(temperature, callback) {
callback(temperature);
}

// ✅ Example usage:
checkTemperature(35, evaluateTemperature);
checkTemperature(22, evaluateTemperature);
checkTemperature(10, evaluateTemperature);
4 changes: 4 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
<head>Week 5 Assignment</head>
<body>
<h1>Look at the console to see the results</h1>
<script src="arrays.js"></script>
<script src="objects.js"></script>
<script src="callbacks.js"></script>


<!-- Before starting, add the javascript files in this html fie -->
</body>
Expand Down
65 changes: 65 additions & 0 deletions objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,27 @@ Expected Output:
// ✍️ Solve it here ✍️


const gamerProfile = {
username: "ShadowSlayer",
level: 5,
isOnline: false
};


function updateOnlineStatus(profile, status) {
profile.isOnline = status;

if (status) {
console.log(`${profile.username} is now online.`);
} else {
console.log(`${profile.username} is now offline.`);
}
}

// ✅ Example usage:
updateOnlineStatus(gamerProfile, true);
updateOnlineStatus(gamerProfile, false);


/*
Task 2: Dress Inventory Checker 👗 👗 👗 👗 👗
Expand Down Expand Up @@ -64,6 +85,23 @@ 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);


/*
Expand Down Expand Up @@ -104,3 +142,30 @@ Features:
*/

// ✍️ Solve it here ✍️


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

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


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


function capitalize(str) {
return str.charAt(0).toUpperCase() + str.slice(1);
}

addFeature(supercar, "turbo");