Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 58b5197

Browse files
committed
additions to fundamentals
1 parent 72babba commit 58b5197

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

โ€Žfundamentals/DOM_manipulation.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,17 @@ We can also create elements
2626
const a = document.createElement('li');
2727
x.appendChild(a);
2828
```
29+
30+
We can set attributes on elements
31+
```js
32+
a.setAttribute('id', 'hackyourfuture');
33+
```
34+
35+
We can add event listeners to elements:
36+
37+
```js
38+
turnLeftButton.addEventListener('click', function () {
39+
turn('left');
40+
});
41+
```
42+

โ€Žfundamentals/conditional_execution.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,19 @@ const conditionOfCar = age < 1 ? 'new' : 'used';
101101

102102
The `conditionOfCar` variable will be assigned the string `'new'` if the `age < 1` condition holds true, otherwise it is assigned the string `'used'`.
103103

104+
It is always possible to rewrite a ternary operator as an `if-then-else` statement, for example:
105+
106+
```js
107+
let conditionOfCar;
108+
if (age < 1) {
109+
conditionOfCar = 'new';
110+
} else {
111+
conditionOfCar = 'used';
112+
}
113+
```
114+
115+
Note that you can't use `const` here for `conditionOfCar` because we can't combine declaration and initialization in a single statement. Therefore we must now use `let`.
116+
104117
It is **not** recommended to use the conditional operator if you do not intend to use its value:
105118

106119
```js

โ€Žfundamentals/functions.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,30 @@
11
# Functions
22

3-
A function is a reusable piece of code (see _Why Use Functions_ below). Functions are *very* important in JavaScript, to the extent that some people call JavaScript a "function-oriented" language. As mentioned above, variables can be of type function. In fact, *every function is a variable*.
3+
Consider this **function** from [high-school math](https://www.mathplanet.com/education/algebra-2/how-to-graph-functions-and-linear-equations/functions-and-linear-equations):
4+
5+
> ๐‘“(x) = x + 7
6+
>
7+
> _if x = 2 then_
8+
>
9+
> ๐‘“(2) = 2 + 7 = 9
10+
11+
The value of the function ๐‘“(x) is dependent on the value you supply for its argument x. (Instead of the term 'argument', sometimes the word 'parameter' is used). This function adds 7 to the value of its argument. Whenever we need to add 7 to some number we can reuse this same function over and over again.
12+
13+
Here is the equivalent JavaScript function:
14+
15+
```js
16+
// function definition
17+
function f(x) {
18+
return x + 7;
19+
}
20+
21+
// call the function and log its value for x = 2
22+
console.log(f(2)); // -> 9
23+
```
24+
25+
During execution, the value of x in the function body (the part between the curly braces) is substituted with the value 'passed' during the function call.
26+
27+
A function thus is a reusable piece of code (see _Why Use Functions_ below). Functions are *very* important in JavaScript, to the extent that some people call JavaScript a "function-oriented" language. As mentioned above, variables can be of type function. In fact, *every function is a variable*.
428

529
The following two pieces of code have the exact same result:
630

0 commit comments

Comments
ย (0)