Conquering Logical Operators
2. Introduction
Today I learned about logical operators in Javascript. This was in line with my learning of ‘if else’ in Js. To solidify my understanding of the course I am taking in Udemy I asked Chat gtp to create two challenges for me to solve. Here’s how it went.
3. Challenge Description
Challenge 1: (Can you ride?)
This challenge was to write code that checked if a person met a certain height and age requirement and utilized the && operator to check multiple conditions.
Challenge 2: (Are you eligible?)
This challenge had me write code that used the | | operator to determine eligibilty based on age and parental permission.
4. My Approach
Js variables are a new concept to me but after a couple of weeks of being introduced to them I am now getting the bigger picture in my mind. Meaning, you usually tackle a problem by creating the variables first. For challenge #1 I quickly realized that I would need a height and age variable. After that, it was simply creating the if and else portion of the code.
5. My Code
// Challenge 1: Can You Ride?
let height = 50; // Example input
let age = 12; // Example input
if (height >= 48 && age >= 10) {
console.log("You can ride the roller coaster!");
} else {
console.log("Sorry, you can't ride yet.");
}
// Challenge 2: Are You Eligible?
let age = 16; // Example input
let hasPermission = true; // Example input
if (age > 18 || hasPermission) {
console.log("You are eligible for the membership!");
} else {
console.log("You are not eligible for the membership.");
}
6. Lessons Learned
What I learned was that in my else condition it was too strict if I used the &&. I wasn’t aware that it would be an issue. The else condition did not log to the console until I changed it to the OR operator.
7. Next Steps
I plan to ask Chat to create more complex if/else challenges so that I can feel more confident using these foundational concepts.
8. Conclusion
Tackling challenges like this makes coding feel less intimidating. It’s not about getting it right on the first try—it’s about persistence and learning. On to the next one!