Skip to content
This repository was archived by the owner on Jan 14, 2024. It is now read-only.

Commit d15befd

Browse files
committed
Add exercises
1 parent 0a37034 commit d15befd

File tree

28 files changed

+1087
-0
lines changed

28 files changed

+1087
-0
lines changed

exercises/A-expressions/README.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
In JavaScript there are **expressions** and **statements**. We will use these words frequently to describe code.
2+
3+
### Expression
4+
5+
An expression returns a value. Sometimes we will say that an expression _evaluates to_ a value.
6+
7+
The following are all examples of expressions:
8+
9+
```js
10+
1 + 1; // returns 2
11+
("hello"); // returns "hello"
12+
2 * 4; // returns 8
13+
"hello" + "world"; // returns "helloworld"
14+
```
15+
16+
We can take the value produced by an expression and assign it to a variable. That line of code would be called a statement.
17+
18+
### Statement
19+
20+
A statement is some code that performs an action. Here are some examples:
21+
22+
```js
23+
var sum = 1 + 1; // action: assigns result of `1 + 1` to variable `sum`
24+
var greeting = "hello"; // action: assigns result of the expression "hello" to variable `greeting`
25+
console.log(2 * 4); // action: logs the result of `2 * 4` to the console
26+
sayGreeting(greeting); // action: calls the function `sayGreeting` with the parameter `greeting`
27+
```
28+
29+
There are some other different types of statements that we will learn in the coming weeks.
30+
31+
## Exercise
32+
33+
You quickly find out the result of an expression by running node in a terminal window.
34+
35+
* Open a terminal window
36+
* Run the command `node`
37+
* _You have now opened a node console (also called a REPL)_
38+
* Type an expression and press enter
39+
* To exit the console type Ctrl+C or type the command `.exit`
40+
41+
Example from inside a terminal window:
42+
43+
```bash
44+
$ node
45+
> 1 + 2
46+
3
47+
> "hello"
48+
'hello'
49+
> var greeting = "hello"
50+
undefined
51+
> greeting
52+
'hello'
53+
> console.log(greeting)
54+
hello
55+
undefined
56+
> .exit
57+
$
58+
```
59+
60+
> Notice how when we execute an expression the value it produces is printed below it. When we execute a statement, we see `undefined` printed below. This is because statements don't produce values like expressions, they _do something_.
61+
62+
* Write some more expressions in the node console
63+
* Assign some expressions to variables
64+
* Check the value of the variables
65+
66+
Further reading on using the node console: https://hackernoon.com/know-node-repl-better-dbd15bca0af6
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
There is a special data type in JavaScript known as a **boolean** value. A boolean is either `true` or `false`, and it should be written without quotes.
2+
3+
```js
4+
var codeYourFutureIsGreat = true;
5+
```
6+
7+
## Exercise
8+
9+
Head over to `exercise.js` and follow the instructions in the comments.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
BOOLEAN LITERALS
3+
----------------
4+
This program needs some variables to log the expected result.
5+
Add the required variables with the correct boolean values assigned.
6+
*/
7+
8+
var codeYourFutureIsGreat = true;
9+
10+
/*
11+
DO NOT EDIT BELOW THIS LINE
12+
--------------------------- */
13+
14+
console.log("Is Code Your Future great?", codeYourFutureIsGreat);
15+
console.log("Is Mozafar cool?", mozafarIsCool);
16+
console.log("Does 1 + 1 = 2?", calculationCorrect);
17+
console.log("Are there more than 10 students?", moreThan10Students);
18+
19+
/*
20+
EXPECTED RESULT
21+
---------------
22+
Is Code Your Future great? true
23+
Is Mozafar cool? false
24+
Does 1 + 1 = 2? true
25+
Are there more than 10 students? false
26+
*/
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
We can also write **expressions** that return boolean values.
2+
3+
Here's an expression that evaulates to a boolean.
4+
5+
```
6+
1 > 2
7+
```
8+
9+
* Can you work out what value this expression evaluates to?
10+
11+
The `>` symbol in the expression is a **comparison operator**. Comparison operators compare two values. This operator checks to see if the number on the left is bigger than the number on the right.
12+
13+
`1` is not bigger than `2` so this expression returns `false`.
14+
15+
**More comparison operators**
16+
17+
```
18+
> greater than
19+
< less than
20+
<= less than or equal
21+
>= greater than or equal
22+
=== same value
23+
!== not the same value
24+
```
25+
26+
## Exercise
27+
28+
* Open `exercise.js` and follow the instructions.
29+
* Open a node console, and write some expressions that use comparison operators
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
BOOLEAN WITH COMPARISON OPERATORS
3+
---------------------------------
4+
Using comparison operators complete the unfinished statements.
5+
The variables should have values that match the expected results.
6+
*/
7+
8+
var studentCount = 16;
9+
var mentorCount = 9;
10+
var moreStudentsThanMentors; // finish this statement
11+
12+
var roomMaxCapacity = 25;
13+
var enoughSpaceInRoom; // finish this statement
14+
15+
var personA = "Daniel";
16+
var personB = "Irina";
17+
var sameName; // finish this statement
18+
19+
/*
20+
DO NOT EDIT BELOW THIS LINE
21+
--------------------------- */
22+
console.log("Are there more students than mentors?", moreStudentsThanMentors);
23+
console.log(
24+
"Is there enough space in the room for all students and mentors?",
25+
enoughSpaceInRoom
26+
);
27+
console.log("Do person A and person B have the the same name?", sameName);
28+
29+
/*
30+
EXPECTED RESULT
31+
---------------
32+
Are there more students than mentors? true
33+
Is there enough space in the room for all students and mentors? true
34+
Do person A and person B have the the same name? false
35+
*/

