Divide an integer into evenly distributed groups with an ES6 generator

Say you’d like to divide an integer, e.g. 20, into equal parts of 3 and distribute any remainder equally across the groups. The result of such an operation would be the following 3 integers:

7,7,6

20 can be divided into 3 equal parts of 6 and we have a remainder of 20 – 6 * 3 = 2. 2 is then added as 1 and 1 to the first two groups of 6. The result is a more or less equal distribution of the starting integer.

The following ES6 generator function will perform just that:

Read more of this post

Advertisement

Using ES6 generators for custom iterators

We looked at the basics of using ES6 generators in this post. Generators also come with symbol iterators which can be applied to objects. A symbol iterator helps us implement a custom iterator for our objects. We can declare how our object should behave in a for-of loop. Symbol iterators also involve some weird syntax but generators are weird anyway so a little more weirdness should be fine.

Let’s start off with a simple example:

let partyOrganisation = {
  host: 'John',
  dj: 'Jane',
  food: 'Peter',
  games: 'Mary'
}

Read more of this post

Using ES6 generators in for loops

Generator functions are a new feature in ES6. A generator is a function that can be entered multiple times and each time it will return something else. Generators are definitely strange at first. If you are familiar with the yield keyword in C# and how it is used then you’ll catch up with generators in ES6 quicker than others. I’m not aware of a similar feature in Java. Generators are strongly linked to iterators and arrays as we’ll see in a bit. I believe that Python uses yield as well.

The best thing is if we jump right into it.

Read more of this post

Asynchronous operations using Promises in ES6

The Promise object in ES6 makes asynchronous programming in JavaScript easier. Asynchronous method calls are most useful in case of long-running function calls such as AJAX calls to a backend method. We dispatch a function call and take care of its result when it is done without holding up the main execution thread.

A Promise object accepts two functions: resolve and reject which can be called within the Promise depending on its outcome.

Here’s a basic example of initialising a Promise object:

Read more of this post

Declaring variables in ES6

We don’t use the var keyword in ES6 anymore to declare variables:

var name = 'John'

Instead we have the “let” and “const” keywords for this purpose. With “let” we declare variables whose value can change over time:

Read more of this post

The rest and spread operator in ES6

ES6 comes with a new operator that consists of three dots: … It is strongly related to arrays. It has two major purposes:

Let’s see how the … operator works.

Read more of this post

Enhanced string concatenation/interpolation in ES6

ES6 comes with a new syntactic feature that makes concatenating strings easier. Here’s an example of creating an object with a function with traditional string concatenation in JavaScript:

Read more of this post

Using the forEach array helper in ES6

ES6 comes with a number of helper methods that can be applied to arrays. One of these is called forEach which applies a function on each member of an array. If you’re familiar with C# and LINQ then the forEach array helper is very similar to the ForEach LINQ extension method.

Let’s see an example.

Read more of this post

Fat arrow functions in ES6

ES6 comes with a syntactic enhancement of declaring functions using the => operator. It’s commonly called the fat arrow operator. In fact it’s not only syntactic sugar that saves us a number of characters. Instead, it helps us avoid the “TypeError: Cannot read property ‘propertyName’ of undefined” error.

Here’s a traditional JavaScript function:

Read more of this post

Using the some array helper in ES6

ES6 comes with a number of helper methods that can be applied to arrays. One of these is called some which returns true if at least one element in an array fulfils the specified condition and false otherwise. If you’re familiar with C# and LINQ then the some function is very similar to the Any LINQ extension method.

Let’s see an example.

Read more of this post

Elliot Balynn's Blog

A directory of wonderful thoughts

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

WEB APPLICATION DEVELOPMENT TUTORIALS WITH OPEN-SOURCE PROJECTS

Once Upon a Camayoc

Bite-size insight on Cyber Security for the not too technical.

%d bloggers like this: