A Very Simple Example of Functional Programming in JavaScript

The functional programming paradigm is quite a popular thing at the moment and it is something I keep meaning to take more of a look at and add to my work process. I thought I would start with something very simple; a loop that would call another function and iterate over itself.

Here’s my example, as you can see it does nothing special just takes two values and increments one of them. It calls the console log function and if the count is not reached does the whole thing again.

const looper = (start, count) => {
  // do something or call something
  console.log(start);
  
  start += 1;

  if (start <= count) { 
    looper(start, count);
  }
}

looper(1, 10);

Javascript Events

…and triggering them

Recently I had an issue attempting to dispatch an event to a DOM element in a unit test. The particular element was a radio button and the way I would normally create an event:

var event  =  new Event('click',{'bubbles':true, 'cancelable':false})

object.dispatchEvent(event)

just didn’t work, this led to some digging about, looking through various documentation .. you know the score.

In the end I had to resort to a much older method of using object.click()

object.click() 
new MouseEvent('click', {bubbles:true});

more information:

https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/click

https://developer.mozilla.org/en-US/docs/Web/API/Event/Event

https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent