Christopher Fremond
0d8f2f28f3
|
1 month ago | |
---|---|---|
.. | ||
README.md | 1 month ago |
README.md
Only If
Mindful AI mode
Context
You are close to getting the ultimate power! Are you ready to command your robot using conditions? In JavaScript, conditions allow you to control what your robots do based on different situations.
Let's have some fun with it!
AI-Powered Learning Techniques
Interactive Learning Technique: This type of prompt engages you in active problem-solving by providing challenges or tasks that require applying concepts. This can work to compare your results with your peers!
Find the examples across the subject ;)
Concepts
Conditions in JavaScript
Conditions in JavaScript are like decision points, they allow you to execute different actions based on whether a condition is true or false.
if (condition) {
// Code to execute if the condition is true
} else {
// Code to execute if the condition is false
}
For example:
let batteryLevel = 75;
if (batteryLevel > 50) {
console.log("Robot is ready to patrol!");
} else {
console.log("Robot needs to recharge.");
}
Truthy and Falsy
In JavaScript, all the values are either truthy
or falsy
. Truthy values validate conditions, while falsy values do not.
Falsy Values:
undefined
andnull
- Numbers:
0
andNaN
- Empty string:
''
- Boolean:
false
All other values are truthy
!
Logical Operators
AND Operator (&&)
The AND operator groups conditions:
// Ex: let robot = { status: 'active', battery: 75, name: 'RoboGuard' };
if (robot.status === "active" && robot.battery > 50) {
console.log("Robot" + robot.name + "is active and has sufficient battery.");
}
//Output : Robot RoboGuard is active and has sufficient battery.
OR Operator (||)
The OR operator groups conditions:
if (robot.type === "security" || robot.type === "assistant") {
console.log(robot.name + "is available for tasks.");
}
else if
Keyword
Chain conditions using else if:
if (temperature < 8) {
console.log("Very cold!");
} else if (temperature < 16) {
console.log("Still too cold...");
} else if (temperature < 24) {
console.log("Getting warmer");
} else if (temperature < 32) {
console.log("Nice :)");
} else {
console.log("Too hot!!!");
}
Prompt Example
:
- "Give me an easy coding challenge involving conditions in JavaScript, and provide feedback on my solution."
Instructions
Task 1:
Your Robot must always seek the truth.
- Check if the value of the provided variable
truth
is truthy, log the string:The truth was spoken.
- Otherwise, log the string:
Lies !!!!
because the value of the provided variabletruth
is falsy.
Task 2:
Your RoboGuard's traveling company
has a special promotion for robot members aged between 18 (included) and 25 (included).
NB: The variable ticket has already been declared, so do not declare it again.
1- Assign the message "You cannot benefit from our special promotion" to the variable ticket
.
2- Use an if statement to check that all these conditions are true:
user.age
must be greater than or equal to18
.user.age
must be less than or equal to25
.user.activeMembership
must betrue
.
3- If all conditions are true, update the variable ticket
with the message: "You can benefit from our special promotion".
Hint : use an AND Operator in your condition!
Task 3:
Your RoboGuard is selling plane tickets, each costing 9.99$
. The RoboGuard must confirm that the customer robot has the means to buy this ticket.
The customer robot may have enough cash or be in possesion of a voucher.
Check if the provided variable customer can afford the ticket:
If the customer has enough cash
(customer.cash
property)
OR If the customer has a voucher
(customer.hasVoucher
property is true)
If so, increment
the provided variable ticketSold
value by 1
.
Hint : use OR Operator in your condition!