繁体   English   中英

Javascript更改功能麻烦

[英]Javascript changing function trouble

下面的代码是它最初的样子

   function getRandomEmail() {
        const randomNumber = Math.floor(Math.random() * 200000)+1;
        return `User${randomNumber}@example.com`
    }

我试图在@example.com 前面添加一个单词列表,而不是生成和插入的随机数

像这样

function getRandomEmail() {
var emailname = "testing testing2 testing3".split(" ");
return `User$(Math.floor(Math.random() * emailname.split)+ @example.com`

我现在在运行脚本时得到的输出是

BAD_EMAIL:该电子邮件无效

我错过了什么吗???

1 emailname在初始化中split后已经是一个数组。 现在你需要的是它的length

2插入变量使用${..} ,而不是$()

function getRandomEmail() {
  var emailname = "testing testing2 testing3".split(" ");
  return `User${Math.floor(Math.random() * emailname.length)}@example.com`
}

emailname是一个字符串数组“ testing testing2 testing3 ”。

你不需要再次分裂。


此外,如果您使用反引号 (`),那么您可以使用${variableName}访问变量/常量,而不是通过$(variableName)

 function getRandomEmail() { var emailname = "testing testing2 testing3".split(" "); const randomElement = emailname[Math.floor(Math.random() * emailname.length)]; return `User${randomElement}@example.com`; } console.log(getRandomEmail());

更新


  1. 首先,从数组中生成一个随机索引。

    const randomElementIndex = Math.floor(Math.random() * emailname.length);

  2. 然后找到对应于该索引的元素。

    const randomElement = emailname[randomElementIndex]; ;

  3. 现在,您需要为数组删除该元素。

    emailname.splice(randomElementIndex , 1); // 这将返回一个没有您使用过的元素的数组。

所以现在你的getRandomEmail()可以如下。

 function getRandomEmail(emailName) { //Generating random element const randomElementIndex = Math.floor(Math.random() * emailName.length); // Getting random name based on random element. const randomElement = emailName[randomElementIndex]; // Poping out that random element from an array emailName.splice(randomElementIndex , 1); return `User${randomElement}@example.com`; } var emailName = "testing testing2 testing3".split(" "); console.log(getRandomEmail(emailName));


假设您没有从中获取此名称字符串的数据库。
在这种情况下,您可以将字符串“testing testing2 testing3”保存在localStorage以将该字符串存储在用户的设备上,并在您想要生成新电子邮件时使用它。

所以现在的程序将是

1) 将字符串保存在用户的设备中。
window.localStorage.setItem("names-list-string", "testing testing2 testing3");

2) 现在从localStorage获取字符串并拆分为数组。
var emailName = window.localStorage.getItem("names-list-string").split(" ");

3) 将 emailName 数组传递给getRandomEmail()
getRandomEmail(emailName);

4) 我们必须稍微修改getRandomEmail()
现在,当我们splice()和一个数组时,我们将其转换回字符串并更新 localStorage。
emailName.splice(randomElementIndex , 1);

let emailString = emailName.join(" ");

window.localStorage.setItem("names-list-string", emailString);

我认为就我的知识而言,以下内容是好的。 这是正确的吗 ? 我错过了什么吗?

编辑:未按预期运行。 数组中的同一个“词”被多次使用,并且数组永远不会被清空。

 function getRandomEmail() { var emailname = "test1 test2 test3".split(" "); const randomElement = emailname[Math.floor(Math.random() * emailname.length)]; const index = emailname.indexOf(randomElement); if (index > -1){ emailname.splice(index, 1);} console.log(emailname); if (emailname && emailname.length > 0) { console.log('emailname is not empty.'); return `${randomElement}@example.com`; }else{ console.log('emailname is empty.'); } return ("no mail available"); } console.log(getRandomEmail());

这是一个简单的解决方案,它使用randojs.com来提高可读性。

 function getRandomEmail(){ return rando(["accounting", "sales", "hr"]).value + "@dundermifflin.com"; } console.log(getRandomEmail());
 <script src="https://randojs.com/1.0.0.js"></script>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM