Javascript - Functions 1

  • Defining functions

  • Scope

  • Argument/Parameters

  • Return Values

  • Static Methods

Preparation:

Videoarrow-up-right

Function basicsarrow-up-right

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

πŸ“ Exercise 3 (advanced)

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-stringarrow-up-right

πŸ“ 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 celciusToFahrenheit and log the result

  • If the inputScale argument is equal to "celcius", the function should call the FahrenheitToCelcius and log the result

  • If 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:

  1. How many times is the function printMessage called ?

  2. How many times is the function getMessage called ?

  3. What is the parameter name for the function printMessage ?

  4. What is the parameter name for the function getMessage ?

From Code your futurearrow-up-right

πŸ“ 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 javascript

    • https://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:

Wearing jackets

πŸ“ 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