首页 > 解决方案 > Javascript更改功能麻烦

问题描述

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

   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:该电子邮件无效

我错过了什么吗???

标签: javascript

解决方案


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

2插入变量 use ${..}, not$()

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

推荐阅读