Skip to content

Commit a966ddd

Browse files
committed
Added page for JS functions
1 parent cc37882 commit a966ddd

File tree

6 files changed

+120
-63
lines changed

6 files changed

+120
-63
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# comp0034_js
2-
COMP0034 Code for the Javascript lecture
2+
COMP0034 Code for the using Javascript in a Flask app lecture
33

44
1. Clone the repository
55
2. Add a venv
66
3. Install the packages from requirements.txt
77
4. Run the Flask app using `run.py`
88
5. The JavaScript code from the lecture slides can be found using the JavaScript examples menu link.
99
6. The final code for the Flask examples is in `static/js/hn.js`. If you want to see the code at the start of each example then use the earlier versions, e.g. `hn_1.js`, `hn_2.js` etc.
10-
7. To view the final code for the news page select the news link from the menu.
10+
7. To view the final code for the news page select the `news` link from the menu.
1111

12-
There are no exercises with this repository.
12+
Apologies, there are no exercises with this repository.

cscourses/main/routes.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ def news_v1():
6363
def news():
6464
return render_template('news.html')
6565

66+
6667
@bp_main.route('/js')
6768
def js():
6869
return render_template('js_examples.html')
@@ -81,3 +82,8 @@ def events():
8182
@bp_main.route('/js/basics')
8283
def basics():
8384
return render_template('basics.html')
85+
86+
87+
@bp_main.route('/js/functions')
88+
def functions():
89+
return render_template('functions.html')

