Skip to content

Commit b3ec0e1

Browse files
committed
chapter 1: code updated due to review
1 parent 174bed9 commit b3ec0e1

File tree

5 files changed

+78
-39
lines changed

5 files changed

+78
-39
lines changed

src/01-intro/01-hello-variables.js

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,52 @@
44
console.log('Hello, World!');
55

66
// variables
7-
var num = 1; // {1}
8-
num = 3; // {2}
7+
var num = 1;
8+
num = 'one' ;
99

10-
let myVar = 2; // {3}
11-
myVar = 4; // {4}
10+
let myVar = 2;
11+
myVar = 4;
1212

13-
const price = 1.5; // {5} number
14-
const publisher = 'Packt'; // {6} string
15-
const javaScriptBook = true; // {7} boolean
16-
const nullVar = null; // {8} null
17-
let und; // {9} undefined
13+
const price = 1.5; // number
14+
const publisher = 'Packt'; // string
15+
const javaScriptBook = true; // boolean
16+
const nullVar = null; // null
17+
let und; // undefined
1818

19-
console.log('num: ' + num);
20-
console.log('myVar: ' + myVar);
19+
console.log('price: ' + price);
2120
console.log('publisher: ' + publisher);
2221
console.log('javaScriptBook: ' + javaScriptBook);
23-
console.log('price: ' + price);
2422
console.log('nullVar: ' + nullVar);
2523
console.log('und: ' + und);
2624

25+
console.log('**** Data types ****');
26+
27+
console.log('typeof price: ', typeof price); // number
28+
console.log('typeof publisher: ', typeof publisher); // string
29+
console.log('typeof javaScriptBook: ', typeof javaScriptBook); // boolean
30+
console.log('typeof nullVar: ', typeof nullVar); // object
31+
console.log('typeof und: ', typeof und); // undefined
32+
2733
const book = {
28-
title: 'Data Structures and Algorithms', // {10}
34+
title: 'Data Structures and Algorithms',
2935
}
30-
book.title = 'Data Structures and Algorithms in JavaScript'; // {11}
31-
// book = { anotherTitle: 'Data Structures’ } // this will not work {12}
36+
37+
console.log('book title: ', book.title);
38+
39+
book.title = 'Data Structures and Algorithms in JavaScript';
40+
// book = { anotherTitle: 'Data Structures’ } // this will not work
41+
42+
// reassignment of objects:
43+
let book2 = {
44+
title: 'Data Structures and Algorithms',
45+
}
46+
book2 = { title: 'Data Structures' };
47+
48+
// symbol
49+
const title = Symbol('title');
50+
const book3 = {
51+
[title]: 'Data Structures and Algorithms'
52+
};
53+
console.log(book3[title]); // Data Structures and Algorithms
3254

3355
// to see the output of this file use the command: node src/01-intro/01-hello-variables.js

src/01-intro/02-conditionals.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ if (number === 1) {
1818
// is the same as
1919
number === 1 ? number-- : number++;
2020

21+
// is the same as
22+
number = number === 1 ? number - 1 : number + 1;
23+
2124
/* Example 03 - if-else-if-else... */
2225
let month = 5;
2326
if (month === 1) {

src/01-intro/04-functions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ function sum(num1, num2) {
1111
return num1 + num2;
1212
}
1313

14-
var result = sum(1, 2);
14+
const result = sum(1, 2);
1515
console.log(result); // outputs 3
1616

1717
/* function with default parameter */

src/01-intro/05-scope.js

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,39 @@
11
// Path: src/01-intro/05-scope.js
22

3-
let movie = 'Lord of the Rings'; // {1}
3+
let movie = 'Lord of the Rings';
44

55
function starWarsFan() {
6-
const movie = 'Star Wars'; // {2}
6+
const movie = 'Star Wars';
77
return movie;
88
}
99

1010
function marvelFan() {
11-
movie = 'The Avengers'; // {3}
11+
movie = 'The Avengers';
1212
return movie;
1313
}
1414

15+
console.log(movie); // Lord of the Rings
16+
console.log(starWarsFan()); // Star Wars
17+
console.log(marvelFan()); // The Avengers
18+
console.log(movie); // The Avengers
19+
20+
// block scope
1521
function blizzardFan() {
1622
const isFan = true;
17-
let phrase = 'Warcraft'; // {4}
23+
let phrase = 'Warcraft';
1824
console.log('Before if: ' + phrase);
1925
if (isFan) {
20-
let phrase = 'initial text'; // {5}
21-
phrase = 'For the Horde!'; // {6}
26+
let phrase = 'initial text';
27+
phrase = 'For the Horde!';
2228
console.log('Inside if: ' + phrase);
2329
}
24-
phrase = 'For the Alliance!'; // {7}
30+
phrase = 'For the Alliance!';
2531
console.log('After if: ' + phrase);
2632
}
2733

28-
console.log(movie); // Lord of the Rings
29-
console.log(starWarsFan()); // Star Wars
30-
console.log(marvelFan()); // The Avengers
31-
console.log(movie); // The Avengers
32-
blizzardFan(); // Before if: Warcraft, Inside if: For the Horde!, After if: For the Alliance!
34+
blizzardFan();
35+
// Before if: Warcraft
36+
// Inside if: For the Horde!
37+
// After if: For the Alliance!
3338

3439
// to see the output of this file use the command: node src/01-intro/05-scope.js

src/01-intro/06-objects.js

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,24 @@ obj = {
1515
/* Class example */
1616
class Book {
1717

18-
#percentagePerSale = 0.12; // {1}
18+
#percentagePerSale = 0.12;
1919

2020
constructor(title, pages, isbn) {
21-
this.title = title; // {2}
21+
this.title = title;
2222
this.pages = pages;
2323
this.isbn = isbn;
2424
}
2525

26-
get price() { // {3}
26+
get price() {
2727
return this.pages * this.#percentagePerSale;
2828
}
2929

30-
static copiesSold = 0; // {4}
31-
static sellCopy() { // {5}
30+
static copiesSold = 0;
31+
static sellCopy() {
3232
this.copiesSold++;
3333
}
3434

35-
printIsbn() { // {6}
35+
printIsbn() {
3636
console.log(this.isbn);
3737
}
3838
}
@@ -52,13 +52,22 @@ console.log(Book.copiesSold); // 1
5252
Book.sellCopy();
5353
console.log(Book.copiesSold); // 2
5454

55-
class Ebook extends Book { // {7}
55+
class Ebook extends Book {
5656
constructor(title, pages, isbn, format) {
57-
super(title, pages, isbn); // {8}
58-
this.format = format; // {9}
57+
super(title, pages, isbn);
58+
this.format = format;
59+
}
60+
printIsbn() {
61+
console.log('Ebook ISBN:',this.isbn);
5962
}
6063
}
61-
Ebook.sellCopy(); // {10}
62-
console.log(Ebook.copiesSold); // {11} 3
64+
Ebook.sellCopy();
65+
console.log(Ebook.copiesSold); // 3
66+
67+
const myBook = new Book('title', 400, 'isbn');
68+
myBook.printIsbn(); // isbn
69+
const myEbook = new Ebook('Data Structures Ebook', 400, 'isbn 123', 'pdf');
70+
myEbook.printIsbn(); // Ebook ISBN: isbn 123
71+
6372

6473
// to see the output of this file use the command: node src/01-intro/06-objects.js

0 commit comments

Comments
 (0)