I was messing around online at work the other day when our phone systems went down. I came across the R
programming language. Now I'm not guru with programming by any means but I like to mess around with different languages here and there, I believe it helps me to become a better programmer. So I decided that I would try to solve the fizzbuzz
challenge in R
for those of you that don't know what fizzbuzz
is:
Write a program that prints the numbers from 1 to 100. But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz"
This is the first thing I've ever written in R
and I would like to have some critique done on what I've accomplished so far:
fizzBuzz = function(range, x, y){
for (i in seq(1, range, by=1)){
if (i %% x == 0 & i %% y == 0){
print('FizzBuzz')
}
else if (i %% y ==0){
print('Buzz')
}
else if (i %% x == 0){
print('Fizz')
}
else{
print(i)
}
}
}
fizzBuzz(100, 3, 5)
Some key things I'd like to focus on, per norm feel free to critique everything;
- Is it a little bit over kill to put this all in a function, obviously I could do it simply without the function, but is this considered good practice in
R
to put your solutions inside of functions? I only ask because I looked around at peoples code and didn't see very many functions in it. - Are there easier ways to create the range? I feel like
seq(from, to, by=1)
is a little harder to understand thenrange(from, to)
data.table
package, meaning I wouldn't need to declare any custom functions. \$\endgroup\$