The Fibonacci numbers are the numbers in the following integer sequence. 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ……..

In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation

F_{n} = F_{n-1} + F_{n-2}
with seed values
F_{0} = 0 and F_{1} = 1
Examples:
Input : n = 2
Output : 1
Input : n = 9
Output : 34
The solution is a simple method that is a direct recursive implementation
mathematical recurrence relation given above.
For Input: n = 14
Output: 377

Follow me on Instagram and don’t miss the latest Challenge!

We start with the creation of the for loop. We have to define the loop is running from 2 to 10 in order to get the first 10 numbers. We start with the 0 and the 1 and need 8 more.

Any number n of the Fibonacci series is created by adding up the number n-1 with the number n-2.

To get the third number, we need to have number n-1 and n-2. With n = 3, this will be number 2 and 1 of the series.

The equation is therefore:

n = n – 1 + n – 2;

Result into:

2 = 1 + 0; The 3rd number of the Fibonacci series.

Then we need to switch the n – 1 and n – 2 to the newly created ones to go further in our Fibonacci series. E voilá, do it as long as you want to get all numbers for the Fibonacci series.

The complete outcome of the console:

Follow me on Instagram and don’t miss the latest Challenge!

To accomplish our task, we create a function called findMax() and pass in our array.

Inside the body, we need to create a variable referencing our found maximum value. We set the variable max to the first element of our array. Since this it the first maximum existing. We didn’t check any other so far.

The next step is to loop over the array and check if the element we are currently looking at is greater than our current max value.

If so, replace it to the newly found maximum element of array.

Once, we are done with iterating, comparing, and adding, just return the found maximum and print it to the console, and you are done!

Follow me on Instagram and don’t miss the latest Challenge!

Calculate the average of the numbers in an array of numbers. The array: [2, 5, 17, 81, 9].

Solution

Explanation

How to calculate an average

A calculated “central” value of a set of numbers.

To calculate it: add up all the numbers, then divide by how many numbers there are.

Example: what is the average of 2, 7 and 9? Add the numbers: 2 + 7 + 9 = 18 Divide by how many numbers (i.e. we added 3 numbers): 18 ÷ 3 = 6 So the average is 6

(Also called the Arithmetic Mean.)

Accomplishing our Goal

To accomplish our task, we create a function called averageArray() and pass in our array.

We need to get the length of the array for our for-loop. Creating a sum and then dividing by the length gets our average.

Just return the number and print it to the console, and you are done!

Follow me on Instagram and don’t miss the latest Challenge!