Home > OS >  What are some simple ways to create a function with other functions
What are some simple ways to create a function with other functions

Time:10-07

JavaScript - Visual Studio Code So, i am trying to create a simple random password generator, I have created 4 different arrays that all have a function to generate a random element out of each, my problem is I'm unsure on how to create a fifth function that can call the others and have them placed on the line with spaces (i.e. 5 happy cats singing). I am very fresh at programming, so I don't fully understand it, ill post what i have so far.

//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'];



}

console.log(Word4());

// function to create password one-line string
function passWord() {


  //??
}

console.log(passWord());

CodePudding user response:

Call all your functions in the password function and concatenate the return values, for instance

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());

CodePudding user response:

Add this in password function:

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

CodePudding user response:

Try this *based on your code

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()}`

}

CodePudding user response:

Try this out

function Word4() {

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

      return verbs[4];
}

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