After running this code, what is the value of a
and
b
?
f <- function(x) {
x = x + 1
if ((x %% 2) == 0) {
x = x - 1
}
x = 2*x
}
a <- f(7)
b <- f(12)
Write the output of this code by hand:
f <- function(x) {
if ((x %% 3) == 0) {
cat('woo!')
return(x %/% 3)
}
return(x %% 2)
}
cat(f(9))
cat(f(11))
Write the output of this code by hand:
f <- function(x) {
if (x > 0) {
cat('abc')
x = 2*x
} else if (x <= 0) {
x = abs(x)
cat('cba')
}
cat(x)
}
cat(f(-9))
cat(f(15))
Here are some functions you should be able to write. Any of these may appear (directly or modified) on a quiz or exam! Your function should be able to pass the test functions provided.
isPositiveMultipleOf4Or7(n)
Write the function isPositiveMultipleOf4Or7(n)
that
returns TRUE
if n
is a positive multiple of 4
or 7 and FALSE
otherwise. Note than n
could be
any data type.
Write the test function testIsPositiveMultipleOf4Or7()
that tests the function isPositiveMultipleOf4Or7(n)
for a
variety of values of n
. Consider cases that you might not
expect, such as cases where n
is not a number.