Fe-interview: [js] 第74天 写一个方法随机生成指定位数的字符串

Created on 28 Jun 2019  ·  10Comments  ·  Source: haizlin/fe-interview

第74天 写一个方法随机生成指定位数的字符串

js

Most helpful comment

function getRandomString (length) {
  let str = Math.random().toString(36).substr(2)
  if (str.length >= length) {
    return str.substr(0, length)
  }
  str += getRandomString(length - str.length)
  return str
}

All 10 comments

function getStringBylength(nlength) {
var str = "";
var arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
];
for (var i = 0; i < nlength; i++) {
pos = Math.round(Math.random() * (arr.length - 1));
str += arr[pos];
}
return str
}

function getRandomString (length) {
  let str = Math.random().toString(36).substr(2)
  if (str.length >= length) {
    return str.substr(0, length)
  }
  str += getRandomString(length - str.length)
  return str
}
function str (length = 0) {
    let arr = new Array()
    const push = () => {
        arr.push(Math.floor(Math.random()*10))
        return (arr.length === length) ? arr.join('') : push()
    }
    return push()
}
console.log(str(5))
function str (length = 0) {
  let arr = new Array()
  const push = () => {
      arr.push(Math.floor(Math.random()*10))
      return (arr.length === length) ? arr.join('') : push()
  }
  return push()
}
console.log(str(5))

我这个方法不好,很容易Maximum call stack size exceeded

```javascript function randomStr(count) { let len = parseInt(count/10) || 1 return [...new Array(len).fill(len)].reduce((total, str) =>${total}${Math.random().toString(36).substr(2)}`, '').substring(0, count)
}

const randomStr = (length = 6) => {
  return Math.random().toString(36).slice(-length)
}

这个可以生成 0 - 12 位的随机字符串

const createRandomString = () =>
  Math.random()
    .toString(36)
    .substr(2);

const getRandomString = (len) => {
  let randomStr = "";
  while (randomStr.length < len) {
    randomStr += createRandomString();
  }
  return randomStr.slice(randomStr.length - len);
};

console.log(getRandomString(2));
console.log(getRandomString(4));
console.log(getRandomString(6));
console.log(getRandomString(12));
console.log(getRandomString(30));
function randomStr(num) {
  const dict = [[65, 90], [97, 122]];
  function randomRange(begin, end) {
    return Math.floor(Math.random() * (end-begin+1) + begin)
  }
  return Array(num).fill(null).map(() => {
    const [begin, end] = dict[randomRange(0, 1)];
    return String.fromCharCode(randomRange(begin, end));
  }).join("");
}

/* 添加了一个随机字母大小写的功能 */
function randomCase (str) {
if (typeof str !== 'string') {
throw new TypeError('需要传入一个字符串')
}
const tempArr = []
for (let w of str) {
if (/\D/.test(w) && Math.random() * 10 < 5) {
tempArr.push(w.toUpperCase())
} else {
tempArr.push(w)
}
}
return tempArr.join('')
}

function randomStr (length = 10, useRandomCase = false) {
  const exception = [+Infinity, -Infinity]
  if (typeof length !== 'number' || Number.isNaN(length) || exception.includes(length)) {
    throw new TypeError('随机字符串的长度必须是一个准确的数字')
  }
  if (typeof useRandomCase !== 'boolean') {
    throw new TypeError('请正确设置是否随机大小写(需要传入布尔值)')
  }
  let str = Math.random().toString(36).slice(2)
  if (length <= 10) {
    str = str.slice(0, length)
  } else {
    str += randomStr(length - 10)
  }
  if (useRandomCase) {
    str = randomCase(str)
  }
  return str
}
function getRandomStr(count) {
  return Array.from(new Array(count)).map(() => {
    return String.fromCharCode(Math.floor(Math.random() * 26) + 97);
  }).join('');
}
console.log(getRandomStr(10));

Was this page helpful?
0 / 5 - 0 ratings