JavaScript basics

In this exercise we will practice programming with (JavaScript)[https://developer.mozilla.org/en-US/docs/Web/JavaScript], which we will need to create interactive visualizations in the browser. Today we will focus on processing data in JS, the functions used to plot the data are provided.

We would like to practice the functional style of programming, because it fits well with the d3.js library which we are going to use for the visualizations. This means that instead of for or while loops to iterate over arrays, we would use forEach, map, reduce and filter - if you haven’t seen them before, please take a look at the linked documentation which contains intuitive examples.

Please write your solutions in exercise/exercise.js and open the site exercise/index.html to execute them. Reload the page to re-run your script after applying changes. Remember that you can use the developer tools to interactively call your functions or see the values of variables.

Functions and iteration

isEven

Let’s try a basic function isEven that will check if an integer is divisible by 2.

Then apply it over an array of integers to see the results:

multiply

Now, imagine, you do not know how many numbers in your array. In JS, there is a way to create functions with arbitrary number of parameters. This is also called Spread syntax. Let’s implement a function multiply that computes a product of all numbers specified as parameters. For example, multiply(1,2,3,4,5) should return 120.

Closures

In JS, functions are treated as objects. When a function is created, it has access to all currently visible variables - the newly created function and these variables form a closure. The details are in the documentation about closures.

divisibleBy

Let’s generalize the above example, and create the function divisibleBy which:

The arrow function syntax is a convenient way to construct functions.

Now we have an alternative solution to the previous task const isEvenNew = divisibleBy(2). Try filtering [0, 1, 2, 3, 4, 5, 6] by divisibleBy(3).

increment

Sometimes, in JS, you will have to deal with nested functions. Implement a function increment such that it can be called as increment(100)(2) with the first parameter as an initial value (0 as a default parameter) and second params as a step size (1 as a default parameter).

colorCycle

In plots, we often want to apply different colors, for example to distinguish between lines illustrating different quantities. When making a general mechanism, we can’t predict how many colored objects there are going to be, so we will make the colors repeat in a cycle.

Create a function colorCycle such that

Now create a cycle my_cc with your chosen colors and run showColorCycle(my_cc) to apply the colors to a demo plot on the website index.html.

Range

In the above task, we were writing the sequences [1, 2, ..., N] explicitly, now let’s automate it.

Counting

Advanced exercise. Throwing balls (optional)

We will simulate a ball thrown at angle b with velocity v0. The initial velocity (vx, vy) is:


vx = v0cos(b)

vy = v0sin(b)

The position of the ball at time t is given by:


x(t)=vx * t

y(t)=vy * t + (a * t2 * 0.5)

where a is the acceleration caused by gravity, usually -9.81 m/s2.

Implement a function simulateBall(v0, angle, num_steps, dt, g) such that:

Use the range function to create the array of time points, then map them to the [x, y] values given by the equations above.

const ball_cc = colorCycle(['hsl(160, 100%, 64%)', 'hsl(200, 100%, 64%)', 'hsl(240, 100%, 64%)', 'hsl(120, 100%, 64%)', 'hsl(80, 100%, 64%)']);
plotBall(simulateBall(40, 60), ball_cc());
plotBall(simulateBall(40, 30), ball_cc());
plotBall(simulateBall(40, 45), ball_cc());