- Create a function called
repeatwhich takes two arguments:- The first argument should be an arbitrary function,
fn - The second argument should be a number,
n
- The first argument should be an arbitrary function,
repeatshould loopntimes- Each iteration of the loop, it should call
fn - Create two more functions called
helloandgoodbye:helloshould log the string'Hello world'goodbyeshould log the string'Goodbye world'
- Use your
repeatfunction to call thehellofunction five times:repeat(hello, 5) - Use your
repeatfunction to call thegoodbyefunction five times:repeat(goodbye, 5)
-
Create a function called
createGreeterwhich takes a single string argument,greeting -
createGreetershould return a function -
The function which is returned should take a single argument,
name -
The function which is returned should log the greeting and name combined. So if
greetingwas'Bonjour', andnamewas'Sofia'the function should log'Bonjour Sofia'. -
Create a "Hello" greeter by calling the
createGreeterfunction, passing'Hello'as thegreetingargument. -
Create a "Bonjour" greeter by calling the
createGreeterfunction, passing'Bonjour'as thegreetingargument -
Call your two greeters, passing the names
'Anna'and'Sofia'as thenameargument. Your output should be:Hello Anna Bonjour Sofia
- A turtle's movements can be represented by an array which looks like this:
[3, 4]. The first item in the array represents the number of steps the turtle takes forwards. The second number in the array is the number of steps the turtle takes to the left. - Here is an array of different movements made by a turtle:
[[0, 0], [0, 5], [-1, -3], [-3, 1], [2, -4], [3, 2]]. - Use the
filtermethod to remove any items where the turtle moves backwards or to the right (i.e. where either the first of second number is an item is negative). - Use the
mapmethod to create a new array containing how many steps the turtle makes in total with each movement (i.e. the first and second number added together). - Use the
forEachmethod to log out how many steps the turtle took in each case.