Search code examples
javascriptloopsfor-loopfunctional-programmingcounter

Functional Programming - Simple For Loop For Incrementing Counter


We dont use for loop in functional programming, instead, we use higher order functions like map, filter, reduce etc. These are fine for iterating through an array.

However, I wonder how do I do a simple counter loop.

let i = 0;
for( i; i < 10; i++) {
  console.log( "functional programming is a religion")
};

So, how would one do this in functional programming?


Solution

  • Do not use 'while' or 'for' that is to control flows of Imperative Programming not Functional.

    Array(10).fill("functional programming is not a religion")
    .map((msg) => {
      console.log(msg);
      return msg;
    });