📖 Scenario: You are helping a small shop decide if a customer qualifies for a special discount. The shop has simple rules based on the customer's age and membership status.
🎯 Goal: Build a Ruby program that uses logical operators && and || to check if a customer qualifies for a discount.
📋 What You'll Learn
Create variables with exact names and values as instructed
Use logical operators && and || exactly as shown
Print the exact output string as instructed
💡 Why This Matters
🌍 Real World
Stores and businesses often use simple rules to decide if customers get discounts or special offers based on their age or membership.
💼 Career
Understanding logical operators is essential for writing conditions in programs, which is a key skill for any software developer.
Progress0 / 4 steps
1
Create customer data variables
Create a variable called age and set it to 25. Create another variable called is_member and set it to true.
Ruby
Hint
Use = to assign values. For is_member, use true without quotes.
2
Create discount eligibility rules
Create a variable called is_senior and set it to false. Create a variable called min_age and set it to 60.
Ruby
Hint
Remember to use false without quotes for boolean false.
3
Use logical operators to check discount eligibility
Create a variable called qualifies_for_discount that is true if the customer is a member and their age is less than min_age, or if they are a senior. Use && and || operators exactly as shown.
Ruby
Hint
Use parentheses to group conditions clearly. qualifies_for_discount should be true if either condition is met.
4
Print the discount eligibility result
Print the exact text "Discount eligibility: true" or "Discount eligibility: false" depending on the value of qualifies_for_discount. Use puts and string interpolation with #{}.
Ruby
Hint
Use puts and "Discount eligibility: #{qualifies_for_discount}" to print the result.
Practice
(1/5)
1. What does the Ruby logical operator && do?
easy
A. Checks if two values are equal
B. Returns true if at least one condition is true
C. Reverses the truth value of a condition
D. Returns true only if both conditions are true
Solution
Step 1: Understand the meaning of &&
The && operator means 'and' in Ruby, so it returns true only if both sides are true.
Step 2: Compare with other operators
|| returns true if one or both are true, and ! reverses truth. Equality checks with ==.
Final Answer:
Returns true only if both conditions are true -> Option D
Quick Check:
&& means AND = D [OK]
Hint: && means both must be true to get true [OK]
Common Mistakes:
Confusing && with ||
Thinking && reverses values
Mixing && with equality operator
2. Which of the following is the correct way to write a NOT operation in Ruby?
easy
A. !x
B. ~x
C. not x
D. x != true
Solution
Step 1: Identify Ruby's NOT operator syntax
Ruby uses ! before a value to reverse its truth value.
Step 2: Check other options
not x is valid but less common; ~x is bitwise NOT; x != true is a comparison, not logical NOT.
Final Answer:
!x -> Option A
Quick Check:
Logical NOT = C [OK]
Hint: Use ! before a value to reverse true/false [OK]
Common Mistakes:
Using ~ instead of ! for logical NOT
Confusing != with NOT operator
Using 'not' keyword instead of !
3. What is the output of this Ruby code?
puts (true || false) && !false
medium
A. nil
B. false
C. true
D. SyntaxError
Solution
Step 1: Evaluate inside parentheses
true || false is true because at least one side is true.
Step 2: Evaluate the NOT operator
!false is true because NOT false is true.
Step 3: Evaluate the AND operator
Now we have true && true, which is true.
Final Answer:
true -> Option C
Quick Check:
(true || false) && !false = A [OK]
Hint: Evaluate parentheses, then !, then && [OK]
Common Mistakes:
Evaluating && before ||
Forgetting to apply ! operator
Assuming false because of || false
4. Find the error in this Ruby code snippet:
if !true || false
puts "Yes"
else
puts "No"
end
medium
A. No error; code runs and prints "No"
B. The ! operator cannot be used with true
C. The || operator should be && here
D. Missing parentheses around !true
Solution
Step 1: Check syntax of logical operators
Using ! with true is valid and reverses it to false. The || operator is also valid here.
Step 2: Evaluate the condition
!true is false, so condition is false || false, which is false. The else branch runs printing "No".
Step 3: Confirm no syntax errors
The code syntax is correct and will run without errors.
Final Answer:
No error; code runs and prints "No" -> Option A
Quick Check:
Logical operators used correctly = B [OK]
Hint: !true is valid; check logic, not syntax [OK]
Common Mistakes:
Thinking !true is invalid
Confusing || with &&
Expecting syntax error without parentheses
5. You want to check if a variable x is NOT between 10 and 20 inclusive in Ruby. Which condition correctly uses logical operators?
hard
A. !(x >= 10 && x <= 20)
B. x < 10 || x > 20
C. x <= 10 && x >= 20
D. !(x < 10 || x > 20)
Solution
Step 1: Understand the range condition
Being between 10 and 20 inclusive means x >= 10 && x <= 20.
Step 2: Find the NOT of the range
The NOT of being between 10 and 20 is being less than 10 OR greater than 20, so x < 10 || x > 20.
Step 3: Check options
!(x >= 10 && x <= 20) negates the range but is correct logically; x < 10 || x > 20 directly expresses the NOT condition without negation. x <= 10 && x >= 20 is impossible (x can't be both <=10 and >=20). !(x < 10 || x > 20) negates the NOT condition, so it's the original range.
Final Answer:
x < 10 || x > 20 -> Option B
Quick Check:
NOT between 10 and 20 = A [OK]
Hint: NOT between means less than OR greater than [OK]
Common Mistakes:
Using AND instead of OR for NOT condition
Confusing negation placement
Choosing impossible conditions
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.