javascript - 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:该电子邮件无效
我错过了什么吗???
解决方案
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`
}
推荐阅读
- java - 为什么 SwitchCompat 不能在 Android Studio 上运行?
- c++ - 如何根据编译器指令 -D 自动选择 include.h 文件?
- javascript - 如何从元素中删除 EventListener(就像 chrome 一样)
- c - 如何返回一个数组并在 main() 中打印它?
- java - 如何使用自上而下的 Web 服务?
- javascript - 如何在没有时区偏移的情况下即时输出日期/时间
- c# - WebClient 需要 43 秒来下载这个 json 字符串 https://jsonplaceholder.typicode.com/posts
- compilation - 如何消除 Julia 可执行文件中的 JIT 开销(使用 MWE)
- android - 使用 retrofit2 解析失败的 JSON 对象
- android - 颤振:第一次运行我的颤振应用程序时出现 gradle 错误