Functional Programming - Simple For Loop For Incrementing Counter

Kayote picture Kayote · May 2, 2016 · Viewed 9k times · Source

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?

Answer

user1028880 picture user1028880 · Jul 8, 2018

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;
});