Print Friendly, PDF & Email

On this occasion we will discuss four ways of thinking to program Javascript, with approaches or iterative, recursive, functional and declarative programming paradigms. Each one of them is so different of the other that is them considered as paradigms of programming separate, although this not wants tell that an expert not can use them ones with others. It is always good to know ways to think and solve problems.

think for programming in Javascript

Definition of the problem

To show the different forms of thinking, consider the following problem:

Given a number n that is a whole positive get all the powers of 2 from the zero to the value n and list them in a vector of results.

Programming Javascript with thought iterative

This is the approach to traditional to program in Javascript. For the approach to iterative must of broken the Poblem for enhance it step by step. The people who solve the problem using this paradigm usually first solve the problem with a mathematical approach and then encode it. This type of code is too explicit, but can shape is easily with diagrams of flow. Concepts that can be included implicitly programmes might be indexes, state machines, conditional, repetitive, etc.

  1. Define that must of return an array.
  2. For each number i with values ranging from 0 up to n add to arrangement 2 elevated to the i-th power
  3. Return the array with the values


function powersOfTwo (n) {}
  var result = [];
  for (var i = 0; i < = n; i)}
    result.push (Math.pow(2, i));
  return result;

Programming Javascript with recursive thought


He approach recursive is an approach that is focused on a mentality of that sometimes is more easy understand a task repetitive with minimum variations that break down it in many steps. This type programs tends to be fairly compact but very powerful if is has much RAM.

This approach requires a purely algorithmic approach, used mathematics tend to be minimalist and are very much based on the principle of induction of the discrete mathematics. Concepts that could come implicit in this type of programs could be, condition of failure, recursion of two steps, induction mathematical, subroutines, cuts, etc.

  1. For the case more simple if n is worth zero must return the value 1 within an arrangement.
  2. In other cases, return the value of the power of 2 to the n at the end of an arrangement that create us this procedure call for the value n removing one.
function powersOfTwo (n) {}
  if(n==0) {}
  } else {}
    return powersOfTwo(n-1) .concat (Math.pow(2,n));

Programming Javascript with thinking functional

Functional Javascript

This approach to programming in Javascript arises mainly in EcmasCript 6 (and it love all the hipsters of the code!). For the approach functional will need a metalidad strictly mathematical, and a reasoning causal. It is a less common approach since a very large capacity for abstraction is needed. Typically functional concepts that come normally implicit in this type of program could be generators, iterators, lambdas, etc.

  1. Return an array of size n 1 with them values individual created by the following function
  2. Each element e with index i us refresará a value of 2 high to the power i
powersOfTwo = n =>.[...Array(n - 1)]map ((e, i) => Math.pow(2, i));

Programming Javascript with declarative thinking

This is the noevo approach that is looking for in the new version Ecma Script 7 to program in Javascript. This approach requires knowing how to express that it is what you want without saying how you want it done. This approach contains to the beauty of that is easily understandable to who it reads, but its implementation may hide details as processing asynchronous and optimizations. Some concepts that could come embedded in this type of program might be backtracking, unification, peresoza, model evaluation of data, etc.

  1. Return an array based on the following elements: high 2 function to the index of an element and an array of size n-1.
function powersOfTwo (n) {}
  return Array.from ({length: n-1}, (e, i) => Math.pow(2,i));


Do NOT follow this link or you will be banned from the site!