1.每个单词手字母大写
var capitalizeEveryWord = str => str.replace(/\b[a-z]/g, char => char.toUpperCase()); capitalizeEveryWord(‘hello world‘);
2.计算平均数
var calculatedMean = arr => arr.reduce((total, number) => total + number, 0) / arr.length; calculatedMean([1, 2, 3])
3.驼峰命名
const camelCase = str => str.replace(/-(\w)/g, (char1, char2) => char2.toUpperCase());
console.log(camelCase(‘hello-world‘));
4.计算数字在数组中出现次数
const numberOfReplications = (arr, num) => arr.reduce((m, n) => num == n ? m+1 : m+0, 0);
numberOfReplications([1, 2, 3, 1], 1);
5.解析URL参数
const getQueryString = (name) =>{ const reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)"); const r = window.location.search.substr(1).match(reg); if(r) return decodeURI(r[2]); return null; }
6.求数组中最大值
const maximum = arr => Math.max(...arr);
maximum([1,2,10]);
7.求数组中最小值
const minimum = arr => Math.min(...arr);
minimum([1,2,3,4]);
请大家多多指出不足。。。