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.
'TRUE'and"FALSE", or the R native objectsTRUE/FALSE? I suggest the latter, which is more idiomatic and efficient (imo). (2) Nested function calls are natural in any language, such asf(1)andg(...)can be combined asg(f(1)). What have you tried? What failed?return, it immediately exits the function with its argument as the return value to the function, soxis ignored inpassGrade. (2) if you agree that using strings should be changed to boolean objects, thenpassGrade <- function(x) return(x >= 70)(or justfunction(x) (x >= 70), since the last expression/value is automatically returned) is enough and much more idiomatic/succinct.