Reputation: 11
I am very very new to R and programming in general and I am asking this question as part of a degree program. Don't worry, our professor said we could use StackOverflow. So here is my question: our first requirement is to create a function that returns TRUE
or FALSE
based on a passing grade score of 70. Here is my code for this:
passGrade <- function(x) {
if(x>=70) {
return('TRUE')
}
return('FALSE')
}
The second requirement is to use the results from this function and write a new function that will print "Congrats" is TRUE
and "Try Harder!" if FALSE
. It seems like I need to store the results of the first set of code as a variable, but when I do that it is not correctly read in the second code set. Here is my failed attempt
passGrade <- function(x) {
if(x>=70) {
x <- return('TRUE')
}
x <- return('FALSE')
}
Message <- function(x) {
if(x == 'TRUE') {
return("Congrats")
}
return("Try Harder!")
}
I am sure there is a super simple solution to this, but I am not having any luck.
Upvotes: 1
Views: 960
Reputation: 1611
Below is a simple solution, you can directly return TRUE
or FALSE
based on the condition and then pass this to Message
function to print out the required output, as shown below:
passGrade <- function(x) {
if(x>=70) {
return(TRUE)
}
return(FALSE)
}
Message <- function(x) {
if(x == TRUE) {
return("Congrats")
}
return("Try Harder!")
}
Message(passGrade(60))
Message(passGrade(80))
Output:
[1] "Congrats"
[1] "Try Harder!"
Upvotes: 2