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

second assignment #22

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
106 changes: 106 additions & 0 deletions arrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ Expected Output:
*/

// ✍️ Solve it here ✍️
const inventory = ['apples','Bread','milk','eggs']
inventory.push('oranges','Bananas')
inventory.pop()
console.log('updated inventory ',inventory)



Expand All @@ -40,6 +44,17 @@ Output: "Ali is present."

// ✍️ Write your function here ✍️

const student = ['ali','fatima','hassan','layla'];
function isPresent(name){
if (student.includes(name)){
return `${name}is present.`;
}else{
return `${name}is absent.`;
}

}console.log(isPresent('ali'));
console.log(isPresent(''));




Expand All @@ -66,6 +81,41 @@ 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(player => player.name === playerName);

if (player) {
// If the player exists, add the score to their total
player.score += scoreToAdd;
} else {
// If the player doesn't exist, add them to the leaderboard
topScorers.push({ name: playerName, score: scoreToAdd });
}
}

// Function to print the leaderboard
function printLeaderboard() {
// Sort the leaderboard in descending order of scores
const sortedLeaderboard = [...topScorers].sort((a, b) => b.score - a.score);

// Print the leaderboard
console.log("Leaderboard:");
sortedLeaderboard.forEach((player, index) => {
console.log(`${index + 1}. ${player.name} - ${player.score}`);
});
}
updateScore("Ronaldo", 2);
updateScore("Mbappe", 6);
printLeaderboard();





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

*/
// Input Data
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"];

function findClue(cluesArray, clueName) {
if (cluesArray.includes(clueName)) {
return `Clue ${clueName} found!`;
} else {
return `Clue ${clueName} is missing, search again!`;
}
}

// 2. Function to decipher scrambled messages
function decipherMessage(messages) {
const deciphered = messages.map(message => message.split("").reverse().join(""));
return deciphered;
}

// 3. Function to follow the treasure map steps
function followSteps(steps) {
for (let i = 0; i < steps.length; i++) {
console.log(`Step ${i + 1}: ${steps[i]}`);
if (steps[i] === "Danger") {
console.log("Stopped at danger. Cannot continue.");
return false; // Journey ends at danger
}
}
return steps[steps.length - 1] === "Treasure";
}

function ultimateTreasureHunt(cluesArray, messagesArray, stepsArray) {

const decipheredClues = decipherMessage(messagesArray);

// Check all clues
for (let clue of cluesArray) {
const clueCheck = findClue(decipheredClues, clue);
console.log(clueCheck);
if (clueCheck.includes("missing")) {
console.log("The treasure remains hidden. Try again!");
return;
}
}

// Follow the steps
const treasureFound = followSteps(stepsArray);

// Determine the final result
if (treasureFound) {
console.log("Congratulations! You found the ultimate treasure!");
} else {
console.log("The treasure remains hidden. Try again!");
}
}

70 changes: 68 additions & 2 deletions callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@ Expected Output:
*/

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

function sendMessage(userName, callback) {
callback(userName);
}
sendMessage("Amina", theCallBackFunction);

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




Expand Down Expand Up @@ -47,6 +62,23 @@ Expected Output:
*/

// ✍️ Solve it here ✍️
function temperatureEvaluator(temp) {
let status;
if (temp > 30) {
status = "Hot";
} else if (temp >= 15 && temp <= 30) {
status = "Warm";
} else {
status = "Cold";
}
console.log(`${temp}°C is ${status}.`);
}

// Example usage:
checkTemperature(35, temperatureEvaluator);
checkTemperature(22, temperatureEvaluator);
checkTemperature(10, temperatureEvaluator);




Expand All @@ -71,5 +103,39 @@ Expected Output:
- If user's input is "10": "Correct!"
- If user's input is "15": "Incorrect. The correct answer is 10."
*/

// ✍️ Solve it here ✍️
function checkTemperature(temp, callback) {
callback(temp);
}

function temperatureEvaluator(temp) {
let status;
if (temp > 30) {
status = "Hot";
} else if (temp >= 15 && temp <= 30) {
status = "Warm";
} else {
status = "Cold";
}
console.log(`${temp}°C is ${status}.`);
}

// Example usage:
checkTemperature(35, temperatureEvaluator); // "35°C is Hot."
checkTemperature(22, temperatureEvaluator); // "22°C is Warm."
checkTemperature(10, temperatureEvaluator); // "10°C is Cold."

function evaluateAnswer(question, correctAnswer, callback) {
const userAnswer = prompt(question);
callback(userAnswer.trim(), correctAnswer);
}

function answerEvaluator(userAnswer, correctAnswer) {
if (userAnswer.toLowerCase() === correctAnswer.toLowerCase()) {
console.log("Correct!");
} else {
console.log(`Incorrect. The correct answer is ${correctAnswer}.`);
}
}

// Example usage:
evaluateAnswer("What is 5 + 5?", "10", answerEvaluator);
6 changes: 4 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
<head>Week 5 Assignment</head>
<body>
<h1>Look at the console to see the results</h1>

<!-- Before starting, add the javascript files in this html fie -->

</body>
<script src="arrays.js"></script>
<script src="callbacks.js"></script>
<script src="objects.js"></script>
</html>
66 changes: 66 additions & 0 deletions objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ Expected Output:
*/

// ✍️ Solve it here ✍️
function updateOnlineStatus(profile, status) {
// Update the isOnline property
profile.isOnline = status;

// Log the appropriate message based on the status
if (status) {
console.log(`${profile.username} is now online.`);
} else {
console.log(`${profile.username} is now offline.`);
}
}



Expand Down Expand Up @@ -63,6 +74,24 @@ Expected Output:
*/

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

// Define the checkAvailability function
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.`);
}
}

// Example usage
checkAvailability(dress);




Expand Down Expand Up @@ -104,3 +133,40 @@ Features:
*/

// ✍️ Solve it here ✍️
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);


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.`);
}
}

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

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

addFeature(supercar, "turbo");

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