|
1 |
| -# Exercises |
| 1 | +# 📘 JavaScript Exercises |
2 | 2 |
|
3 |
| -## The Sum of a Range |
| 3 | +## Table of Contents |
| 4 | +1. [🌈 The Sum of a Range](#-the-sum-of-a-range) |
| 5 | +2. [🔄 Reversing an Array](#-reversing-an-array) |
| 6 | +3. [📝 Working with Lists](#-working-with-lists) |
| 7 | +4. [🔍 Deep Comparison](#-deep-comparison) |
4 | 8 |
|
5 |
| -1. Write a `range` function that takes two arguments, `start` and `end`, and returns an array containing all the numbers from `start` up to and including `end`. |
6 |
| -2. Write a `sum` function that takes an array of numbers and returns the sum of these numbers. |
7 |
| -3. Modify the `range` function to take an optional third argument that indicates the “step” value used when building the array. Ensure that it works with both positive and negative step values. |
| 9 | +## 🌈 The Sum of a Range |
8 | 10 |
|
9 |
| -## Reversing an Array |
| 11 | +1. **`range` Function** |
| 12 | + Create a `range` function that takes two arguments, `start` and `end`, and returns an array containing all numbers between these values, inclusive. |
| 13 | + |
| 14 | +2. **`sum` Function** |
| 15 | + Write a `sum` function that takes an array of numbers and calculates the sum of all elements. |
10 | 16 |
|
11 |
| -1. Write a function `reverseArray` that takes an array as an argument and produces a new array that has the same elements in the inverse order. |
12 |
| -2. Write a function `reverseArrayInPlace` that modifies the array given as an argument by reversing its elements without using the standard `reverse` method. |
| 17 | +3. **Adding a Step Argument** |
| 18 | + Modify the `range` function to take an optional third argument, `step`, that allows the function to skip numbers. Ensure it works with both positive and negative values! |
13 | 19 |
|
14 |
| -## A List |
| 20 | + **Hints**: |
| 21 | + - Think about how the `step` value changes the array construction. |
| 22 | + - Consider both forward and backward counting based on the step value. |
15 | 23 |
|
16 |
| -1. Write a function `arrayToList` that builds up a list structure like the one shown when given `[1, 2, 3]` as an argument. |
17 |
| -2. Write a function `listToArray` that produces an array from a list. |
18 |
| -3. Write a helper function `prepend` that takes an element and a list and creates a new list that adds the element to the front of the input list. |
19 |
| -4. Write a function `nth` that takes a list and a number and returns the element at the given position in the list (with zero referring to the first element) or `undefined` when there is no such element. |
20 |
| -5. Write a recursive version of `nth`. |
| 24 | +## 🔄 Reversing an Array |
21 | 25 |
|
22 |
| -## Deep Comparison |
| 26 | +1. **`reverseArray` Function** |
| 27 | + Write a function that takes an array and returns a new array with its elements in reverse order. |
23 | 28 |
|
24 |
| -1. Write a function `deepEqual` that takes two values and returns `true` only if they are the same value or are objects with the same properties, where the values of the properties are equal when compared with a recursive call to `deepEqual`. |
| 29 | +2. **`reverseArrayInPlace` Function** |
| 30 | + Create a function that reverses an array in place, meaning the original array is modified without creating a new array. |
| 31 | + |
| 32 | + **Tips**: |
| 33 | + - For `reverseArrayInPlace`, try using a loop to swap elements from the beginning and end until you reach the middle. |
| 34 | + - Avoid using JavaScript’s built-in `reverse` method to deepen your understanding of array manipulation. |
| 35 | + |
| 36 | +## 📝 Working with Lists |
| 37 | + |
| 38 | +1. **`arrayToList` Function** |
| 39 | + Write a function that converts an array into a nested list structure. A list is an object where each element has a `value` and a `rest` property. |
| 40 | + |
| 41 | +2. **`listToArray` Function** |
| 42 | + Convert a list back into an array format. |
| 43 | + |
| 44 | +3. **`prepend` Helper Function** |
| 45 | + Write a helper function that adds an element to the front of a list. This function should return a new list with the element prepended. |
| 46 | + |
| 47 | +4. **`nth` Function** |
| 48 | + Access a specific element in the list by its position (with zero being the first element). Return `undefined` if the element doesn’t exist. |
| 49 | + |
| 50 | +5. **Recursive `nth` Function** |
| 51 | + Write a recursive version of `nth` to practice recursive thinking! |
| 52 | + |
| 53 | + **Quick Note**: |
| 54 | + - Lists are often implemented as nested objects, which can be a fun departure from typical arrays. Try to think of each element in the list as a unique object in a chain. |
| 55 | + |
| 56 | +## 🔍 Deep Comparison |
| 57 | + |
| 58 | +1. **`deepEqual` Function** |
| 59 | + Write a `deepEqual` function that checks if two values are "deeply equal." In other words, they should have the same values, and if they’re objects, all properties must match as well. |
| 60 | + |
| 61 | + **Tips**: |
| 62 | + - Use recursion to handle nested objects. |
| 63 | + - Remember that `deepEqual` should handle primitive types as well as objects. |
| 64 | + - Consider cases where one value is `null` or `undefined` to make your function more robust. |
| 65 | + |
| 66 | +### 🌟 Happy Coding! 🌟 |
| 67 | + |
| 68 | +These exercises cover a range of essential JavaScript skills, from basic array manipulation to deeper object comparisons. Enjoy exploring these concepts, and remember to write code that’s not only functional but also clear and readable. Good luck! 🚀 |
0 commit comments