R/bowler_score.R

Defines functions bowler_score

Documented in bowler_score

#' Returns all the bowlers with an average score higher than the given score
#'
#' @param given_score The avg score for which we want all the bowler's names for, as a numeric vector
#'
#' @return `bowler_score` returns a list of names.
#'
#' @examples
#'
#' library(ipl)
#'
#' # Finds all the bowlers with an average score higher than 30
#' bowler_score(30)
#' @importFrom magrittr "%>%"
#' @import dplyr
#'
#' @export
#'

bowler_score <- function(given_score) {
    bowlers_list <- list()

    # Ensures that it is a valid score
    if (!is.numeric(given_score)) {
      stop(paste0("Invalid input: ", given_score, " must be a numeric vector"))
    } else if (given_score < 0) {
      stop("Invalid score. Must be greater than 0.")
    } else if (given_score >= 0 & given_score <= max(bowlers_100$avg)) {
      # Retrieves all the bowlers with an avg score greater than given_score
      bowlers_list <- bowlers_100$player[bowlers_100$avg > given_score]
      return(bowlers_list)
    } else if (given_score > max(bowlers_100$avg)) {
      stop("Invalid score. Please input a lower number.")
    }
}
Swaha294/ipl documentation built on May 10, 2022, 3:23 p.m.