From ad314308ebe14a61da57ba1ca6d26af985f46a68 Mon Sep 17 00:00:00 2001 From: Kjartan Thor Wikfeldt Date: Tue, 27 Apr 2021 12:14:36 +0200 Subject: [PATCH 1/2] add R version of TDD exercise --- content/code/tdd_sol.R | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/content/code/tdd_sol.R b/content/code/tdd_sol.R index d49c92c..0add312 100644 --- a/content/code/tdd_sol.R +++ b/content/code/tdd_sol.R @@ -1 +1,40 @@ -WRITEME \ No newline at end of file +# 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')) +}) From 1dcda6b39e19a6ae6fba35c64496ae21236a6556 Mon Sep 17 00:00:00 2001 From: Kjartan Thor Wikfeldt Date: Tue, 27 Apr 2021 15:16:39 +0200 Subject: [PATCH 2/2] Update content/code/tdd_sol.R Co-authored-by: Marijn van Vliet --- content/code/tdd_sol.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/code/tdd_sol.R b/content/code/tdd_sol.R index 0add312..98d3765 100644 --- a/content/code/tdd_sol.R +++ b/content/code/tdd_sol.R @@ -2,7 +2,7 @@ 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') }