exercises/D-predicates/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
**Predicate** is a fancy word for a function that returns a boolean value.
2+
3+
These functions are very useful because they let you test if a value satisifies certain requirements.
4+
5+
```js
6+
function isNumber(value) {
7+
return typeof value === "number";
8+
}
9+
10+
isNumber(10); // returns true
11+
isNumber("hello"); // returns false
12+
```
13+
14+
JavaScript programmers often give predicate functions a name that starts with a verb e.g. `isBig`, `isNegative`, `isActive`, `shouldUpdate`,
15+
16+
Calling a predicate function is like asking a question: "is this value a number". The return value is the answer to your question.

exercises/D-predicates/exercise.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
Predicates
3+
---------------------------------
4+
Write a predicate to predicates
5+
The variables should have values that match the expected results.
6+
*/
7+
8+
// Finish the predicate function to test if the passed number is negative (less than zero)
9+
function isNegative(number) {
10+
11+
}
12+
13+
// Finish the predicate function to test if the passed number is between 0 and 10
14+
function isBetweenZeroAnd10(number) {
15+
16+
}
17+
18+
/*
19+
DO NOT EDIT BELOW THIS LINE
20+
--------------------------- */
21+
var number = 5;
22+
var numberNegative = isNegative(number);
23+
var numberBetweenZeroAnd10 = isBetweenZeroAnd10(number);
24+
console.log("The number in test is " + number);
25+
console.log("Is the number negative? " + numberNegative);
26+
console.log("Is the number between 0 and 10? " + numberBetweenZeroAnd10);
27+
28+
/*
29+
EXPECTED RESULT
30+
---------------
31+
The number in test is 5
32+
Is the number negative? false
33+
Is the number between 0 and 10? true
34+
*/
35+

exercises/E-conditionals/README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
Like humans, computer programs make decisions based on information given to them. **Conditionals** are a way of representing these decisions in code.
2+
3+
For example:
4+
5+
* In a game, if the player has 0 lives, then the game is over
6+
* In a weather app, if rain is forecast, a picture of rain clouds is shown
7+
8+
The most common type of conditional is the **if statement**.
9+
10+
An if statment runs some code if a condition is met. If the condition is not met, then the code will skipped.
11+
12+
```js
13+
var isHappy = true;
14+
15+
if (isHappy) {
16+
console.log("I am happy");
17+
}
18+
```
19+
20+
The code in paratheses - e.g. `(isHappy)` - is the condition. The condition can be _any_ expression. The following are all valid conditions:
21+
22+
```js
23+
// boolean value
24+
if (true) {
25+
// do something
26+
}
27+
28+
// variable assigned to boolean value
29+
if (isHappy) {
30+
// do something
31+
}
32+
33+
// equality operator returns a boolean value
34+
if (1 + 1 === 2) {
35+
// do something
36+
}
37+
38+
// comparison operator returns a boolean value
39+
if (10 > 5) {
40+
// do something
41+
}
42+
43+
// function call returns boolean value
44+
if (greaterThan10(5)) {
45+
// do something
46+
}
47+
```
48+
49+
An if statement runs code when a condition is met. What if the condition is not met? Sometimes you want to run an alternative bit of code.
50+
51+
An **if...else statement** also runs code when the condition is _not_ met.
52+
53+
```js
54+
var isHappy = true;
55+
56+
if (isHappy) {
57+
console.log("I am happy 😄");
58+
} else {
59+
console.log("I am not happy 😢");
60+
}
61+
```
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
Conditionals
3+
---------------------------------
4+
Add an if statement to check Daniel's role in a CYF class.
5+
If Daniel is a mentor, print out "Hi, I'm Daniel, I'm a mentor."
6+
If Daniel is a student, print out "Hi, I'm Daniel, I'm a student."
7+
*/
8+
9+
var name = "Daniel";
10+
var danielsRole = "mentor";
11+
12+
/*
13+
EXPECTED RESULT
14+
---------------
15+
Hi, I'm Daniel, I'm a mentor.
16+
*/
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
There are three logical operators in JavaScript: `||` (OR), `&&` (AND), `!` (NOT).
2+
3+
They let you write expressions that evaluate to a boolean value.
4+
5+
Suppose you want to test if a number if bigger than 3 and smaller than 10. We can write this using logical operators.
6+
7+
```js
8+
var num = 10;
9+
10+
function satisfiesRequirements(num) {
11+
if (num > 3 && num < 10) {
12+
return true;
13+
}
14+
15+
return false;
16+
}
17+
```
18+
19+
We can test expressions with logical operators in a node console too:
20+
21+
```sh
22+
$ node
23+
> var num = 10;
24+
undefined
25+
> num > 5 && num < 15
26+
true
27+
> num < 10 || num === 10
28+
true
29+
> false || true
30+
true
31+
> !true
32+
false
33+
> var greaterThan5 = num > 5
34+
undefined
35+
> !greaterThan5
36+
false
37+
> !(num === 10)
38+
false
39+
```

0 commit comments

Comments
 (0)