Skip to content

Commit 1743d9c

Browse files
Dario-DCmoT01
andauthored
chore(curriculum): add JS array transcripts (freeCodeCamp#58461)
Co-authored-by: Tom <20648924+moT01@users.noreply.github.com>
1 parent 672ab0a commit 1743d9c

File tree

10 files changed

+600
-61
lines changed

10 files changed

+600
-61
lines changed

curriculum/challenges/english/25-front-end-development/lecture-working-with-arrays/67329f508a6ec45b046700b3.md

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,41 @@ dashedName: what-are-the-key-characteristics-of-javascript-arrays
1010

1111
Watch the video lecture and answer the questions below.
1212

13+
# --transcript--
14+
15+
What are key characteristics of JavaScript arrays?
16+
17+
An array in JavaScript is an ordered collection of values, each identified by a numeric index. The values in a JavaScript array can be of different data types, including numbers, strings, booleans, objects, and even other arrays.
18+
19+
To create an array in JavaScript, you can use square brackets, `[]`, and separate the values with commas. Here's an example:
20+
21+
```js
22+
let fruits = ["apple", "banana", "orange"];
23+
```
24+
25+
In this example, we declare a variable `fruits` and assign it an array containing three string values: `apple`, `banana`, and `orange`.
26+
27+
One of the key characteristics of arrays is that they are zero-indexed, meaning that the first element in an array has an index of `0`, the second element has an index of `1`, and so on. You can access individual elements in an array using their index. For example:
28+
29+
```js
30+
let fruits = ["apple", "banana", "orange"];
31+
console.log(fruits[0]); // "apple"
32+
console.log(fruits[2]); // "orange"
33+
```
34+
35+
In this example, we use the index `0` to access the first element (`apple`) and the index `2` to access the third element (`orange`).
36+
37+
Arrays in JavaScript have a special `length` property that returns the number of elements in the array. You can access this property using the `length` keyword. For example:
38+
39+
```js
40+
let fruits = ["apple", "banana", "orange"];
41+
console.log(fruits.length); // 3
42+
```
43+
44+
Another key characteristic of arrays in JavaScript is that they are dynamic, meaning that their size can change after they are created. You can add or remove elements from an array using various array methods, such as `push()`, `pop()`, `shift()`, `unshift()`, `splice()`, and more. These methods will be taught in upcoming lectures videos.
45+
46+
JavaScript arrays are versatile and useful when it comes to data storage inside your programs. Throughout this module, you'll get to see firsthand how working with arrays will help you efficiently manage and manipulate collections of data.
47+
1348
# --questions--
1449

1550
## --text--
@@ -94,7 +129,7 @@ Remember that arrays in JavaScript are zero-indexed.
94129
What will be the output of the following code?
95130

96131
```js
97-
let colors = ['red', 'green', 'blue'];
132+
let colors = ["red", "green", "blue"];
98133
console.log(colors.length);
99134
```
100135

curriculum/challenges/english/25-front-end-development/lecture-working-with-arrays/6732aebaa8abb9086a9bb17a.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,43 @@ dashedName: how-do-you-access-and-update-elements-in-an-array
1010

1111
Watch the lecture video and answer the questions below.
1212

13+
# --transcript--
14+
15+
How do you access and update elements in an array?
16+
17+
In the previous lecture video, you were first introduced to working with arrays and accessing different elements from arrays. Here is a reminder on how to access the second element from an array:
18+
19+
```js
20+
const fruits = ["apple", "banana", "cherry"];
21+
22+
console.log(fruits[1]); // "banana"
23+
```
24+
25+
Since arrays are zero based indexed, the first element will be at index `0`, the second index will be at index `1`, etc. It's important to note that if you try to access an index that doesn't exist in the array, JavaScript will return `undefined`.
26+
27+
```js
28+
let fruits = ["apple", "banana", "cherry"];
29+
console.log(fruits[3]); // undefined
30+
```
31+
32+
In this example, there is no index `3` for the `fruits` array. So the log will show `undefined`. Now, let's look at how to update elements in an array. You can update an element by assigning a new value to a specific index.
33+
34+
```js
35+
let fruits = ["apple", "banana", "cherry"];
36+
fruits[1] = "blueberry";
37+
console.log(fruits); // ["apple", "blueberry", "cherry"]
38+
```
39+
40+
In this example, we've replaced `banana` with `blueberry` at index `1`. This method allows you to change any element in the array, as long as you know its index. You can also add new elements to an array by assigning a value to an index that doesn't yet exist:
41+
42+
```js
43+
let fruits = ["apple", "banana", "cherry"];
44+
fruits[3] = "date";
45+
console.log(fruits); // Outputs: ["apple", "blueberry", "cherry", "date"]
46+
```
47+
48+
However, exercise caution when doing this. If you assign a value to an index that is much larger than the current length of the array, you will create undefined elements for the indices in between, which can lead to unexpected behavior. As you continue to work with JavaScript, you'll find that these ways of accessing and updating array elements are fundamental to many programming tasks. Whether you're building a simple todo list or processing complex data structures, these skills will be invaluable.
49+
1350
# --questions--
1451

1552
## --text--

curriculum/challenges/english/25-front-end-development/lecture-working-with-arrays/6732aec982904608b637716b.md

Lines changed: 71 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,66 @@ dashedName: how-do-you-add-and-remove-elements-from-the-beginning-and-end-of-an-
1010

1111
Watch the lecture video and answer the questions below.
1212

13+
# --transcript--
14+
15+
How do you add/remove elements from the beginning and end of an array?
16+
17+
Arrays in JavaScript are dynamic, which means you can easily add or remove elements from them. There are four main methods for adding and removing elements from the beginning and end of an array: `push()`, `pop()`, `shift()`, and `unshift()`. Let's explore each of these methods in detail.
18+
19+
The `push()` method is used to add one or more elements to the end of an array. The return value for the `push()` method is the new length of the array. Here's an example of adding a new fruit to the existing `fruits` array:
20+
21+
```js
22+
const fruits = ["apple", "banana"];
23+
const newLength = fruits.push("orange");
24+
console.log(newLength); // 3
25+
console.log(fruits); // ["apple", "banana", "orange"]
26+
```
27+
28+
In this example, we start with an array called `fruits` which contains two elements. We then use the `push()` method to add the string `orange` to the end of the array.
29+
30+
You might have noticed that we are using `const` when declaring the `fruits` array. But why is it possible to add more elements to this `fruits` array when `fruits` is a constant? This is possible because declaring an array with the `const` keyword creates a reference to the array. While the array itself is mutable and can be modified, you cannot reassign a new value to the `fruits` constant, like this:
31+
32+
```js
33+
const fruits = ["apple", "banana"];
34+
fruits = ["This", "will", "not", "work"];
35+
console.log(fruits); // Uncaught TypeError: Assignment to constant variable.
36+
```
37+
38+
The next method we will look at is the `pop()` method. The `pop()` method removes the last element from an array and returns that element. It also modifies the original array. Here's how it works:
39+
40+
```js
41+
let fruits = ["apple", "banana", "orange"];
42+
let lastFruit = fruits.pop();
43+
console.log(fruits); // ["apple", "banana"]
44+
console.log(lastFruit); // "orange"
45+
```
46+
47+
In this example, we start with an array of three fruits. The `pop()` method removes the last element (`orange`) from the array and returns it. The original `fruits` array is modified and contains only two elements.
48+
49+
The `unshift()` method adds one or more elements to the beginning of an array and returns its new length. It works similarly to `push()`, but modifies the start of the array instead of the end. Here's an example:
50+
51+
```js
52+
let numbers = [2, 3];
53+
let newLength = numbers.unshift(1);
54+
console.log(numbers); // [1, 2, 3]
55+
console.log(newLength); // 3
56+
```
57+
58+
In this example, we use `unshift()` to add the number `1` to the beginning of the `numbers` array. The method returns the new length of the array, which is `3`.
59+
60+
Finally, the `shift()` method removes the first element from an array and returns that element. It's similar to `pop()`, but it works at the beginning of the array instead of the end. Here's how it works:
61+
62+
```js
63+
let colors = ["red", "green", "blue"];
64+
let firstColor = colors.shift();
65+
console.log(colors); // ["green", "blue"]
66+
console.log(firstColor); // "red"
67+
```
68+
69+
In this example, we start with an array of three colors. The `shift()` method removes the first element (`red`) from the array and returns it. The original `colors` array is modified to contain only two elements.
70+
71+
Note that while `push()` and `unshift()` can add multiple elements at once, `pop()` and `shift()` remove only one element at a time.
72+
1373
# --questions--
1474

1575
## --text--
@@ -60,35 +120,35 @@ Consider the order of operations and what each method does to the array.
60120
What will be the output of the following code?
61121

62122
```js
63-
let arr = ['a', 'b', 'c', 'd'];
123+
let arr = ["a", "b", "c", "d"];
64124
let first = arr.shift();
65125
let last = arr.pop();
66126
console.log(first, last, arr);
67127
```
68128

69129
## --answers--
70130

71-
`'a' 'd' ['b', 'c']`
131+
`"a" "d" ["b", "c"]`
72132

73133
---
74134

75-
`'d' 'a' ['b', 'c']`
135+
`"d" "a" ["b", "c"]`
76136

77137
### --feedback--
78138

79139
Remember that `shift()` removes from the beginning and `pop()` removes from the end.
80140

81141
---
82142

83-
`'a' 'b' ['c', 'd']`
143+
`"a" "b" ["c", "d"]`
84144

85145
### --feedback--
86146

87147
Remember that `shift()` removes from the beginning and `pop()` removes from the end.
88148

89149
---
90150

91-
`'b' 'c' ['a', 'd']`
151+
`"b" "c" ["a", "d"]`
92152

93153
### --feedback--
94154

@@ -103,35 +163,35 @@ Remember that `shift()` removes from the beginning and `pop()` removes from the
103163
What will be the result of the following code?
104164

105165
```js
106-
let arr = ['a', 'b', 'c'];
107-
arr.unshift('x', 'y');
166+
let arr = ["a", "b", "c"];
167+
arr.unshift("x", "y");
108168
arr.pop();
109169
console.log(arr);
110170
```
111171

112172
## --answers--
113173

114-
`['x', 'y', 'a', 'b']`
174+
`["x", "y", "a", "b"]`
115175

116176
---
117177

118-
`['x', 'y', 'a', 'b', 'c']`
178+
`["x", "y", "a", "b", "c"]`
119179

120180
### --feedback--
121181

122182
Pay attention to the order of operations and what each method does to the array.
123183

124184
---
125185

126-
`['a', 'b', 'x', 'y']`
186+
`["a", "b", "x", "y"]`
127187

128188
### --feedback--
129189

130190
Pay attention to the order of operations and what each method does to the array.
131191

132192
---
133193

134-
`['y', 'x', 'a', 'b']`
194+
`["y", "x", "a", "b"]`
135195

136196
### --feedback--
137197

curriculum/challenges/english/25-front-end-development/lecture-working-with-arrays/6732aed2ac3d3c08f6149dd6.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,52 @@ dashedName: what-is-the-difference-between-one-dimensional-and-two-dimensional-a
1010

1111
Watch the lecture video and answer the questions below.
1212

13+
# --transcript--
14+
15+
What is the difference between one-dimensional and two-dimensional arrays?
16+
17+
In programming, arrays are fundamental data structures used to store collections of elements. Understanding the difference between one-dimensional and two-dimensional arrays is crucial for organizing and manipulating data effectively. Let's explore these concepts in a way that's easy for beginners to grasp.
18+
19+
A one-dimensional array, often called an array, is like a single row of boxes. Imagine you have a line of storage lockers at a train station. Each locker can hold one item, and you can access any locker directly if you know its number.
20+
21+
In programming terms, each item in a one-dimensional array is accessed using a single index. For example, in JavaScript, you might create and use a one-dimensional array like this:
22+
23+
```js
24+
let fruits = ["apple", "banana", "cherry", "date"];
25+
console.log(fruits[2]); // Outputs: "cherry"
26+
```
27+
28+
Here, `fruits` is a one-dimensional array. You can think of it as a single row of fruit names. To access any fruit, you use one number (the index) inside square brackets.
29+
30+
Now, let's move on to two-dimensional arrays. If a one-dimensional array is like a single row of lockers, a two-dimensional array is like a grid of lockers – multiple rows and columns. In programming, a two-dimensional array is essentially an array of arrays. It's used to represent data that has a natural grid-like structure, such as a chessboard, a spreadsheet, or pixels in an image.
31+
32+
To access an element in a two-dimensional array, you need two indices: one for the row and one for the column. Here's an example of how you might create and use a two-dimensional array in JavaScript:
33+
34+
```js
35+
let chessboard = [
36+
["R", "N", "B", "Q", "K", "B", "N", "R"],
37+
["P", "P", "P", "P", "P", "P", "P", "P"],
38+
[" ", " ", " ", " ", " ", " ", " ", " "],
39+
[" ", " ", " ", " ", " ", " ", " ", " "],
40+
[" ", " ", " ", " ", " ", " ", " ", " "],
41+
[" ", " ", " ", " ", " ", " ", " ", " "],
42+
["p", "p", "p", "p", "p", "p", "p", "p"],
43+
["r", "n", "b", "q", "k", "b", "n", "r"]
44+
];
45+
46+
console.log(chessboard[0][3]); // Outputs: "Q"
47+
```
48+
49+
In this example, `chessboard` is a two-dimensional array representing a chess game's initial setup. To access the queen (`Q`) in the top row, we use two indices: `[0][3]`. The first index, `0`, selects the first row, and the second index, `3`, selects the fourth column in that row.
50+
51+
The key difference between one-dimensional and two-dimensional arrays lies in how you access and organize the data. One-dimensional arrays use a single index and are suitable for linear data like lists or sequences. Two-dimensional arrays use two indices and are ideal for grid-like data structures.
52+
53+
It's worth noting that in JavaScript, two-dimensional arrays are actually arrays of arrays. This means each element of the outer array is itself an array. This nested structure allows for great flexibility but also requires careful handling to avoid errors.
54+
55+
As you progress in your programming journey, you'll find that choosing between one-dimensional and two-dimensional arrays depends on the nature of your data and how you need to manipulate it.
56+
57+
One-dimensional arrays are simpler and sufficient for many tasks, while two-dimensional arrays become invaluable when dealing with more complex, structured data.
58+
1359
# --questions--
1460

1561
## --text--

0 commit comments

Comments
 (0)