Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions courses/Foundation/assignment-projects/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Assignment Projects

TODO

We need to restructure/redesign the old javascript homework projects to fit with the new modules here. Leaving this stub for now, so we can point all old links here.
8 changes: 6 additions & 2 deletions courses/Foundation/intro-to-frontend/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Introduction to Frontend
# Intro to Frontend

TODO
Here you can find module content and assignments for the Intro to Frontend module.

| Week | Topic | Preparation | Homework | Lesson plan |
| ---- | ----------------------------------------------------------------------------- | ------------------------------------- | ------------------------------------- | ------------------------------------- |
| 1. | [The browser environment, DOM, event listeners and making requests](./week1/) | [Preparation](./week1/preparation.md) | [Assignment](../assignment-projects/) | [Lesson plan](./week1/lesson-plan.md) |
171 changes: 171 additions & 0 deletions courses/Foundation/intro-to-frontend/week1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
# Intro to Frontend, Week 1

## Learning goals

Browser environment

- [ ] HTML vs CSS vs JS
- [ ] Client vs server
- [ ] Where is the script tag being loaded

DOM

- [ ] What is it and what do developers use the DOM for?
- [ ] DOM manipulation
- [ ] Get elements
- [ ] Insert elements
- [ ] Element manipulation (style, innerHTML, text)
- [ ] Window object
- [ ] Document object

Event listeners

- [ ] Document onload
- [ ] Click, submit, change, input - Focus on usage

Making requests

- [ ] Fetch (No promise explanation! Focus on usage)

## Relevant links

- [Preparation](preparation.md)
- [Assignment](../../assignment-projects/)
- [Lesson plan](lesson-plan.md)

## HTML interaction
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't find any relevant content for Fetch to copy into here (which covers the other topics)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't added anything here since we didn't have anything in the legacy modules. But i really think after this restructure, a valuable task would be to go through each module and tidy up content like this.


Interacting with the HTML DOM is done through the document object in the browser. With the document object we can get html elements and change them.

For the next js part we use this html.

```html
<html>
<body>
<h1>Javascript week 3</h1>
<div class="queue"></div>
<div id="test-id"></div>
<script src="js/review.js"></script>
</body>
</html>
```

```js
// use the querySelector to select elements just like in css
const testIdElement = document.querySelector("#test-id");
console.log(testIdElement); // logs the html element with id "test-id"

// Change the inner html of the test-id element
testIdElement.innerHTML = "test";
// Change the background-color of the test-id element. Inline css changes is done via the style attribute on the element
testIdElement.style.backgroundColor = "blue";

// It is also possible to create html elements
// Create a div element
const div = document.createElement("div");
// Change its inner html
div.innerHTML = "We created this div!!!";

// Lest append it to the div with the class queue
const queueDiv = document.querySelector(".queue");
queueDiv.appendChild(div);
```

## Code Commenting

First the straightforward part: how do we place comments in our code?

### HTML

[W3Schools](https://www.w3schools.com/html/html_comments.asp)
Comments

```html
<!-- Write
your comments here -->

<!-- Write your comments here -->
```

### CSS

[MDN on CSS comments](https://developer.mozilla.org/en-US/docs/Web/CSS/Comments)

```css
/* Comment */

/*
A comment
which stretches
over several
lines
*/
```

### JavaScript

Single line comments

```js
// Change heading:
document.getElementById("myH").innerHTML = "My First Page";
```

Single line comments at end of the line:

```js
const x = 5; // Declare x, give it the value of 5
```

Writing js documentation: [JSDoc](https://usejsdoc.org/) <!-- http-trash -->

### When to comment?

Now for the hard part: when to comment? When you work for different companies, you will see different styles. Embrace something you like, and then learn to let go. Google on "when to comment code?" and you'll find a big bunch of different opinions.

The general concept is, however, that it is there to help make the code more easy to understand. Note, however, that comments can also make code more difficult to understand when not applied properly.

## Events

Events in JavaScript are things like:
A timer has just finished, a user clicked a button, our page has loaded,
someone types into an input element or we have just gotten some data from a server.
When these events happen, we usually want to add some functionality.
For example, when a user clicks the like button (event), we want to increment the like counter and color the like button blue.
Or when someone clicks "Close cookies" (event) we want to remove the cookie div.

Lets first try to create some js that waits for 2 seconds and the console.logs out "2 seconds has elapsed!"

In JavaScript we use an _event listener_ to listen:

```javascript
setTimeout(function () {
console.log("2 seconds has elapsed!");
}, 2000);

// Cool, now lets make a function as a variable:
const fourSecondLog = function () {
console.log("4 seconds has elapsed!");
};

setTimeout(fourSecondLog, 4000);
```

Now lets try and log out "button clicked!" when a button is clicked.

To check if a button gets clicked we use a what is called an _event listener_.

Imagine a person listening to the click of a button and every time he hears a click he yells out "CLICKED".

```javascript
const buttonElement = document.querySelector("button");
buttonElement.addEventListener("click", function () {
console.log("Button clicked!");
});

const buttonClicked = function () {
console.log("Button clicked as a variable!");
};
// Cool man! Lets try and add that function as a variable.
buttonElement.addEventListener("click", buttonClicked);
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Get random integer between two numbers, found here: https://stackoverflow.com/a/7228322
* @param {integer} min - The min number
* @param {integer} max - The max number
* @returns {Number} Random number between min and max
*/
function randomIntFromInterval(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}

/**
* Get an array with car objects with random color and speed
* @param {integer} numberOfCars - The number of cars
* @returns {array} Array containing the car objects
*/
function generateCars(numberOfCars) {
const cars = [];

const carMakes = ["Honda", "BMW", "Fiat", "Skoda", "Volvo"];
const carColors = [
"lightgrey",
"lightcyan",
"lightyellow",
"lightgreen",
"lightcoral",
"lightpink",
];

for (let i = 0; i < numberOfCars; i++) {
const car = {};
const randomMakeIndex = randomIntFromInterval(0, carMakes.length - 1);
const randomColorIndex = randomIntFromInterval(0, carColors.length - 1);

car.make = carMakes[randomMakeIndex];
car.color = carColors[randomColorIndex];
car.speed = randomIntFromInterval(0, 100);

cars.push(car);
}

return cars;
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading