StefanoAllesina / BSD-QBio4

Fourth BSD Quantitative Biology Bootcamp @ MBL
GNU General Public License v3.0
4 stars 9 forks source link

my_factorial function in Basic Computing 2 cannot handle edge cases #6

Closed grace-hansen closed 6 years ago

grace-hansen commented 6 years ago

Describe the bug The my_factorial example function in Basic Computing 2 returns incorrect answers if the input is 0 or 1

To Reproduce my_factorial <- function(a = 6){ if (as.integer(a) != a) { print("Please enter an integer!") } else { tmp <- 1 for (i in 2:a){ tmp <- tmp * i } print(paste(a, "! = ", tmp, sep = "")) } }

my_factorial(1) [1] "1! = 2" my_factorial(0) [1] "0! = 0"

FIX: explicitly code in behavior in edge cases

sepalmer commented 6 years ago

Fixed! I changed the if-then so it asks for a positive integer (i.e. not 0 or negative nums), and fixed the for loop so that it returns 1! = 1.