Recap - Validation Function
Part of the Fundamentals section of Coddy's R journey — lesson 50 of 78.
Challenge
EasyCreate a function called is_valid_password that takes one argument: password (a character string). The function should validate the password based on the following criteria:
- The password length must be at least 8 characters
- The password length must be no more than 20 characters
The function should return TRUE if the password meets both criteria, and FALSE otherwise.
Use nchar() to get the length of the password string.
Read a password from input and call your function with this value. Print the returned result.
For example, if the input is mypassword123, the output should be:
[1] TRUEIf the input is short, the output should be:
[1] FALSEIf the input is thispasswordiswaytoolongtobevalid, the output should be:
[1] FALSETry it yourself
# Read input
con <- file("stdin", "r")
password <- suppressWarnings(readLines(con, n = 1))
# TODO: Create the is_valid_password function below
# The function should:
# - Take a password string as argument
# - Check if length is at least 8 characters
# - Check if length is no more than 20 characters
# - Return TRUE if both conditions are met, FALSE otherwise
# Hint: Use nchar() to get the string length
# Call the function and print the result
print(is_valid_password(password))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