Recap - Simple Logic
Part of the Fundamentals section of Coddy's R journey — lesson 19 of 78.
Challenge
EasyDetermine whether a customer qualifies for a special loyalty reward by combining multiple conditions using comparison and logical operators.
You are provided with the following variables:
purchase_amount <- 150
years_as_customer <- 3
has_loyalty_card <- TRUE
items_bought <- 7
is_holiday_season <- FALSEUse comparison and logical operators to create the following variables:
- Create a variable called
big_spenderthat storesTRUEif the purchase amount is greater than or equal to 100 AND the customer has a loyalty card - Create a variable called
loyal_customerthat storesTRUEif the customer has been a customer for 5 or more years OR has a loyalty card - Create a variable called
qualifies_for_giftthat storesTRUEif the customer bought more than 5 items AND the purchase amount is greater than 100 AND it is NOT the holiday season - Create a variable called
gets_bonus_pointsthat storesTRUEif the customer is a big spender (purchase amount >= 100) AND has been a customer for at least 2 years AND has a loyalty card - Create a variable called
special_rewardthat storesTRUEif the customer qualifies for any of these: (purchase amount > 200) OR (years as customer >= 5 AND has loyalty card) OR (items bought >= 10)
Use the print() function to display big_spender, loyal_customer, qualifies_for_gift, gets_bonus_points, and special_reward in that exact order.
Try it yourself
# Given variables - DO NOT MODIFY
purchase_amount <- 150
years_as_customer <- 3
has_loyalty_card <- TRUE
items_bought <- 7
is_holiday_season <- FALSE
# TODO: Write your code below
# Create the following variables using comparison and logical operators:
# 1. big_spender - TRUE if purchase_amount >= 100 AND has_loyalty_card
# 2. loyal_customer - TRUE if years_as_customer >= 5 OR has_loyalty_card
# 3. qualifies_for_gift - TRUE if items_bought > 5 AND purchase_amount > 100 AND NOT is_holiday_season
# 4. gets_bonus_points - TRUE if purchase_amount >= 100 AND years_as_customer >= 2 AND has_loyalty_card
# 5. special_reward - TRUE if (purchase_amount > 200) OR (years_as_customer >= 5 AND has_loyalty_card) OR (items_bought >= 10)
# Output the results
print(big_spender)
print(loyal_customer)
print(qualifies_for_gift)
print(gets_bonus_points)
print(special_reward)All lessons in Fundamentals
4Operators Part 2
Logical Operators (AND, OR)Logical Operators Part 2 (NOT)Recap - Simple LogicVectorized Logic Part 1Vectorized Logic Part 22Variables and Data Types
Numeric Data TypeInteger Data TypeCharacter Data TypeLogical Data TypeChecking Data TypesNaming ConventionsMissing Values: NARecap - Variable Creation8Loops
For LoopWhile LoopBreakNext (Continue)Recap - FactorialSequence Generation (seq, :)Nested LoopsRecap - Dynamic Input3Operators Part 1
Arithmetic OperatorsInteger Division and ModuloAssignment OperatorsRecap - Simple MathComparison Operators6Basic IO
Print OutputCat for OutputOutput With VariablesReading Input with readline()Type Conversion BasicsRecap - Age CalculatorRecap - True or False9Functions
Declaring a FunctionFunction ArgumentsReturn ValuesRecap - Sigma FunctionRecap - Validation FunctionDefault Parameter Values