You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
3. Explain how to manipulate arrays in a non-destructive way
6
+
- Practice using the `const` and `let` variables in JavaScript
9
7
10
-
## Introduction
8
+
## Instructions
11
9
12
-
Previously, we've learned about how arrays work. We know that `push()` pushes elements onto the ends of arrays, and `pop()` pops them off; similarly, `unshift()` adds elements to the beginnings of arrays, and `shift()` pulls them off.
10
+
In this lab, you'll need to declare some variables using `const`and `let`.
13
11
14
-
Now it's time to put what we've learned to the test.
15
-
16
-
## What's with all this destruction?
17
-
18
-
You might have noticed that our tests are looking for functions like `destructivelyAppendDriver()` — what's up with that?
19
-
20
-
We want to distinguish between actions that _mutate_ ("change") their underlying structures (like `pop()`, `push()`, `shift()`, and `unshift()`) and those functions that leave those structures untouched.
21
-
22
-
In general, it's good practice to avoid mutating a program's state whenever possible. So we want to call out these methods as destructive, since mutating state means we don't always know what we're dealing with. Indeed, these mutations mean that we need to refresh the test environment after every test to make sure that we're not working with mutated data!
23
-
24
-
By contrast, we also have methods like `appendDriver()`, which simply adds a driver to the end of the `drivers` array and returns the _new_ array, leaving the existing array untouched. This flow is preferable to mutating state because we have complete control over what's going into and coming out of the function.
25
-
26
-
## Run those tests!
27
-
28
-
You'll notice that the first test asks for an array called `drivers`, set to an initial value of `["Milo", "Otis", "Garfield"]`.
29
-
30
-
In our test file, we're going to reset this array to your initial value after every test. Some of our tests manipulate arrays in place, and we want to be sure that we can get back to a blank slate between tests.
31
-
32
-
Why is a blank slate important? We want our programs to be predictable: this makes them more robust, easier to maintain, and less prone to bugs. One way to achieve predictability is by isolating our tests from one another, meaning that no test should depend on the outcome or process of any other test. That way, tests can run in any order and test _known_ inputs and environments, rather than depending on other tests running first and modifying the entire environment.
12
+
As usual, start by running the specs and reading the results.
33
13
34
14
Remember the workflow:
35
15
@@ -43,8 +23,22 @@ Remember the workflow:
43
23
44
24
5. Run `learnsubmit` when finished!
45
25
46
-
If you open up `test/indexTest.js`, you will see that in the `beforeEach` block we initialize our `driver` array values so that you can focus on what really matters — understanding how to manipulate arrays in JavaScript.
26
+
27
+
## Working Through The Problems
28
+
29
+
If you open up `test/indexTest.js`, you will see the tasks in front of you.
30
+
31
+
+`companyName` - Inside the `test/indexTest`, look inside of the word `describe` where the tests are trying to indicate that this test is describing the `companyName` variable. The `it` word that comes afterwords, tells us the features of `companyName`. In the first `it` function call, it says that `it` (companyName) `is set as Scuber`. In the next line, you can see that the test checks to make sure this occurs by seeing if `companyName` equals `Scuber`. So this means that you need to go to your `index.js` file and declare a variable named `companyName` and set it equal to `Scuber`. Once you do that, if `learn-test` is running, you will see the first test in this lab as passing.
32
+
33
+
In the next `it` function call, we are still describing `companyName`. This time, it says it `raises error if the companyName is changed`. The next line of code tests this. It's ok if some of the code in that line are confusing. Just know that the code attempts to change `companyName` to a different value, and that this reassignment to throw an error. So you need to make sure that you are using the correct variable type such that reassigning the variable would throw an error.
34
+
35
+
+`mostProfitableNeighborhood` - Here we need to declare another variable, `mostProfitableNeighborhood` and assign it to `Chelsea`. In the next `it` function call, you can see that our tests ensure that `mostProfitableNeighborhood` does not throw an error when reassigned. So you need to make sure that you are using the correct variable type such that reassigning the `mostProfitableNeighborhood` would not throw an error.
36
+
37
+
+`companyCeo` - Here, we are getting more practice with declaring variables. Once again, a reassignment should not throw an error.
<pclass='util--hide'>View <ahref='https://learn.co/lessons/javascript-array-lab'>Javascript Array Lab</a> on Learn.co and start learning to code for free.</p>
0 commit comments