Without a doubt, JavaScript is one of the most popular programming languages for Web development. Whether you use React, Vue, or Angular, it’s just JavaScript. An extensive and important ecosystem is built around JS, providing countless frameworks and libraries to help you develop your applications faster.

But sometimes it’s best to take a step back and try to figure out how to do things without libraries. Take a look at the code snippets below to solve simple problems in an elegant way and use this knowledge in everyday project situations or to prepare for a coding interview.

1. Reverse the string

In this example, we use the extension operator (…) , Array’s reverse method and String’s join method to reverse a given String.

const reverseString = string= > [...string].reverse().join(' ');

/ / case
reverseString('javascript'); // 'tpircsavaj'
reverseString('good'); // 'doog'
Copy the code

2. Take the factorial of numbers

To compute the factorial of a given number, we use the arrow function and nested ternary operators.

const factoriaOfNumber = number= > number < 0 ? (() = > {
  throw new TypeError('No negative numbers please');
})()
: number <=1 
? 1
: number * factoriaOfNumber(number -1);

/ / case
factoriaOfNumber(4); / / 24
factoriaOfNumber(8); / / 40320
Copy the code

3. Convert numbers into numbers arrays

In this example, we use the extension operator (…) , Array’s map method and parseInt function convert the given number to a singular Array.

const convertToArray = number= > [...`${number}`].map(el= > parseInt(el));

/ / case
convertToArray(5678); // [5, 6, 7, 8]
convertToArray(123456789); // [1, 2, 3, 4, 5, 6, 7, 8, 9]
Copy the code

4. Check if the number is a power of 2

That’s easy. We check that the number is not forged AND use the bitbit AND operator (&) to determine if the number is a power of 2.

const isNumberPowerOfTwo = number= >!!!!! number && (number & (number -1)) = =0;

/ / case
isNumberPowerOfTwo(100); // false
isNumberPowerOfTwo(128); // true
Copy the code

5. Create an array of key-value pairs from the object

In this example, we use the keys method in Object and the Map method in Array to map the keys of Object and create an Array of key/value pairs.

const keyValuePairsToArray = object= > Object.keys(object).map(el= > [el, object[el]]);

/ / case
keyValuePairsToArray({ Better: 4.Programming: 2 });
// [ ['Better', 4], ['Programming', 2] ]
keyValuePairsToArray({ x: 1.y: 2.z: 3 });
// [ ['x', 1], ['y', 2], ['z', 3] ]
Copy the code

6. Return the maximum Number of elements in the array

To return the maximum element from the array, we use an arrow function that gets the array and the number of elements we want the function to return. We use the extension operator (…) And the sort and slice methods in Array. Note that if we do not provide a second argument, the default value of number is 1, so only one maximum element is returned.

const maxElementsFromArray = (array, number = 1) = > [...array].sort((x, y) = > y - x).slice(0, number);

/ / case
maxElementsFromArray([1.2.3.4.5]); / / [5]
maxElementsFromArray([7.8.9.10.10].2); / / [10, 10]
Copy the code

7. Check that all elements in the array are equal

In this short example, we use the every method in Array to check that all elements in the Array are equal. We basically check if each element is equal to the first element in the array.

const elementsAreEqual = array= > array.every(el= > el === array[0]);

/ / case
elementsAreEqual([9.8.7.6.5]); // false
elementsAreEqual([4.4.4.4.4]); // true
Copy the code

8. Return the average of the two numbers

In this example, we use the extension operator (…) And the reduce method in Array to return the average of two given numbers or an Array.

const averageOfTwoNumbers = (. numbers) = > numbers.reduce((accumulator, currentValue) = > accumulator + currentValue, 0) / numbers.length;

/ / caseaverageOfTwoNumbers(... [6.7.8]); / / 7
averageOfTwoNumbers(6.7.8.9); / / 7.5
Copy the code

9. Return the sum of two or more numbers

To return the sum of two or more given numbers or an array, we again use the extension operator (…). And the Reduce method in Array.

const sumOfNumbers = (. array) = > [...array].reduce((accumulator, currentValue) = > accumulator + currentValue, 0);

/ / case
sumOfNumbers(5.6.7.8.9.10); / / 45sumOfNumbers(... [1.2.3.4.5.6.7.8.9.10]); / / 50
Copy the code

10. Returns the power set of the numeric array

In the last example, we return the power set of a numeric array. Therefore, we use the Reduce, Map, and concat methods in Array.

const powersetOfArray = array= > array.reduce((accumulator, currentValue) = > accumulator.concat(accumulator.map(el= > [currentValue].concat(el))), [[]]);

/ / case
powersetOfArray([4.2]); // [[], [4], [2], [2, 4]]
powersetOfArray([1.2.3]); /
/ / [[], [1], [2], [2, 1], [3], [3, 1], [3, 2], [3, 2, 1]]
Copy the code

As you can see, it’s not always difficult to solve these tasks using JavaScript and some ES6 magic.


Source: medium.com/better-prog… “, written by Simon Holdorf, translated by Public account front-end Full Stack Developer