创建随机电子邮件列表-如何输出列表?

时间:2019-01-30 20:43:30

标签: javascript

我写了一些创建randoom电子邮件地址的功能。 接下来的事情是将它们放在一个组合的列表中,并在每个列表旁边加上一个数字。该列表应为10个项目。 到目前为止,我设法得到的是一个列表,该列表生成相同的电子邮件*长度。我该怎么办?
到目前为止,这是代码:

function createRan(limit) {
  var result = document.querySelector('#test');
  var genEmail = ''
  var num = 0
  var suffix = '@gmail.com'
  var suffix2 = '@hotmail.com'
  var counter = Math.round(getRandomInt(0, 1))
  var address = Math.random().toString(36).substr(2, 8);

  while (num <= limit) {
    if (counter === 0) {
      genEmail = address + suffix
      num++
      result.innerHTML += `${num} ${genEmail} <br></br>`
    } else if (counter === 1) {
      genEmail = address + suffix2
      num++
      result.innerHTML += `${num} ${genEmail} <br></br>`

    }
  }
}

createRan(10)

function getRandomInt(min, max) {
  return Math.random() * (max - min) + min;
}
<pre id="test"></pre>

4 个答案:

答案 0 :(得分:0)

function createRan(limit) {
  var result = document.querySelector('#test');
  var genEmail = ''
  var num = 0
  var suffix = '@gmail.com'
  var suffix2 = '@hotmail.com'

  while (num < limit) {
    let counter = Math.round(getRandomInt(0, 1))
    let address = Math.random().toString(36).substr(2, 8);
    if (counter === 0) {
      genEmail = address + suffix
      num++
      result.innerHTML += `${num} ${genEmail} <br></br>`
    } else if (counter === 1) {
      genEmail = address + suffix2
      num++
      result.innerHTML += `${num} ${genEmail} <br></br>`

    }
  }
}

createRan(10)

function getRandomInt(min, max) {
  return Math.random() * (max - min) + min;
}
<pre id="test"></pre>

答案 1 :(得分:0)

您需要在循环中重新定义counteraddress变量(如@ajxs所写):

function createRan(limit) {
var result = document.getElementById("test");
var genEmail = ''
var num = 0
var suffix = '@gmail.com'
var suffix2 = '@hotmail.com'
var counter
var address

while (num++ <= limit) {
    counter = Math.round(getRandomInt(0, 1))
    address = Math.random().toString(36).substr(2, 8);
    if (counter === 0) {
        genEmail = address + suffix
    } else if (counter === 1) {
        genEmail = address + suffix2
    }
    result.innerHTML += `${num} ${genEmail} <br></br>` 
  }
}
createRan(10)

function getRandomInt(min, max) {
  return Math.random() * (max - min) + min;
}
<pre id="test"></pre>

答案 2 :(得分:0)

只需将地址插入循环中即可。您应该在每次迭代中创建一个新地址。

dtruncated_gamma <- function(x, alpha, theta)
  dtrunc(x, "gamma", shape=shape , scale=scale, a = 25000, b = Inf)
ptruncated_gamma <- function(x, alpha, theta)
  ptrunc(x, "gamma", shape=shape , scale=scale, a = 25000, b = Inf)
fitdist(LargeClaims, "truncated_gamma", method = "mle", start = c(shape = 2, scale = 1000))

答案 3 :(得分:0)

由于您的地址变量不在循环中,因此它会生成同一封电子邮件,因此只声明一次。您也可以使用for循环而不是while循环来简化代码。

function createRan(limit) {

  var result = document.querySelector('#test');
  var genEmail = ''
  var suffix = '@gmail.com'
  var suffix2 = '@hotmail.com'

  for (let i = 1; i <= limit; i++) {
    var address = Math.random().toString(36).substr(2, 8);
    var counter = Math.round(getRandomInt(0, 1));
    if (counter === 0) {
    	genEmail = address + suffix;
    } else if (counter === 1) {
    	genEmail = address + suffix2;
    }
    result.innerHTML += `${i} ${genEmail} <br></br>`
  }
}

createRan(10)

function getRandomInt(min, max) {
  return Math.random() * (max - min) + min;
}
<pre id="test"></pre>