Image for post
Image for post

All software engineers should be familiar with design patterns. One of the most known and common one is the singleton pattern. If you want to understand this pattern and how to use it with Node.JS read this quick and easy tutorial.

A bit of theory

We need singleton when we want to make sure there is only one object instantiated. Therefore, instead of creating a new object we need to ensure the constructor was called only once and then we reuse the instance.

We can achieve this by refactoring our class to have:

  • hidden (private)
  • public method that returns instance of the class

That’s the theory part done. …


A solution to the popular interview question

Image for post
Image for post

In this article we are going to check a few techniques how to make the first letter of a string Uppercase. This may be a popular interview question so let’s deep dive into many ways of achieving the same result.

First, let’s look at a few test strings together with expected results:

'here is a string'   ==> 'Here is a string'
'the New York Times' ==> 'The New York Times'
'...continued' ==> '...continued'

Simple yet working

One way to uppercase the first letter is basically to… uppercase the first letter of a string:

const s = 'here is a string';
const result = s[0].toUpperCase() …


Here’s why rounding numbers in JavaScript is still a problem

Image for post
Image for post
Photo by Nick Hillier on Unsplash.

It is 2020 (or maybe) and rounding numbers is still a problem in JavaScript. Actually, it’s a problem in computing in general.

Here is a definitive guide on how to do this properly using JavaScript. Read through once and remember forever!

Introduction

First of all, let’s understand why floating numbers have been, are, and will continue to be a problem in computing.

Each number is a sequence of bytes. For example, the decimal number is binary . That’s basically how computers represent each number — with bytes (i.e. one byte is 8 bits: or). If we take eight bytes, is equivalent to in a decimal system. is equivalent to . Each byte has a weight. Starting from the left, it goes ,, , , , , , . After adding this together, we get . …


How refactoring of obtuse value can make our code cleaner and easier to understand.

Image for post
Image for post

What’s actually a magic number? Basically, magic number is a code, cipher or number that has some “magic”, obscure but important meaning. However, just from reading the source code it’s not clear what’s the meaning.

It’s like finding the Answer to the Ultimate Question of Life, the Universe, and Everything. The answer is: 42. Why 42? ¯\_(ツ)_/¯

Let’s go back to computer programming and imagine we get some back:

const response = await API.request();if (response.code === 5) {
// DO SOMETHING
}

Here we can see a magic number. means something but looking at the source code we can’t really tell what’s the secret behind that particular value. …


Explained and done right!

Image for post
Image for post

Quick introduction

Latin alphabet is widely used across the World. Its base for many European languages, including English. In fact, English alphabet basically it’s the same as ISO basic Latin alphabet and consists of 26 letters: .

However, all other non-English, Latin based alphabets will have some special letters. …


Image for post
Image for post

When working with strings in JavaScript we may encounter a task to substitute all occurrences of a string with another one.

Let’s create a very simple test case that will illustrate the problem. First, let’s define a and our goal will be to replace all strings with :

const testString = 'abc foo def foo xyz foo';

The simple and naive way would be to try something like function:

const result = testString.replace('foo', 'bar')

Once we run this we get only the first occurrences replaced and the result will be:

abc bar def foo xyz foo

Definitely, not what we wanted! …


Image for post
Image for post

While Number and parseInt may look similarly there are a few major differences that may lead to confusion and bugs. In this article we will go through the differences and explain them one by one.

First of all, let’s note that parseInt is used to parse string values into integers with given radix. The second parameter is optional. However, it’s a bad practice not to use it as this leads to errors. For example, if we pass as hexadecimal value () which is prefixed with it will be correctly autodetected and parsed. Yet, octal number which are prefixed with caused confusion and problems as many programmers assumed will be ignored and the string will be parsed as decimal. However, since ECMAScript 5 auto-detection of octal literals was dropped which… leads to confusion if you were aware that value with leading will be processed as octal! …


Image for post
Image for post

In this tutorial we are going to enable binary support in API Gateway using AWS console. This task may not sound too complicated yet looking at number of related questions on Stack Overflow [1] [2] [3] and comments like:

I’ve been struggling to do the same for days, but couldn’t find any documentation to back up that it’s possible

it’s not that easy and the process is confusing.

Moreover, in this tutorial we are not going to use CONVERT_TO_BINARY. It’s not necessary. We are going to use as little changes as possible to get binary media types to work.

First we are going to create a lambda function and then we are going to go through step by step API Gateway configuration in order to get an image returned correctly. …


Image for post
Image for post

Joining two arrays may sound like an easy task yet looking, for example, at this StackOverflow question and the number of answers (let’s note that none was accepted up to this day!) it may cause a bit of headache.

While we can use external libraries to join arrays, we can simply rely on plain JavaScript. In this article we are going to look how to do it with plain JS and also with the newer ES6 syntax.

First of all let’s define the problem.

We have two arrays and . We want to join them together into one, single array with all the elements present:

const abc = ['a', 'b', 'c'];
const def = ['d', 'e', 'f'];
// expected result: ['a', 'b', 'c', 'd', 'e'…

Image for post
Image for post

Standard way of creating functions and passing parameters is simply to declare a function name and list parameters in the bracket:

function foo(param) {
// do something
}

This works well and there is absolutely nothing wrong with such a function.

However, when we pass more parameters to our function and/or we start adding optional parameters, things can become awkward to use:

function bar (param1, param2, param3, optionalParam1 = true) {
// do something
}

First of all we have to remember the order of parameters. Obviously, all modern days IDEs will help us a bit. …

About

Arek Jaworski

Programming, project management and politics

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store