Skip to content

Commit 8a20614

Browse files
committed
adding array chapter readme
1 parent 3fd10a1 commit 8a20614

File tree

3 files changed

+80
-25
lines changed

3 files changed

+80
-25
lines changed

02-chapter-Arrays.js renamed to 02-chapter-Array/02-chapter-Arrays.js

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,22 @@ class grades {
1515
}
1616
}
1717

18-
// ##############################################
18+
// Implementation of the grade class
19+
console.log('### Excercise 1');
20+
const gradeObj = [90,89,75];
21+
const grade = new grades(gradeObj);
22+
grade.addGrade(90);
23+
grade.addGrade(25);
24+
console.log(`Avg grade: ${grade.displayAvg()}`);
25+
26+
// #############################################
27+
console.log('\n### Excercise 2');
28+
const arrayWords = ["hello ","my ","friend "];
29+
console.log(`orginal array: ${arrayWords} `);
30+
console.log(`displaying forward: ${arrayWords.reduce((total, word) => total + word)}`);
31+
console.log(`displaying backward: ${arrayWords.reduceRight((total, word) => total + word)}`);
32+
33+
// #############################################
1934
class weekTemps {
2035

2136
constructor(dataStore = []) {
@@ -57,26 +72,7 @@ class weekTemps {
5772
}
5873
}
5974

60-
// Excercises
61-
// #############################################
62-
console.log('CHAPTER 2');
63-
console.log('### Excercise 1');
64-
65-
const gradeObj = [90,89,75];
66-
const grade = new grades(gradeObj);
67-
grade.addGrade(90);
68-
grade.addGrade(25);
69-
console.log(`Avg grade: ${grade.displayAvg()}`);
70-
71-
// #############################################
72-
console.log('\n### Excercise 2');
73-
74-
const arrayWords = ["hello ","my ","friend "];
75-
console.log(`orginal array: ${arrayWords} `);
76-
console.log(`displaying forward: ${arrayWords.reduce((total, word) => total + word)}`);
77-
console.log(`displaying backward: ${arrayWords.reduceRight((total, word) => total + word)}`);
78-
79-
// #############################################
75+
// Implementation of weekTemps class
8076
console.log('\n### Excercise 3');
8177

8278
const randomMonth = [

02-chapter-Array/readme.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Array data structure
2+
3+
### Examples
4+
- [Array exercises and examples codes in ES6](./02-chapter-Arrays.js)
5+
6+
### Definition
7+
8+
The array is the most common data structure in computer programming. Arrays are usually very efficient and are considered good choices for many data storage purposes.
9+
10+
Arrays in JavaScript are very flexible. There are several different ways to create arrays, access array elements, and perform tasks such as searching and sorting the elements stored in an array.
11+
12+
```
13+
const myArray = [1, 'one', {some: 'object'}, new Date()]
14+
```
15+
![](https://docs.oracle.com/javase/tutorial/figures/java/objects-tenElementArray.gif)
16+
17+
### Usage of arrays
18+
19+
**Creating an array**
20+
21+
```
22+
const grades = [1,2,3]; // this is the best way to create an array.
23+
const grades = new Array(1,2,3); // use of the constructor.
24+
const grades = new Array(3); // will initialize the array with the length of 3.
25+
grades = [...grades, grade]; // will reinstantiate the grades variable.
26+
27+
// use of the concat() function.
28+
const a = [1,2,3];
29+
const b = [4,5,6];
30+
const c = a.concat(b); // this creates a new array by concatenating the 2 arrays.
31+
32+
```
33+
or using strings to create an array, by using the ***split*** method and space ' ' delimiter will create a new array containg the words of the text.
34+
```
35+
const words = someStringText.split(' ');
36+
```
37+
38+
**Inserting elements to an array**
39+
40+
There are several ways to add a new element to the array for example:
41+
42+
```
43+
// This two operations will add a new element at the end of the array.
44+
grades.push(element);
45+
grades[grades.length + 1] = grade;
46+
47+
// use of shift() method will insert a new element at the front of the array.
48+
grades.shift(grade);
49+
50+
//use of the splice() method will insert a new element at the position indicated.
51+
grades.splice(position, 0, grade);
52+
```
53+
The splice() method, has the ability to insert or to remove an element, the first parameter of the method indicates the position, the second parameter indicates the type of operation 0 = insert, 1 = delete, and the third parameter is the new value to insert.
54+
55+
**Deleting elements to an array**
56+
57+
```
58+
// use of the splice() method.
59+
grades.splice(position, numElementsToDelete);
60+
```

README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
# Data Structures and Algorithms with ES6
22

3-
43
![](https://i.blogs.es/545cf8/es6-logo/original.png) ![](https://lh3.googleusercontent.com/a4Xrc-8oQLu05mOrNPuvA_o2nZEIEnOoTH4wB91Slw_hCvuIu_Qgi440bK9mC8ml-KA=w300)
54

6-
This exercises are based from the book [Data Structures and Algorithms with JavaScript](http://shop.oreilly.com/product/0636920029557.do) - [by Michael McMillian (O’Reilly)](http://www.oreilly.com/pub/au/518) ISBN - 978-1-449-36493-9.
5+
The exercises are based from the book [Data Structures and Algorithms with JavaScript](http://shop.oreilly.com/product/0636920029557.do) - [by Michael McMillian (O’Reilly)](http://www.oreilly.com/pub/au/518) ISBN - 978-1-449-36493-9.
76

8-
The purpose of this repo is to update the exercises to ES6 standards and to correct the errors from the examples that this book has.
7+
The purpose of this repo is to update the exercises to ES6 standards and to correct the many errors that the book has.
98

109
## Examples in this repo
1110

1211
| **Num** | **Type** | **Exercises** | **Description** |
1312
--- | --- | --- | ---
14-
| 1.- | [Array](./02-chapter-Arrays.js) | 4 | The `Array` is the most common data structure in computer programming
13+
| 1.- | [Array](./02-chapter-Array) | 4 | The `Array` is the most common data structure in computer programming
1514
| 2.- | [Lists](./03-chapter-List.js) | 5 | A `List` is an ordered sequence of data, where elements are not meant to be ordered.
1615
| 3.- | [Stacks](./04-chapter-Stacks.js) | 3 | A `Stack` is an example of Last-in, First-Out (LIFO)
1716
| 4.- | [Queues](./05-chapter-Queues.js) | 3 | A `Queue` is an example of First-in, First-Out (FIFO)

0 commit comments

Comments
 (0)