# 你现在可以使用的10个JavaScript代码段 ## 1.反转字符串

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

// 例子
reverseString('javascript'); // 'tpircsavaj'
reverseString('good'); // 'doog'``````

## 2.计算数字的阶乘

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

// 例子
factoriaOfNumber(4); // 24
factoriaOfNumber(8); // 40320``````

## 3.将数字转换为数字数组

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

// 例子
convertToArray(5678); // [5, 6, 7, 8]
convertToArray(123456789); // [1, 2, 3, 4, 5, 6, 7, 8, 9]``````

## 4.检查数字是否为2的幂

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

// 例子
isNumberPowerOfTwo(100); // false
isNumberPowerOfTwo(128); // true``````

## 5.从对象创建键值对数组

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

// 例子
keyValuePairsToArray({ Better: 4, Programming: 2 });
// [ ['Better', 4], ['Programming', 2] ]
keyValuePairsToArray({ x: 1, y: 2, z: 3 });
// [ ['x', 1], ['y', 2], ['z', 3] ]``````

## 6.返回数组中的[Number]个最大元素

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

// 例子
maxElementsFromArray([1,2,3,4,5]); // 
maxElementsFromArray([7,8,9,10,10],2); // [10, 10]``````

## 7.检查数组中的所有元素是否相等

``````const elementsAreEqual = array => array.every(el => el === array);

// 例子
elementsAreEqual([9,8,7,6,5]); // false
elementsAreEqual([4,4,4,4,4]); // true``````

## 8.返回两个数的平均值

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

// 例子
averageOfTwoNumbers(...[6,7,8]); // 7
averageOfTwoNumbers(6,7,8,9); // 7.5``````

## 9.返回两个或多个数字的总和

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

// 例子
sumOfNumbers(5,6,7,8,9.10); // 45
sumOfNumbers(...[1,2,3,4,5,6,7,8,9,10]); // 50``````

## 10.返回数字数组的幂集

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

// 例子
powersetOfArray([4, 2]); // [[], , , [2, 4]]
powersetOfArray([1, 2, 3]); /
// [[], , , [2, 1], , [3, 1], [3, 2], [3, 2, 1]]`````` OA_show(3);

1 条评论 orange

##### 前端Javascript与Nodejs的异同

alwaysVe 阅读 8.4k 58 赞 10 评论

##### 前端计划——JavaScript的Math、数组和字符串方法总结

Alarak 阅读 2.3k 32 赞

##### 推销自己的前端技术书籍

TGCode 阅读 1.7k 10 赞

##### ArcGIS Server SOE 前端JavaScript 调用

Higer 阅读 2.2k 2 赞

### 联系我们

#### 13687733322
{"ft_lang_label":"__label__zh","ft_lang_prob":0.54611045,"math_prob":0.9983794,"size":3245,"snap":"2021-04-2021-17","text_gpt3_token_len":1593,"char_repetition_ratio":0.13421783,"word_repetition_ratio":0.0065146578,"special_character_ratio":0.34453005,"punctuation_ratio":0.33629894,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9850717,"pos_list":[0,1,2,3,4,5,6,7,8,9,10],"im_url_duplicate_count":[null,null,null,null,null,null,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-04-12T06:39:59Z\",\"WARC-Record-ID\":\"<urn:uuid:582417fa-385b-4d73-b307-bd05163a04bf>\",\"Content-Length\":\"86117\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:667759bc-685b-4edc-b5a8-73f3ac8dcc3c>\",\"WARC-Concurrent-To\":\"<urn:uuid:9e165c85-beb3-451a-84e9-d99d0c7b6023>\",\"WARC-IP-Address\":\"123.57.64.232\",\"WARC-Target-URI\":\"http://www.cxybcw.com/199541.html\",\"WARC-Payload-Digest\":\"sha1:QMK6ZYLGTS2J77ZRT5QCPDHHGE7ACEW7\",\"WARC-Block-Digest\":\"sha1:MQW66DRPWMMM7QZOQRBR2GQ7QSD4YT7E\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-17/CC-MAIN-2021-17_segments_1618038066613.21_warc_CC-MAIN-20210412053559-20210412083559-00086.warc.gz\"}"}