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

// ✍️ Solve it here ✍️



/*
const inventory = ["Apples", "Bread", "Milk", "Eggs"]
inventory.push("Oranges","Banana")
inventory.shift()
console.log("Updated inventory" ,inventory)
*/


/*
Expand All @@ -40,9 +43,22 @@ Output: "Ali is present."

// ✍️ Write your function here ✍️

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

function isPresent(stdName){
if(students.includes(stdName)){
console.log(`${stdName} is present`)
}
else{
console.log(`${stdName} is absent`)
}

}

isPresent("Ali")

*/


/*
Expand All @@ -67,11 +83,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) {

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

if (player) {
player.score = player.score + scoreToAdd;
} else {
topScorers.push({ name: playerName, score: scoreToAdd });
}
}

function printLeaderboard() {
const sorted = topScorers.sort(function(a, b) {
return b.score - a.score;
});
console.log("Sorted Leaderboard:");
console.log(sorted);
}

// Test
updateScore("Haland", 7);
printLeaderboard();

/*
STRETCH TASK: **The Ultimate Treasure Hunt** 🗺️💎🏴‍☠️
Expand Down
28 changes: 27 additions & 1 deletion callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,16 @@ Expected Output:

// ✍️ Solve it here ✍️

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

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

// Test
sendMessage("Amina", theCallBackFunction);


/*
Expand Down Expand Up @@ -48,7 +57,24 @@ Expected Output:

// ✍️ Solve it here ✍️


function temperatureMessage(temp) {
if (temp > 30) {
console.log(temp + "°C is Hot.");
} else if (temp >= 15) {
console.log(temp + "°C is Warm.");
} else {
console.log(temp + "°C is Cold.");
}
}

function checkTemperature(value, callback) {
callback(value);
}

// Tests
checkTemperature(35, temperatureMessage)
checkTemperature(22, temperatureMessage)
checkTemperature(10, temperatureMessage)


/*
Expand Down
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>
60 changes: 60 additions & 0 deletions objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,24 @@ Expected Output:

// ✍️ Solve it here ✍️

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

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

if (status === true) {
console.log(profile.username + " is now online.");
} else {
console.log(profile.username + " is now offline.");
}
}

updateOnlineStatus(gamerProfile, false)



/*
Expand Down Expand Up @@ -64,7 +82,20 @@ Expected Output:

// ✍️ Solve it here ✍️

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

function checkAvailability(dressItem) {
if (dressItem.inStock === true) {
console.log(dressItem.name + " is available in size " + dressItem.size + ".");
} else {
console.log(dressItem.name + " is out of stock.");
}
}
checkAvailability(dress)

/*
Task 3: Supercar Feature Adder 🚗 🚗 🚗 🚗
Expand Down Expand Up @@ -104,3 +135,32 @@ Features:
*/

// ✍️ Solve it here ✍️

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

function addFeature(car, featureName) {
car.features[featureName] = true

const formattedName =
featureName.charAt(0).toUpperCase() + featureName.slice(1);

console.log(formattedName + " has been added to " + car.model + ".")
}


addFeature(supercar, "turbo");

console.log("Features:");

const featureKeys = Object.keys(supercar.features);

for (let i = 0; i < featureKeys.length; i++) {
const key = featureKeys[i];
console.log("- " + key + ": " + supercar.features[key]);
}