Javascript - Functions 1
Defining functions
Scope
Argument/Parameters
Return Values
Static Methods
Preparation:
Exercises A
π Exercise 1
Create two arrays with the following values: [5,6,3,4,6,34,5,5] [5,5,5,66,7,3,1,1,1,4]
Concatenate the arrays and sort the concatenated array
Ensure the array has been sorted by logging the results
π Exercise 2
Use the parseInt method to parse the two strings to integers, such that we can obtain a result of 44 instead of 2222
π Exercise 3 (advanced)
Use a loop, the split method and the parseInt method to calculate the total age of all participants
Exercises B
π Exercise 1
Create a function that takes a string and then logs that string out
π Exercise 1.1
Create a function that adds two numbers together
π Exercise 2
Create a function that takes a name and returns true if the first character is the character a otherwise false
Hint: Check out https://stackoverflow.com/questions/3427132/how-to-get-first-character-of-string
π Exercise 3
The following exercises requires you to understand how to convert celcius to fahrenheit and back again.
Celsius to Fahrenheit Formula: (Β°C * 1.8) + 32 = Β°F
Fahrenheit to Celsius Formula: (Β°F - 32) / 1.8 = Β°C
π Exercise 3A
Create a function called celciusToFahreneit it should have a parameter called celcius.
It should return a string in the following format [CONVERTED_TEMPERATURE] degree fahrenheit fx 23 degree fahrenheit
Reflect:
What are meaningful variables for the function
π Exercise 3B
Create a second function called fahrenheitToCelcius it should have a parameter called fahrenheit
It should return a string in the following format [CONVERTED_TEMPERATURE] degree celcius
π Exercise 3c
Create a third function called convertTemperature it should have two parameters inputScale and degrees
If the inputScale argument is equal to "fahrenheit", the function should call the
celciusToFahrenheitand log the resultIf the inputScale argument is equal to "celcius", the function should call the
FahrenheitToCelciusand log the resultIf the inputscale argument is not equal to either, the function should log
invalid input
π Exercise 4
Create a function called increaseByHalf that should
Take a number as an input
Return this input number increased by half of the input number
Here is an example of the output
π Exercise 5
Examine out the code below:
The functions above behave similarly but differ in some important ways.
Study the code above and then answer the following questions:
How many times is the function
printMessagecalled ?How many times is the function
getMessagecalled ?What is the parameter name for the function
printMessage?What is the parameter name for the function
getMessage?
From Code your future
π Exercise 6
Create a function that has two parameters: stringToLog and numberOfTimesToLog
When calling the function it should log out the stringToLog the amount of times specified in numberOfTimesToLog
If the numbersOfTimesToLog is longer than the string length, the function should log: Invalid parameters
Here is an example of the output π
π Exercise 7 (Advanced - optional)
Create a function that will return either true or false.
The function returns true if it receives an argument that is a valid CPR-number.
The function receives input from the user by using the
prompt()function in javascripthttps://www.w3schools.com/jsref/met_win_prompt.asp
π Exercise 8 (Advanced - optional)
When working with data we often need to convert data from one format to another therefore: Create a function that takes a date in the following format: MONTH/DAY/YEAR fx 10/24/2022. It should return a date in the following format: DAY-MONTH-YEAR fx 24-10-2022
Hint: research the .split method
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split
The function receives input from the user by using the prompt() function in javascript
https://www.w3schools.com/jsref/met_win_prompt.asp
π Exercise 9 (Advanced - optional)
Create a function (that you have to name) that has temperature as parameter. Based on the temperature it should return a string with what the user should wear. You decide what the user should wear based on the temperature.
An example is:

π Exercise 10 (Advanced - optional)
You specify how many days from today an event is being held. The function then figures out what weekday the event is being held. Here is an example:
Today is Sunday and the event is in 5 days. Therefore the event will be held on a friday.
You should get the today's day from the system.
Hint: use remainder operator, array indexes and investigate new Date in js.
Last updated