cscourses/static/js/basics.js

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,4 +195,54 @@ function Person(firstname, lastname, height, dateOfBirth) {
195195
var person1 = new Person("Jo", "Bloggs", 2.1, "12/30/1969");
196196
console.log(person1.firstname); //dot notation
197197
console.log(person1["height"]); //bracket notation
198-
console.log(person1.calculateAge());
198+
console.log(person1.calculateAge());
199+
200+
201+
/* Variable scope */
202+
//Global variable
203+
var count = 0; //Global
204+
function incr(n) {
205+
count += n;
206+
}
207+
208+
function reset() {
209+
count = 0;
210+
}
211+
212+
incr(4);
213+
reset();
214+
incr(2);
215+
console.log(count);
216+
217+
//Function scope
218+
function everything() {
219+
var count = 0;
220+
221+
function incr(n) {
222+
count += n;
223+
}
224+
225+
function reset() {
226+
count = 0;
227+
}
228+
229+
incr(4);
230+
reset();
231+
incr(2);
232+
console.log(count);
233+
}
234+
235+
everything();
236+
237+
//Block scope
238+
/*
239+
Variables declared with the let keyword can have Block Scope, variables declared inside a block {} using let cannot be
240+
accessed from outside the block, variables inside a block {} using var can be accessed from outside the block.
241+
*/
242+
if (true) {
243+
var x = 2;
244+
let y = 2;
245+
}
246+
console.log(x);
247+
console.log(y); //should give an error that y is undefined
248+

cscourses/static/js/functions.js

Lines changed: 8 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
//Named function
44
function findBiggest(firstFraction, secondFraction) {
5-
var result;
5+
let result;
66
if (firstFraction > secondFraction) {
77
result = "The first fraction is bigger: " + firstFraction + " than the second fraction: " + secondFraction;
88
} else {
@@ -11,11 +11,11 @@ function findBiggest(firstFraction, secondFraction) {
1111
return result;
1212
}
1313

14-
console.log(findBiggest(2 / 3, 7 / 12));
14+
//console.log(findBiggest(2 / 3, 7 / 12));
1515

1616
//Anonymous function
17-
var biggest = function (firstFraction, secondFraction) {
18-
var result;
17+
let biggest = function (firstFraction, secondFraction) {
18+
let result;
1919
if (firstFraction > secondFraction) {
2020
result = "The first fraction is bigger: " + firstFraction + " than the second fraction: " + secondFraction;
2121
} else {
@@ -24,12 +24,11 @@ var biggest = function (firstFraction, secondFraction) {
2424
return result;
2525
};
2626

27-
console.log(biggest(2 / 3, 7 / 12));
27+
//console.log(biggest(2 / 3, 7 / 12));
2828

2929
//Self-invoking function
30-
31-
var biggest = (function (firstFraction, secondFraction) {
32-
var result;
30+
let selfInvokingFunction = (function (firstFraction, secondFraction) {
31+
let result;
3332
if (firstFraction > secondFraction) {
3433
result = "The first fraction is bigger: " + firstFraction + " than the second fraction: " + secondFraction;
3534
} else {
@@ -38,54 +37,4 @@ var biggest = (function (firstFraction, secondFraction) {
3837
return result;
3938
})(2 / 3, 7 / 12);
4039

41-
console.log(biggest);
42-
43-
44-
/* Variable scope */
45-
//Global variable
46-
var count = 0; //Global
47-
function incr(n) {
48-
count += n;
49-
}
50-
51-
function reset() {
52-
count = 0;
53-
}
54-
55-
incr(4);
56-
reset();
57-
incr(2);
58-
console.log(count);
59-
60-
//Function scope
61-
function everything() {
62-
var count = 0;
63-
64-
function incr(n) {
65-
count += n;
66-
}
67-
68-
function reset() {
69-
count = 0;
70-
}
71-
72-
incr(4);
73-
reset();
74-
incr(2);
75-
console.log(count);
76-
}
77-
78-
everything();
79-
80-
//Block scope
81-
/*
82-
Variables declared with the let keyword can have Block Scope, variables declared inside a block {} using let cannot be
83-
accessed from outside the block, variables inside a block {} using var can be accessed from outside the block.
84-
*/
85-
if (true) {
86-
var x = 2;
87-
let y = 2;
88-
}
89-
console.log(x);
90-
console.log(y); //should give an error that y is undefined
91-
40+
//console.log(selfInvokingFunction);

cscourses/templates/functions.html

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
{% extends 'base.html' %}
2+
{% block title %}Functions examples{% endblock %}
3+
{% block javascript %}
4+
<script src="{{ url_for('static', filename='js/functions.js') }}" defer></script>
5+
{% endblock %}
6+
{% block content %}
7+
<br>
8+
<h2>Instructions</h2>
9+
<p>The JavaScript functions can be viewed in <a href="{{ url_for('static', filename='js/functions.js') }}">functions.js</a>
10+
</p>
11+
<h4>Named function</h4>
12+
<pre><code>
13+
function findBiggest(firstFraction, secondFraction) {
14+
let result;
15+
if (firstFraction > secondFraction) {
16+
result = "The first fraction is bigger: " + firstFraction + " than the second fraction: " + secondFraction;
17+
} else {
18+
result = "The second fraction is bigger: " + secondFraction + " than the first fraction: " + firstFraction;
19+
}
20+
return result;
21+
}</code></pre>
22+
<h4>Anonymous function</h4>
23+
<pre><code>
24+
let biggest = function (firstFraction, secondFraction) {
25+
let result;
26+
if (firstFraction > secondFraction) {
27+
result = "The first fraction is bigger: " + firstFraction + " than the second fraction: " + secondFraction;
28+
} else {
29+
result = "The second fraction is bigger: " + secondFraction + " than the first fraction: " + firstFraction;
30+
}
31+
return result;
32+
};</code></pre>
33+
<h4>Self-invoking function</h4>
34+
<pre><code>
35+
let selfInvokingFunction = (function (firstFraction, secondFraction) {
36+
let result;
37+
if (firstFraction > secondFraction) {
38+
result = "The first fraction is bigger: " + firstFraction + " than the second fraction: " + secondFraction;
39+
} else {
40+
result = "The second fraction is bigger: " + secondFraction + " than the first fraction: " + firstFraction;
41+
}
42+
return result;
43+
})(2 / 3, 7 / 12);</code></pre>
44+
<br>
45+
<h3>Execute the types of function</h3>
46+
<p>1. Named function. Enter in the JavaScript console: <code>console.log(findBiggest(2 / 3, 7 / 12));</code></p>
47+
<p>2. Anonymous function. Enter in the JavaScript console: <code>console.log(biggest(2 / 3, 7 / 12));</code></p>
48+
<p>3. An immediately executed function is executed as soon as the JavaScript is seen, so this function has already
49+
been executed. You can see the value that was generated. Enter the following code in the JavaScript console:
50+
<code>console.log(selfInvokingFunction);</code></p>
51+
{% endblock %}

cscourses/templates/js_examples.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<br>
55
<h3>Javascript examples</h3>
66
<p><a href="{{ url_for('main.basics') }}">Basic JavaScript examples</a></p>
7+
<p><a href="{{ url_for('main.functions') }}">JavaScript function examples</a></p>
78
<p><a href="{{ url_for('main.dom') }}">DOM examples</a></p>
89
<p><a href="{{ url_for('main.events') }}">Event examples</a></p>
910
{% endblock %}

0 commit comments

Comments
 (0)