Michael Bryan

Michael Bryan


Factorial Algorithm: ES5 & ES6 JavaScript Solutions

The factorial algorithm is one of the common whiteboard algorithms that you can come across.

Google's definition of factorial:

The product of an integer and all the integers below it; e.g. factorial four ( 4! ) is equal to 24.

That is:

0! = 1
1! = 1
2! = 2 * 1
3! = 3 * 2 * 1
4! = 4 * 3 * 2 * 1
5! = 5 * 4 * 3 * 2 * 1

ES5: While method

function factorial(integer) {
  final = 1
  while (integer > 0) {
    final *= integer
    integer = integer - 1
  return final

Let's break down the code:

function factorial(integer) {
  final = 1
  while (integer > 0) {
    final *= integer

We start with assigning an initial value of 1 to variable final. Inside of the later used while method we will be able to assign the same value as an integer variable by multiplying final variable by the integer argument variable. By doing this we make sure we will start with multiplying the highest possible number by the next smallest possible number.

  while (integer > 0) {

Then we run a while method where the condition is that it will loop as long as the value of integer is greater than zero.

    final *= integer
    integer = integer - 1

Inside of the while loop we multiply the variable final by the integervalue. The integer variable has a new value assigned in every loop execution.

    integer = integer - 1
  return final

At some point, the value of the integer variable will be 0 (because we deduct 1 from the integer value in every loop execution - integer = integer - 1), because of that the while loop will stop executing and the value of final variable will be the factorial of the value of integerargument variable we passed into the function.

Let's see an example with integer variable having a value of 3.

integer = 3;
function factorial(integer) {
  final = 1
  while (integer > 0) {
    final *= integer // 1st loop: 1 * 3 = 3 | 2nd loop: 3 * 2 = 6 | 3rd loop: 6 * 1 = 6
    integer = integer - 1 // 1st loop: 3 - 1 = 2 | 2nd loop: 2 - 1 = 1 | 3rd loop: 1 - 1 = 0
  return final // 6 because of the 3rd loop of final *= integer = 6 * 1

ES6: Arrow Loop Function with Conditional (ternary) operator

const factorial = integer => integer ? (integer * factorial(integer - 1)) : 1;

Here we have a one-line arrow function that takes integer as argument variable. Inside of the function is shorthand if else statement.

/*const factorial = integer =>*/ integer ? /*(integer * factorial(integer - 1)) */ : 1;

The condition of the If Else statement is if integer == true. That must be (in our case) any positive number. And by now we know that if the condition is false it will return value of 1.

/* const factorial = integer => integer ?*/ (integer * factorial(integer - 1)) /* : 1; */

Here comes the fun part. If the condition is evaluated as true, we will multiply the integer variable by the result of the factorial function itself with the integer value being deducted by 1 (integer - 1).

Now we see that factorial function is a loop function that calls itself as long as the value of integer(that is being deducted by 1 with every loop) is greater than 0.

/* const factorial = integer =>*/ integer ? (integer * /*factorial*/(integer - 1))  : 1;

Once the statement evaluation of the integer ? becomes false (in our case 1 - 1 = 0) it will multiply the last assigned value of integer by 1 (: 1) and stops the loop.

Let's see an example with integer variable having a value of 3.

integer = 3;
const factorial = integer =>
integer ? // 1st loop: 3 == true  | 2nd loop: 2 == true | 3rd loop: 1 == true | 4th loop: 0 == false
(integer * factorial(integer - 1)) // 1st loop: 3 * 2 | 2nd loop: 6 * 1
: 1; // 6 * 1 and stops the loop
factorial(integer); // 6

Hope you enjoyed this algorithm code breakdown and let me know if you have any other favourite solution (there's many off course).

By : Jan Sysala


What is GEEK

Buddha Community

Explanation of Quick Sort

In this article, I will be talking about quick sort & all the components that you need to know.

Quicksort is nothing but another algorithm to sort a list of items in ascending or descending order. To understand quick sort, we need to understand the following items:

  • pivot
  • how partition works
  • recursion within quick sort & the base case

Let’s start with pivot. What is pivot then?

Pivot is the element that will be used to divide the list of values less than pivot to the left(or the right, if we are sorting it the other direction) and values greater than the pivot to the right of the pivot.

Let’s say we have the following:

/* Example array: 4 1 5 2 10 8 6 3 */

	const array = [4,1,5,2,10,8,6,3];

	//the first element,4, or the last element,3, is commonly used as a pivot in quick sort. 

	//after partition:
view raw
pivot.js hosted with ❤ by GitHub

If 3 is chosen as the pivot, it will always land at where it should be left after a partition. In this case, that would be the 2nd index of the array.

To further demonstrate how that would work, view the following illustration:

Image for post

On the illustration above, we have i, j, and the pivot. 4 is used as the pivot. i, initially, is set to -1. This indicates the number of elements we have that are less than the pivot. j, on the other hand, is used as an index to traverse through every element.

In partition, for every element j, if the pivot is great than arr[j], increment i and swap arr[i] and arr[j]. This will help put whatever element less than pivot on the left side of the array. J will increment regardless.

After the whole array is done, i equates to the number of elements that are less the pivot. Increment i by 1 and swap arr[i] with the pivot. Now the pivot is truly the dividing point between elements greater than it to the right and elements lesser than it to the left.

Afterward, quicksort will be called on the subarray to the left of the pivot and also to the right of the pivot. This process will repeat until there are no more subarrays to be sorted.

#sorting-algorithms #es6 #algorithms #javascript #web-development #algorithms

Rahul Jangid


What is JavaScript - Stackfindover - Blog

Who invented JavaScript, how it works, as we have given information about Programming language in our previous article ( What is PHP ), but today we will talk about what is JavaScript, why JavaScript is used The Answers to all such questions and much other information about JavaScript, you are going to get here today. Hope this information will work for you.

Who invented JavaScript?

JavaScript language was invented by Brendan Eich in 1995. JavaScript is inspired by Java Programming Language. The first name of JavaScript was Mocha which was named by Marc Andreessen, Marc Andreessen is the founder of Netscape and in the same year Mocha was renamed LiveScript, and later in December 1995, it was renamed JavaScript which is still in trend.

What is JavaScript?

JavaScript is a client-side scripting language used with HTML (Hypertext Markup Language). JavaScript is an Interpreted / Oriented language called JS in programming language JavaScript code can be run on any normal web browser. To run the code of JavaScript, we have to enable JavaScript of Web Browser. But some web browsers already have JavaScript enabled.

Today almost all websites are using it as web technology, mind is that there is maximum scope in JavaScript in the coming time, so if you want to become a programmer, then you can be very beneficial to learn JavaScript.

JavaScript Hello World Program

In JavaScript, ‘document.write‘ is used to represent a string on a browser.

<script type="text/javascript">
	document.write("Hello World!");

How to comment JavaScript code?

  • For single line comment in JavaScript we have to use // (double slashes)
  • For multiple line comments we have to use / * – – * /
<script type="text/javascript">

//single line comment

/* document.write("Hello"); */


Advantages and Disadvantages of JavaScript

#javascript #javascript code #javascript hello world #what is javascript #who invented javascript

Hire Dedicated JavaScript Developers -Hire JavaScript Developers

It is said that a digital resource a business has must be interactive in nature, so the website or the business app should be interactive. How do you make the app interactive? With the use of JavaScript.

Does your business need an interactive website or app?

Hire Dedicated JavaScript Developer from WebClues Infotech as the developer we offer is highly skilled and expert in what they do. Our developers are collaborative in nature and work with complete transparency with the customers.

The technology used to develop the overall app by the developers from WebClues Infotech is at par with the latest available technology.

Get your business app with JavaScript

For more inquiry click here https://bit.ly/31eZyDZ

Book Free Interview: https://bit.ly/3dDShFg

#hire dedicated javascript developers #hire javascript developers #top javascript developers for hire #hire javascript developer #hire a freelancer for javascript developer #hire the best javascript developers

Niraj Kafle


The essential JavaScript concepts that you should understand

As a JavaScript developer of any level, you need to understand its foundational concepts and some of the new ideas that help us developing code. In this article, we are going to review 16 basic concepts. So without further ado, let’s get to it.

#javascript-interview #javascript-development #javascript-fundamental #javascript #javascript-tips

Madelyn  Frami

Madelyn Frami


ES6 JavaScript— “this” keyword

Try to summarize the usage of “this” with examples

  • Every function created with the function keyword or with the method shorthand has its own “this”, and it generally binds to “who” called it.
  • Arrow functions don’t bind “this” to anything, the “this” inside depends on the surrounding context

Example 1

“handler” declared by error function here, that means the “this” is nothing, just same as outside the handler, thus the output is Window object

const handler = () => {
btn1.addEventListener('click', handler);


Window {parent: Window, opener: null, top: Window, length: 0, frames: Window, …}

#es6-in-depth #self-learning #javascript-tips #es6-js #self-taught #javascript