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
53 changes: 46 additions & 7 deletions arrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ Expected Output:
- Updated inventory
*/

// ✍️ Solve it here ✍️





/*
Task 2: Student Attendance Checker 📚✅

Expand All @@ -40,10 +34,28 @@ Output: "Ali is present."

// ✍️ Write your function here ✍️

//Question # 1
const inventory = ["Apples", "Bread", "Milk", "Eggs"];
inventory.push("Oranges","Banana");
inventory.shift();
console.log(inventory);

//end of question 1

//Q2
const students = ["Ali", "Fatima", "Hassan", "Layla"];


function isPresent(name){
if (students.includes(name)){
return console.log(`${name} is present`)
}
else {
return console.log(`${name} is absent`);
}
};
(isPresent("Fatima"));
(isPresent("Zainab"));
//end of Q2

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

// ✍️ Write your functions here ✍️

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


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


function printLeaderboard() {
const sorted = [...topScorers].sort((a, b) => b.score - a.score);
console.log(" Leaderboard:");
sorted.forEach((player, index) => {
console.log(`${index + 1}. ${player.name} - ${player.score} goals`);
});
}


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



Expand Down
27 changes: 27 additions & 0 deletions callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ Expected Output:
*/

// ✍️ Solve it here ✍️
function welcomeMessage(name) {
console.log(`Welcome, ${name}!`);
}

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

sendMessage("Amina", welcomeMessage);



Expand Down Expand Up @@ -48,6 +57,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(tempValue, callback) {
callback(tempValue);
}

checkTemperature(35, evaluateTemperature);
checkTemperature(22, evaluateTemperature);
checkTemperature(10, evaluateTemperature);




Expand Down
4 changes: 3 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
<body>
<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>
75 changes: 74 additions & 1 deletion objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,24 @@ Expected Output:
*/

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

function updateOnlineStatus (gamerProfile, status){
gamerProfile.isOnline = status;
if (status === true) {console.log (`${gamerProfile.username} is now online`)

}
else {console.log(`${gamerProfile.username} is offline`)

}
}

updateOnlineStatus(gamerProfile, true);
updateOnlineStatus(gamerProfile, false);


/*
Expand Down Expand Up @@ -64,8 +81,25 @@ 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 🚗 🚗 🚗 🚗

Expand All @@ -74,16 +108,19 @@ You are building a configurator for a supercar.
Steps:
1. Create an object named `supercar` with the following properties:
- `model` (string): The car's model.
- `price` (number): The base price.
- `price` (number): The base price.+
- `features` (object): An object with a `color` property.

2. Write a function `addFeature` that:
- Takes the `supercar` object and a feature name (string) as arguments.
- Adds the feature to the `features` object and sets it to `true`.
- Logs: "[featureName] has been added to [model]."



3. Use a **for...in loop** to log all the features of the `supercar` object.


Example:
Input:
const supercar = {
Expand All @@ -104,3 +141,39 @@ Features:
*/

// ✍️ Solve it here ✍️

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

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

function addFeatures(supercar, newFeatures) {
for (let i = 0; i < newFeatures.length; i++) {
const feature = newFeatures[i];

if (!supercar.features[feature]) {
supercar.features[feature] = true;
console.log(`${feature} has been added to ${supercar.model}`);
} else {
console.log(`${feature} is already present in ${supercar.model}`);
}
}
}

const newFeatures = ["Turbo", "GPS", "Bluetooth"];
addFeatures(supercar, newFeatures);