Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define a function named greet that prints a message.
R Programming
greet <- function() {
print([1])
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting quotes around the message
Putting print inside the string
✗ Incorrect
The print function needs a string inside quotes to display the message.
2fill in blank
mediumComplete the code to call the greet function.
R Programming
[1]() Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using print instead of the function name
Forgetting parentheses
✗ Incorrect
To run the function, use its name followed by parentheses.
3fill in blank
hardFix the error in the function definition to accept a name parameter.
R Programming
greet <- function([1]) { print(paste("Hello," , name)) }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different parameter name than inside the function
Forgetting to add a parameter
✗ Incorrect
The function needs a parameter named 'name' to use it inside.
4fill in blank
hardFill both blanks to create a function that adds two numbers and returns the result.
R Programming
add_numbers <- function(a, b) {
result <- a [1] b
return([2])
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction instead of addition
Returning the wrong variable
✗ Incorrect
The function adds a and b, then returns the result variable.
5fill in blank
hardFill all three blanks to create a function that checks if a number is positive, negative, or zero.
R Programming
check_number <- function(num) {
if (num [1] 0) {
return("Positive")
} else if (num [2] 0) {
return("Negative")
} else {
return([3])
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong comparison operators
Returning a variable instead of a string for zero
✗ Incorrect
The function uses > and < to check positive or negative, and returns "Zero" if neither.