---
# System prepended metadata

title: Exercises for Session 2

---

# Exercises for Session 2

## Array

1. Use an array to represent an [identity matrix](https://en.wikipedia.org/wiki/Identity_matrix) of 5 x 5.

1. Given the vectors $A = [1.1, 3, 5]$ and $B = [-2, 3.1, 0]$, use two arrays to represent them, store their [vector sum](https://en.wikipedia.org/wiki/Euclidean_vector#Addition_and_subtraction) in a third array and then log the result to the console.

1. Generate an array that contains the first 10 natural numbers.

1. Print all the elements of the previous array in reverse order using a `for` loop.

1. Refactor the solution of the previous exercise to use a `for ... of`.

1. Given the array:

   ```
   const mjStats = [32292, 6672, 5633];
   ```

   Destructure it into the variables `points`, `assists`, and `rebounds` and log each of them to the console using a _template literal_ that also prints the name of the stat.

1. Given the vector $[1, 3, −5]$ calculate its [dot product](https://en.wikipedia.org/wiki/Dot_product#Algebraic_definition) with itself using a reducer.

1. Given the matrices

   $$
     A =
     \left[ {\begin{array}{cc}
       1 & -1 \\
       0 & 42 \\
     \end{array} } \right]

     B =
     \left[ {\begin{array}{cc}
       -2 & 33 \\
       7 & -2 \\
     \end{array} } \right]
   $$

   Represent them using nested arrays and store the result of their [sum](https://en.wikipedia.org/wiki/Matrix_addition#Entrywise_sum) using the same method.

## Object

1. Given this object describing a rectangle:

   ```javascript
   const rect = {
     width: 40,
     height: 30,
   };
   ```

   Expand `rect` so that it also stores the `area` and the `perimeter` of the rectangle.

1. Given this object describing an NBA players stats in the current season:

   ```javascript
   const player = {
     name: "Luka Doncic",
     stats: {
       wins: 13,
       losses: 8,
       minPlayedPerGame: 37.1,
       pointsPerGame: 34.4,
     },
   };
   ```

   Write a program that:

   1. Calculates the total games played by the player and prints the value to the console using a template literal containing the substring "Total games this season: ".
   1. Calculates the total minutes played by the player and prints the value to the console using a template literal containing the substring "Total played minutes this season: ".
   1. Calculates the total points scored by the player and prints the value to the console using a template literal containing the substring "Total points this season: ".

1. Given the following data:
   ```javascript
   const transactions = [
     {
       amount: 3.14,
       date: { month: 7, day: 21 },
     },
     {
       amount: 22,
       date: { month: 6, day: 22 },
     },
     {
       amount: 33.14,
       date: { month: 6, day: 17 },
     },
     {
       amount: 2.15,
       date: { month: 7, day: 21 },
     },
     {
       amount: 32.04,
       date: { month: 6, day: 27 },
     },
   ];
   ```
   Write a program that:
   1. Determines the total number of transactions during June as well as their total sum and prints the result to the console in this format:
      ```
      JUNE
      Transactions: <theResultObtained>
      Total USD volume: <theResultObtained>
      ```
   1. Does the same for July.
1. Given the array of objects:
   ```javascript
   const dataPoints = [
     { x: 1, y: 32, z: 3.1 },
     { x: 1.2, y: 52 },
     { x: 8, y: 2, z: 4.1, w: 2.2 },
   ];
   ```
   Write a program that determines the dimension of each data point (how many data items it contains) and prints the iformation to the console using a descriptive message.
1. Given the array of objects:
   ```javascript
   const tweets = [
     {
       text: "Ipsum repellat neque eaque aliquid nesciunt",
       emotion: "anger",
     },
     {
       text: "Dicta quae quos fuga nam facere",
       emotion: "anger",
     },
     {
       text: "Quas dolor excepturi voluptate in asperiores",
       emotion: "happiness",
     },
     {
       text: "Ut tempore cum voluptate molestias rerum esse quas ",
       emotion: "happiness",
     },
     {
       text: "Tempora eius optio odit",
       emotion: "neutral",
     },
   ];
   ```
   Write a program that builds an object whose keys are each of the emotions found in the original object and whose values are arrays containing the text of each of the tweets that belong to a particular emotion category.

## Set

1. Determine the total number of emotion categories in the `tweets` object given above using a set.