【问题标题】:What are some simple ways to create a function with other functions用其他函数创建函数有哪些简单的方法
【发布时间】: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


【解决方案1】:

在密码功能中添加:

    const part1 = word1()
    const part2 = word2()
    const part3 = word3()
    const part4 = word4()
    
    return part1 + part2 + part3 + part4

【讨论】:

  • 谢谢,这很有效,让我对我能做什么有了更多的了解。
【解决方案2】:

试试这个

function Word4() {

      var verbs = ['running', 'walking', 'sleeping', 'talking', 'singing', 'sitting'];

      return verbs[4];
}

function passWord() {
        return  Word1() + ' ' + Word2() + ' ' + Word3() + ' ' + Word4();
}

【讨论】:

    【解决方案3】:

    例如,在 password 函数中调用所有函数并连接返回值

    function random(...array) {
      return array[array.length * Math.random() | 0];
    }
    
    function word1() {
      return Math.floor(Math.random() * 9 + 1);
    }
    
    function word2() {
      return random('sad', 'happy', 'cheerful', 'angry', 'fear', 'surprise');
    }
    
    function word3() {
      const plural = 's';
      return random('computer', 'day', 'car', 'flower', 'house', 'cat') + plural;
    }
    
    function word4() {
      return random('running', 'walking', 'sleeping', 'talking', 'singing', 'sitting');
    }
    
    function password() {
      return [word1(), word2(), word3(), word4()].join(' ');
    }
    
    console.log(password());

    【讨论】:

    • 如果你总是加复数 s ,为什么不把它加到单词中呢?最好只根据word1的结果添加复数
    【解决方案4】:

    *根据您的代码试试这个

    function Word4() {
    
      var verbs = ['running', 'walking', 'sleeping', 'talking', 'singing', 'sitting'];
    
      return verbs[Math.floor(Math.random() * verbs.length)];
    
    }
    
    console.log(Word4());
    
    // function to create password one-line string
    function passWord() {
    
        return `${Word1()} ${Word2()} ${Word3()} ${Word4()}`
    
    }
    

    【讨论】:

    • 这也行得通,我对标识符没有做太多,所以这对我有所启发。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-26
    • 2015-01-22
    • 1970-01-01
    • 2015-07-17
    • 2018-12-11
    • 2011-02-13
    • 2012-11-02
    相关资源
    最近更新 更多