Menu
Coddy logo textTech

Recap - Validation Function

Part of the Fundamentals section of Coddy's R journey — lesson 50 of 78.

challenge icon

Challenge

Easy

Create 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] TRUE

If the input is short, the output should be:

[1] FALSE

If the input is thispasswordiswaytoolongtobevalid, the output should be:

[1] FALSE

Try 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