Learning JavaScript with freeCodeCamp (10 Part Series)

Yo! How are you, people? I hope everyone is doing great. This time, it is another short post covering different concepts in brief. This is in continuation of my JavaScript learning from freeCodeCamp. I am really enjoying it and I am certain you are as well. :)

Let's continue towards the actual content without making any delay.

#
__Recursion__

We have recently learned about JavaScript loops. The loops can be replaced by the concept of Recursion.

Recursion implies the concept where a function can be written in terms of itself. For example - when you want to find a factorial of a number, you can write it using the loop as

```
function factorial(n) {
var result = 1;
for(var i = 1; i <= n; i++) {
result *= i;
}
}
```

The result would store the result as `1 * 2 * 3 * 4 * 5`

ie `120`

if `n = 5`

.

The above works perfectly fine. But as we are trying to grasp the concept of recursion, we aim to write this function in term of itself. Before that, let's understand the factorial concept

```
5! = 5 * 4 * 3 * 2 * 1
4! = 4 * 3 * 2 * 1
3! = 3 * 2 * 1
2! = 2 * 1
1! = 1
```

I have written down the factorial expansion of number from `5`

to `1`

. Can you see a pattern in it? I am sure you can, just give it some time.

Can we write the above structure like this

```
5! = 5 * 4! // 5 * (5 - 1)!
4! = 4 * 3! // 4 * (4 - 1)!
3! = 3 * 2! // 3 * (3 - 1)!
2! = 2 * 1! // 2 * (2 - 1)!
1! = 1 * 0! // 1 * (1 - 1)!
```

Here we have written the `n!`

in terms of `(n - 1)!`

. This pattern is used by recursion. We can rewrite the above function as

```
function factorial(n) {
if (n <= 1) {
return 1; // as 1! = 1
} else {
return n * factorial(n - 1);
}
}
```

Here the condition `n <= 1`

is called a ** base condition** and every recursive function should have a base condition to mark the end of the recursive loop. When the value of

`n`

would reach to `1`

, we get the value of the `1!`

and then this can be substituted to evaluate the value of `2!`

and like this, up to `n!`

.The function calls can be seen as(for `n = 5`

)

```
factorial(5) -> 5 * factorial(4) -> 5 * 4 * factorial(3) -> 5 * 4 * 3 * factorial(2) -> 5 * 4 * 3 * 2 * factorial(1)
```

When the function call reaches to `factorial(1)`

, the function returns a finite value, instead of another recursive call, as it was doing up till now. This is then, substituted to get the final value.

I hope, I was able to explain the concept in simple words.

#
__Generating random numbers in JavaScript__

### Generating random fraction

JavaScript has a `Math.random()`

function which generates a random decimal number in the range [0, 1). The brackets imply that it includes 0, but excludes 1. So we may get a 0 as an output from this function, but not 1.

```
function generateRandomNumber() {
return Math.random(); // returns a random number -> 0.78379758
}
```

### Generating random whole numbers

We can also generate whole numbers within a range by applying some mathematics. Let's see

- Using `Math.random()`

to generate a random decimal.

- Multiplying that with the upper bound of range ie `n`

.

- Using `Math.floor`

to round the decimal to the nearest whole number.

That's it. With this process, we can get a random whole number between `0`

to `n -1`

as `n`

won't be counted in the initial `Math.random()`

function.

An example to generate the random number between 0 to 20 would be

```
function generateRandomWholeNumber() {
return Math.floor(Math.random() * 20);
}
```

This would generate a random number between [0, 20) for us. :)

### Generating random whole numbers within a range

Until now, we could generate the whole number from `0`

to `n`

. But what if we also want to use some other minimum value, other than `0`

. It's quite possible, using another mathematics trick.

```
Math.floor(Math.random() * (max - min + 1)) + min
```

This is the formula to generate a random whole number between `min`

and `max`

values.

#
`parseInt`

function in JavaScript

`parseInt`

function in JavaScriptThe `parseInt`

function in JavaScript is used to convert a string into an integer. For e.g.

```
var intValue = parseInt("00321"); // return 321
```

The above statement showed that the final value is an integer. If a string couldn't be converted to an integer, it returns `NaN`

.

The `parseInt`

method, takes an optional argument `radix`

, which specifies the base of the number provided in the string. It can have values between `2`

to `36`

.

For e.g.

```
var intValue = parseInt("1011", 2);
```

The above statement won't return `1011`

integer value, but `11`

. This is because we have provided base `2`

which tells that it is a binary number, whose decimal equivalent is `11`

.

#
__Conclusion__

At the end of this post, we got the basic idea about Recursion and how does one can implement it. Apart from it, we also learned about generating random numbers in JavaScript and using parseInt function to convert a value to an integer value.

#
__References__

This was a short post, but with this, we have covered the basic JavaScript section of freeCodeCamp. We'll meet next time with another post covering the concepts from the next section. Till then be curious and keep learning. :)

Learning JavaScript with freeCodeCamp (10 Part Series)

Posted on by:

### Prashant Sharma

Hey everyone! I am an open source enthusiast who loves to code. I am an aspiring Full stack developer. My weapon of code is Python and I enjoy creating REST APIs through Django REST Framework.

## Discussion