I heard this was a common interview question, any ideas what is off here, thank you.
for(i in 1:100){
if(i%15==0){
print('fizzbuzz')
} else
if (i%3==0){
print("fizz")
} else
if (i%5==0) {
print("buzz")
} else
(print(i))
}
}
I've just made R FizzBuzz check on myself:
If you don't understand any function here you should read manual.
Your
for
loop solution may be not the best on the R dev interview. It may be interpreted as lack of skills to use R's vectorization feature.I'd place the curly braces in different spots, and you need to correct the operator --
%%
instead of%
.But the basic idea is sound: get the special 'fizzbuzz' case out the way first, then deal with remaining (exclusive) cases.
Here are the first 16 results: