This assessment consists of recursive functions you need to write. This is an closed book assessment.
You can move through the questions with the arrows at the upper right next
to the 1 of indicator so if you're not
sure how to write one function move on to another one and come back if you
have time at the end. I want to see how much you do know. Note: you
can also click on thingsLikeThis
in these instructions and
the questions to copy them to the clipboard to avoid spelling mistakes.
(I.e. click to copy and then ⌘-v to paste wherever you want.)
When you are done, please click on your Github username above and
submit a GitHub pull request of the branch
and request me as a reviewer.
Write a recursive function named factorial
that takes a
single non-negative integer argument and returns the nth factorial
number which is defined as the product of the integers from 0 to N,
inclusive except that the 0th factorial,
i.e. factorial(0)
, is 1.
Write a recursive function named fibonacci
that
computes the nth element of the Fibonacci sequence, 0, 1, 1, 2, 3, 5,
8, etc. After the first two items, i.e. fibonacci(0)
which is 0 and fibonacci(1)
which is 1, each item in the
sequence is the sum of the two previous items.
Write a recursive function named sumSquares
that takes
a single non-negative integer argument, n, and returns the
sum of the squares of the numbers from 0 to n, inclusive.
Write a recursive function named maximum
that takes an
array of numbers and returns the maximum value in the array. If the
array is empty, it should return the special
value -Infinity
.
Write a recursive function named treeMap
that takes two
arguments, a tree and a function. Trees are either objects
with left
and right
properties or leaf
values that can be anything. The function isLeaf
, which
is already defined for you in the starter code, will tell you whether
a given value is a leaf. The function should return a new tree with
the same structure as the original but with each leaf replaced with
the result of calling the passed in function with the old leaf value
as an argument. For instance:
treeMap({left: 10, right: { left: 2, right: 3 } }, (x) => x * 2)
should return:
{ left: 20, right: { left: 4, right: 6 } }
Write a recursive function named sumPrimesBelow
that
takes a single integer argument greater than 1 and returns the sum
of the prime numbers less than or equal to that argument. For
instance sumPrimesBelow(10)
should return 17. You
should use the function isPrime
, which has been defined
for you in the starter code, to test if a given number is prime.
Write a recursive function named nvwls
(short for
noVowels) that takes a string as an argument and returns a string
with all the same characters as the original but with all vowels (a,
e, i, o, and u) removed.
Write a recursive function named caesar
that takes a
string and a key (which happens to be a number) and returns the
string encoded by rotating each character using
the rotate
function, which has been defined for you in
the starter code and which rotates a character around the alphabet
based on the key
argument. As simple test of whether
this is working applying caesar
twice with the key 13
should get you back to where you started,
e.g.:
caesar(caesar('any string', 13), 13) === 'any string'
Write a recursive function named toList
that takes a
single array argument and returns an object representation of the
array in the following format, called a list. (Note: in this context
a “list” is not an array even though sometimes in Javascript
we’ll informally call an array a list.)
null
first
and rest
.
first
property is the first item
of the array.
rest
property is a list containin
the remaining elements of the array.
For instance, the array [1, 2, 3]
would be represented as:
{ first: 1, rest: { first: 2, rest: { first: 3, rest: null } } }
Write a recursive function named map
that takes two
arguments, a list as returned by the toList
function
from the previous question and a function, and returns a new list
represented in the same way with each first
value
replaced with the result of passing the original first
value to the function argument.
For instance:
map({ first: 1, rest: { first: 2, rest: { first: 3, rest: null } } }, (n) => n * 10)
should return:
{ first: 10, rest: { first: 20, rest: { first: 30, rest: null } } }