1. Reverse the string

Using extension notation… Parse strings into arrays.

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

reverseString('Medium'); // 'muideM'
Copy the code

2. Number of factorial

Compute the factorial of the data using the arrow function and the ternary operator.

const factorialOfNumber = number= > 
  number < 0
    ? (() = > {
      throw new TypeError('No negative numbers please');
    })()
    : number <= 1
      ? 1
      : number * factorialOfNumber(number - 1);
      
factorialOfNumber(4); / / 24
Copy the code

3. Integer to array

Using extension notation… And combined with the map method.

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

convertToArray(5678); // [5, 6, 7, 8]
Copy the code

Note that ${number} is used instead of number

4. Check whether it is a power of 2

This is straightforward, with clever use of the ampersand operator.

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

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

5. Create an array of key/value pairs for level 1 objects

This example just creates an array for a first-level object. This array is two-dimensional and stores the key-value pairs of the converted object.

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

keyValuePairsToArray({ Better: 4.Programming: 2});
// [['Better', 4], ['Programming', 2]]
Copy the code

6. Return the maximum value in the numeric array

We define a function that takes an array of numbers to pass and an array length to return. Of course, the same idea applies to returning the smallest value in a numeric array.

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

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

7. Check whether the elements in the array are the same

The idea is to compare the second starting element of the array to the first element one by one, using the === symbol.

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

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

8. Calculate the average

We use the reduce function to process the array and average it.

This is also the way to calculate the sum of numbers

const averageOfNumbers = (. numbers) = > numbers.reduce((accumulator, currentValue) = > accumulator + currentValue, 0) / numbers.length; averageOfNumbers(... [6.7.8]); / / 7
averageOfNumbers(6.7.8.9); / / 7.5
Copy the code

🤣 Note: the above code is not rigorous, does not take into account boundary values and other small problems, interested in their own extension, encapsulated into util method, after all, in the actual development can still be used ~

The latter

  • First article: github.com/reng99/blog…

  • Dev /10-javascri…

  • More: github.com/reng99/blog…

My blog will be synchronized to OSCHINA community, this is my OSCHINA ID: reng99, invite you to join us: www.oschina.net/sharing-pla…