Javascript - Functions 2 async and arrow function
Learning goals
Asynchronicity in javascript
setTimeoutsetInterval
Arrow function
Teacher instruction
Codelab!
There are a lot of exercises. They are not meant to be finished all of them.
Level 1 I expect you to finish
Der er aflevering til mandag, i fΓ₯r tid i klassen. Fokuser pΓ₯ data forstΓ₯else aflevering!
Feedback bare lige brug 5 - 10 min pΓ₯ det
Reload this page!
Flipped classroom videos
Peer instruction
What is the value of sumOfNumbers?
8"8"4+4None of the above
Asynchronicity in javascript
setTimeout
setTimeoutsetTimeout is a way to run some javascript after some time. Let's try and log a string after 10 secondsπ
setTimeout takes two arguments
A function (also called a callback function)
Delay in milliseconds
Basiscally setTimeout waits for 10000 milliseconds (10 seconds). After that time it need to run some code. The way we tell setTimeout what to do is by giving a function to it. This function is called a callback function. It's a function (setTimeout) that takes another function π€―
setInterval
setIntervalsetInterval is a function that runs some code each interval. So if we want to log "hello" every 2 seconds we can do that using setIntervalπ
This will log hello every 2 seconds to the browser.
Arrow functions
There are two ways of defining a function
Function declaration using the
functionkeywordArrow function using an arrow
=>
Let's take a look at converting a function into an arrow function π
So we create a variable that gets assigned to the arrow function. The parameters works exactly like with the function declaration. After the parameters we add the arrow =>. The rest of the function is the same as before
It is possible to make the arrow function even more consice like shown below. But it it not necessary!
Exercises
π Exercise 1 - level 1
Create a sum function using only arrow functions
In the following exercises you can decide if you want to use arrow functions or normal function
π Exercise 2 - level 1
Write a function that takes three numbers as parameter, sums the integers and returns the result.
π Exercise 3 - level 1
Write a function that returns a string with an emoji added to the end. You choose the emoji
π Exercise 4 - level 1
Write a method that takes a string as parameter and returns the string in uppercase.
π Exercise 6 - level 1
Write a function that checks if the last character is a questionmark
π Exercise 7 - teachers and students
Duration: 20 min. Preparation: 15 min. presentation 5 min.
In the following exercise one group will randomly be selected to be teachers and the other group will be students
In groups of two people prepare a small 5 minute lecture.
The lecture should explain functions, arguments, parameters and return:
How functions work
One or more practical examples
As teachers present the 5 minute lecture
As students ask good interesting questions
π Exercise 8 - level 2
Create a stopwatch using document.write and setInterval that keeps counting up. First solve this task using the function keyword, then solve it using arrow function
π Exercise 9 - level 2
Create a function that as an argument takes how many ghosts to print and then prints that number of ghosts
π Exercise 10- level 2
Implement a BMI calculator function. Identify the needed parameters and return type.
π Exercise 11 - level 3
Create a function that sums the digits from a number. Fx the sum of the number 12400 is 7 because 1+2+4+0+0=7
π Exercise 12 - level 3
Latitude and longitude is used for finding a position on earth. It is often used when working with data. We can even get the position of our browser π
Its so cool how easy it is to access this super advanced technology!
Create a function that takes the latitude and longitude from the browser and returns a string with the following format: latitude:51.5555;longitude:16.888
Write the position to the document using document.write
π Exercise 13 - Flight booking fullname function - level 3
Even for a startup as successful as Smart-ease there needs to be money in the bank. A customer from a flight booking website has asked for our help creating a specific part of their application:
When a user books a flight they write their firstname and surname, but when the ticket is printed a fullname should be displayed. It is our responsibility to create that.
Create a function called getFullname that returns a fullname. It should have two parameters: firstname and surname.
Test that the function works by calling the function with some different names
Formal fullname
On the flight website the user has the possibility to check a checkbox that indicates if the user wants to be adressed formally. Lets also change getFullname to include support for formal name.
Create an extra parameter useFormalName that is a boolean. If it is true the function should add a Lord in front of the name.
What do we do if useFormalName is not given as an argument?
Remember to consider someone calling the function with an empty string as firstname and lastname.
Try out your code by pasting your getFullname function in the javascript part of this codepen: https://codepen.io/hackyourfuture-cph/pen/jJWwbN
But what if the person is a woman? Describe how you would fix the getFullname so it also works for women
Exercise taken from HackyourFuture
π Exercise 14 - Finish exercises from yesterday
Last updated