【发布时间】:2021-11-27 05:26:29
【问题描述】:
JavaScript - Visual Studio 代码 所以,我正在尝试创建一个简单的随机密码生成器,我创建了 4 个不同的数组,它们都有一个函数可以从中生成一个随机元素,我的问题是我不确定如何创建可以调用的第五个函数其他的并让它们与空格放在一起(即 5 只快乐的猫在唱歌)。 我对编程很陌生,所以我不完全理解它,我把我到目前为止的东西贴出来。
//Function to generate and display to console Word 1 - random number
function Word1() {
var random = Math.floor(Math.random() * 9 + 1);
return random
}
console.log(Word1());
// function to generate and display to console Word 2 - random emotion
function Word2() {
var emotions = ['sad', 'happy', 'cheerful', 'angry', 'fear', 'surprise'];
return emotions[Math.floor(Math.random() * emotions.length)];
}
console.log(Word2());
//function to generate and display to console Word 3 - random plural noun
function Word3() {
var emotions = ['computer', 'day', 'car', 'flower', 'house', 'cat'];
var plural = ['s'];
var random = emotions[Math.floor(Math.random() * emotions.length)];
var emotion_plural = random + plural;
return emotion_plural
}
console.log(Word3());
//function to generate and display to console Word 4 - random verb
function Word4() {
var verbs = ['running', 'walking', 'sleeping', 'talking', 'singing', 'sitting'];
var random = verbs[Math.floor(Math.random() * verbs.length)];
return random
}
console.log(Word4());
// function to create password one-line string
function passWord() {
//??
}
console.log(passWord());
【问题讨论】:
-
let pwd = `${Word1()} ${Word2()} ${Word3()} ${Word4()}`?顺便说一句,您没有从Word4返回任何内容此外,您可能应该在Word3中使用Word1的结果,以便您的句子仅在必要时使用复数
标签: javascript arrays function