js实现随机生成4位数验证码


1.js实现随机生成4位数验证码

  const randomString = () => Math.random().toString(36).slice(2,6);

  randomString();

  //toString() 括号里的内容代表进制

2.翻转字符串

const reverse = str => str.split('').reverse().join('');

reverse('hello world');   // 'dlrow olleh'

3.数组去重

  const removeDuplicates = (arr) => [...new Set(arr)];

  console.log(removeDuplicates([1, 2, 2, 3, 3, 4, 4, 5, 5, 6]));

4.合并数组

  const merge = (a, b) => a.concat(b);

  const merge = (a, b) => [...a, ...b];

5.获得一组数的平均值

const average = (...args) => args.reduce((a, b) => a + b) / args.length;

average(1, 2, 3, 4, 5);   // 3

6. 将RGB转化为十六机制

const rgbToHex = (r, g, b) => "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);

rgbToHex(255, 255, 255);  // '#ffffff'

7. 滚动到页面顶部

const goToTop = () => window.scrollTo(0, 0);

goToTop();

转载 参考 https://juejin.cn/post/7025771605422768159