# Solving Problems in JavaScript Part 1: Recursive vs Iterative

### Aleksandar Vasilevsk ・3 min read

This is just a preview of my original post cross-posted from my **blog**

A problem is an obstacle to overcome, once the obstacle circumvented, the problem is solved and we can move on the next one. When we write programs to solve problems, though, we have a larger goal. We don't want to solve just one instance of a particular problem, we want an algorithm that will solve all instances of a problem. A problem is defined by its inputs and the desired output.

Our goal for solving the problem is to devise a procedure that takes inputs that define a problem instance and produces an output that is the solution to that problem. Most problem solving involves breaking problems into simpler and simpler problems until you find problems simple enough that you already know

how to solve. This approach of solving problems by breaking them into simpler parts is known as **divide-and-conquer**.

Note: An algorithm is a well-defined, computer-implementable instructions, typically to solve a problem or to perform a computation.

## Recursive Problem Solving

Recursive problem solving is where a function calls itself again to repeat the code with a goal to reduce the problem to be simple enough. To define recursion, we use an if expression to test the input. If it is true, the consequent expression is the known answer, otherwise, if the test is false, the recursive case applies the procedure again but with a smaller input. For the application to make progress towards reaching the base case, means the input has to change that gets closer to the base case input. The recursion is more used in functional programming.

### Recursive Factorial Example

```
function factorial(number) {
if (number === 1) {
return 1;
}
return number * factorial(number - 1);
}
```

As we see in the factorial function we have a number parameter which we test with an if statement. If the test is true (the number is `1`

) we stop the cycle, if the test is false (the number is not `1`

) we are multiplying the number with the factorial function, which is called by itself.

Note: Factorial is the operation of multiplying any natural number with all the natural numbers that are smaller than it.

### Recursive Fibonacci Example

```
function fibonacci(number) {
if(number <= 1){
return 1;
}else {
return fibonacci(number - 1) + fibonacci(number - 2);
}
}
```

In our second example is probably one of the most famous algorithms ever **Fibonacci sequence**. We have a number parameter that we test with an if statement. If it is true, then we return 1 and the cycle ends here. if the test is false we are are going on the next test. Here at the else clause, we have a return statement with two Fibonacci functions which are added with the first one have a parameter `number - 1`

and the second one `number - 2`

.

Note: Fibonacci numbers, also called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1.

You can read the full post with the second part about iteration on my **blog**