Skip to content

Commit

Permalink
Merge pull request #173 from coderefinery/thor/R-tdd-exercise
Browse files Browse the repository at this point in the history
add R version of TDD exercise
  • Loading branch information
wikfeldt authored Apr 27, 2021
2 parents dfd6bd1 + 1dcda6b commit c6ee75d
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 additions & 1 deletion content/code/tdd_sol.R
Original file line number Diff line number Diff line change
@@ -1 +1,40 @@
WRITEME
# define the function
fizz_buzz <- function(number){
if(!number%%1==0 | number < 0) {
stop("non-integer or negative input not allowed!")
}
if(number%%3 == 0 & number%%5 == 0) {
return('FizzBuzz')
}
else if(number%%3 == 0) {
return('Fizz')
}
else if (number%%5 == 0){
return('Buzz')
}
else {
return(number)
}

}

# apply it to the numbers 1 to 50
for (number in 1:50) {
print(fizz_buzz(number))
}


library(testthat)

test_that("Test FizzBuzz", {
expect_equal(fizz_buzz(1), 1)
expect_equal(fizz_buzz(2), 2)
expect_equal(fizz_buzz(3), 'Fizz')
expect_equal(fizz_buzz(4), 4)
expect_equal(fizz_buzz(5), 'Buzz')
expect_equal(fizz_buzz(15), 'FizzBuzz')

expect_error(fizz_buzz(-1))
expect_error(fizz_buzz(1.5))
expect_error(fizz_buzz('rabbit'))
})

0 comments on commit c6ee75d

Please sign in to comment.