Skip to content
Permalink
Browse files
Merge branch 'master' into master
  • Loading branch information
aa7401 committed Oct 26, 2019
2 parents 4766658 + afa4426 commit 0b987919e9a32f623b532f74592c0740695dec79
Show file tree
Hide file tree
Showing 16 changed files with 338 additions and 82 deletions.
@@ -0,0 +1,60 @@

# Setup for Windows 10 Users

The tools used in this module are designed to be used in a Unix or Linux environment. Whilst this will create challenges for Windows 10 user there are some steps you will need to take. Please read and follow the steps below carefully:

Install the Windows Subsystem for Linux (WSL). Open PowerShell as Administrator and run:

``` shell
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux
```

Restart your computer when prompted.

Now install Visual Studio Code and once launched install the [remote-wsl](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-wsl) component by Microsoft. You should also install the [browser-preview](https://marketplace.visualstudio.com/items?itemName=auchenberg.vscode-browser-preview) component which will allow you to open html web pages.

![remote-wsl](exercises/.images/remote-wsl.png)

Now you need to install Ubuntu. This can be found by searching for `run linux on windows` in the Microsoft Store. You will be presented with the following screen.

![Microsoft Store](exercises/.images/store.png)

Choose the Ubuntu operating system (v18.04 LTS) and install. Once installed, click on the **Launch** button, this will open a console window and you will need to wait for a few minutes for the installation to complete.

## Cloning the Forked Repository

You will now need to fork the foundation lab by clicking on the Fork button. This will create a copy of the repository. See the standard setup instructions for more details.

Now you can clone your forked repository by running the following command in the Ubuntu console, replacing xxx with the URL of your repository.

```shell
git clone xxx
```

This will create a directory called `foundation` in the Ubuntu console. The final step is to launch VS Code from within the WSL environment by running the following command:

```shell
code foundation
```

This will launch VS Code from within the WSL with the contents of the `foundation/` directory. If you open the integrated terminal (using the **Terminal** menu) you will see that you have the full ubuntu bash shell. You can now run all the remaining steps from this integrated terminal, just as you would for a standard Linux install.

## Installing NodeJS

These steps are identical to those used on a full Ubuntu installation. Start by installing the Node Version Manager (NVM) tool:

```shell
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.0/install.sh | bash
nvm --version
nvm install node
```

If the `nvm` command is not found you will need to reload the shell:

```shell
source ~/.profile
```

Now try to install again.

Now you can go directly to step 4 in the standard setup instructions.
@@ -536,7 +536,7 @@ body {

The next task is to build a navigation menu bar to the website using CSS. Depending on the number of menu items, the menu bar can contain multi-level menus.

A menu bar is be defined as an HTML unordered list. Each menu in a menu bar is a list item.
A menu bar can be defined as a HTML unordered list. Each menu in a menu bar is a list item.

The menus can contain submenus. A submenu is coded as a list that is within the parent menu's list item.

@@ -42,17 +42,95 @@ Start by running the `maths.js` script and map the output it generates against t

1. Create a new function called `multiply()` that takes two parameters, `a` and `b` and returns the _product_ of the two.
- what happens if you call it with only a single parameter?
2. Modify the function so it uses a default parameter to multiply by 1 if the second parameter is missing.
- What happens if you don't supply _any_ parameters?
- Add a second default parameter to prevent this.
3. Write an _arrow function expression_ stored in a constant called `squareRoot` which calculates and returns the square root of the supplied number. You will need to use the `sqrt()` method which is part of the `Math` object.
2. Write an _arrow function expression_ stored in a constant called `squareRoot` which calculates and returns the square root of the supplied number. You will need to use the `sqrt()` method which is part of the `Math` object.

Open the `contact.js` script, implement the `validateEmail()` function and thoroughly test it, you should avoid using regular expressions at this stage:

1. Check that the string is at least 5 character long
2. Check that there is a `@` character and that it is not at the start of the string (HINT: use the [indexOf](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf) String prototype method.
3. Check that there is a period (.) character after the `@` character but before the end of the string.

#### 1.1.2 Function Parameters

In the JavaScript language although we define a function with a set of specified _parameters_, when we call the function we don't need to pass these arguments:

We can choose to pass fewer arguments than are specified in the function parameters. Any parameters that don't have a matching argument are set to `undefined`. For example, the following code will print `undefined`.

```javascript
function sqr(num) {
return num * num
}
sql() // returns NaN (not a number)
```

This can cause issues in your code so to prevent this we provide [Default Parameters](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Default_parameters). If an arguement is missing when a function is called this specified a default value to use. For example consider this version of the function:

```javascript
function sqr(num = null) {
return num * num
}
sqr() // returns 0
```

In JavaScript:

- A value of `undefined` means a value has not been assigned to a variable.
- A value of `null` is a value assigned to a variable and means _no value_.

It is also possible to pass in more arguements than there are parameters in the function declaration. The following is quite valid:

```javascript
function add(num1, num2) {
return num1 + num2
}
add(4, 2, 1) // returns 6
```

As you can see, if there are too many arguments, the extra ones are ignored however JavaScript provides a mechanism to access all the arguments passed to a function regardless of whether they match the parameter list by using the _array-like_ `arguments` object, for example:

```javascript
function add() {
let total = 0
for(arg of arguments) total += arg
return total
}
add(4, 2, 1) // returns 7
```

Using _hidden_ or _magic_ variables that magically come into existence can make your code hard to understand so ECMA6 introduced [Rest Parameters](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters), parameters that can hold any arguments that don't match the parameters in the function declaration. Take the following code:

```javascript
function add(num1, num2, ...others) {
let total = num1 + num2
for(arg of others) total += arg
return total
}
add(4, 2,1) // returns 7
```

This demonstrates how the rest parameter _mops up_ any surplus arguments and could be written as:

```javascript
function add(...numbers) {
let total = 0
for(arg of numbers) total += arg
return total
}
console.log(add(4, 2, 1)) // returns 7
```

#### 1.1.3 Test Your Understanding

1. create a function called `divideThis()` that takes two arguments, a number to be divided, `dividend` and the number to divide by, `divisor`. The function should return the _quotient_.
2. What happens if you don't pass a parameter for the `divisor` parameter? Can you fix this by supplying a suitable _default parameter_?
3. Call the `multiply()` function from the previous task omitting the second parameter. Can you modify the function so it uses a default parameter to multiply by 1 if the second parameter is missing.
- What happens if you don't supply _any_ parameters?
- Add a second default parameter to prevent this.
4. Create a new function called `average()` that takes one or more numerical parameters to return the average of these:
- Write this to make use of the `arguments` construct.
- Rewrite this to use an ECMA6 rest parameter.

### 1.2 Function Expressions

Functions are a data type in JavaScript (they are objects but more on that later). As such they can be stored in variables for later execution. Prior to ECMA6 they were declared using the `function` keyword like this:
@@ -99,7 +177,7 @@ const sqr = num => num * num
2. Compare this to the original version: which is more _readable_?
3. Create a function expression that takes two string parameters and returns the longest string and assign this to a constant called `longest. check this works correctly.
4. Modify the function expression so that it can handle any number of string parameters (use a _rest parameter_). (hint: you will need to use a [`for...in`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in) statement to loop through the strings. How does this differ from a [`for...of`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of) statement?)
5. Use a [ternary operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator) instead of the `if` statement in the loop.
5. Use a [ternary operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator) instead of the `if` statement in the loop.
6. Finally use the [`reduce()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce) method to replace the `for...in` loop to reduce the function to a single line.
## 2 Callbacks
@@ -235,6 +313,13 @@ const grade = employee.grade || 'A'

This will retrieve the value of the grade property if defined and store it in the `const` variable. If this property is missing the `const` variable will contain the string `'A'`.

#### 3.2.1 Test Your Understanding

1. Create a new object called `university` which should contain three properties, `year1`, `year2` and `year3`. Each of these properties should store an object whos keys are the module codes and values the titles of the modules.
2. Create a variable called `study01` containing the `year1` object.
3. Use the `for...in` statement to iterate over this `study01` object printing out all of the _module codes_.
4. Use the `for...of` statement to print out all of the _module names_.

### 3.3 JSON Data

JSON (JavaScript Object Notation) is a standard text-based format to represent structured data. This is very useful as it means we can take any JavaScript object and convert it into a text string. This can then be saved to disk or posted to a web server, etc. It also means that you can take a JSON-formatted text string and convert it into a complex JavaScript object!
@@ -289,10 +374,24 @@ Lets apply our knowledge of callbacks to implement a simple quotes tool.
3. The contents of the file is a utf8 string, use `JSON.parse()` to convert this into a JavaScript object (array) and print this to the terminal instead.
4. Create a loop to iterate through the array, printing the contents of each index.
5. Modify the code so that it only prints the quotes string (not the entire object).
6. Convert the `university` object from the previous exercise into a JSON string and save it to the filesystem as `university.json`.
### 3.3 ECMA6 Object Destructuring
In ECMA6 is is possible to extract multiple pieces of data into separate variables by destructuring using an _object pattern_. This is syntactically similar to creating object literals (see the example below).
There are situations where we want to retrieve multiple object properties and store then in different variables, for example:
```javascript
const employee = {
firstName: 'Colin',
'last name': 'Stephen',
'department': 'Computing'
}
const first = employee.firstName
const last = employee['last name']
console.log(`${first} ${last}`)
```
In ECMA6 it is possible to extract multiple pieces of data into separate variables by destructuring using a [Desctructuring Assignment](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#Object_destructuring). This is syntactically similar to creating object literals (see the example below).
```javascript
const employee = {
@@ -301,11 +400,15 @@ const employee = {
'department': 'Computing'
}
let {firstName: first, 'last name': last, department: dept} = employee
const {firstName: first, 'last name': last, department: dept} = employee
console.log(first) // prints 'Colin'
console.log(dept) // prints 'Computing'
```
#### 3.3.1 Test Your Understanding
1. Take the `university` object you created in an earlier exercise and use a single line destructuring assignment to create three variables, `year1`, `year2` and `year3`.
### 3.4 Getters and Setters
Most object properties are simple values and you can simply assign a value. Sometimes however properties need to be calculated. One solution is to store a function as one of the properties however we would need to call a function to retrieve the value:
@@ -582,7 +685,7 @@ const matt = new ECMA6Student('matt')
console.log(ECMA6Student.studentCount()) // prints '2'
```
Notice that the static vsriable `count` is public (so the `studentCount()` method is somewhat superfluous in this example!). This highlights one of the limitations of JavaScript, the lack of a simple way to define private attributes (variables and methods). The next section goes into this in more detail and explains some workarounds (hacks) to get around this.
Notice that the static variable `count` is public (so the `studentCount()` method is somewhat superfluous in this example!). This highlights one of the limitations of JavaScript, the lack of a simple way to define private attributes (variables and methods). The next section goes into this in more detail and explains some workarounds (hacks) to get around this.
### 4.5 Handling Data Encapsulation
@@ -601,20 +704,3 @@ You should take time to understand the [pros and cons](https://2ality.com/2016/0
2. Use this to create a second **constructor function** class called `OldPickup` that includes `payload` and `seats` fields and use this to create two pickup objects.
3. Now use the same information to create a class called `NewVehicle` and extend this to create a class called `NewPickup` and use this to create two or more pickup objects.
4. Add a static member to capture the total value of all the pickup sales and print this to the terminal.
Show how objects can be turned into strings and saved. text data loaded and converted into a JavaScript object.
### 5.1.1 Test Your Understanding
### 5.2 RESTful APIs
Show how data can be retrieved from an API in JSON format.
//TODO: use the OMDB API in this section
OMDB key: 220d2590
First task is for students to get an OMDB API key and paste it into the provided script.

0 comments on commit 0b98791

Please sign in to comment.