Home > Enterprise >  How do I make my function into an iteration?
How do I make my function into an iteration?

Time:09-01

The function gets its word from the array variables and function above it, im trying to make the password function print 3 passwords one on each new line. I've watched Code Geek JS for beginners video on iterations to achieve this but I tried implementing it into my code but it didnt work and i dont understand how to make it work in the first place. I thought it'd be better to upload the full code as its working in this state so everyone can understand how its supposed to function. Thanks guys

// This function generates and displays to the console. Word1 - Random Number
function word1() {
    return Math.floor(Math.random() * 9)   1;  
};


//Function to generate and display to console Word 2 - Random Emotion
var word2 = (function () {
    var verbArray = [];
    verbArray.push("Happy");
    verbArray.push("Sad");
    verbArray.push("Angry");  
    verbArray.push("Cheerful");
    verbArray.push("Ecastatic");
    verbArray.push("Depressed");
    return function() {
      var randomNumber, randomWord;
      randomNumber = Math.floor(Math.random() * verbArray.length);
      randomWord = verbArray[randomNumber];
      return randomWord;
  
    }; 
  })();
 

// Function to generate and display to console Word 3 - Random Plural Noun

  var word3 = (function () {
    var nounArray = [];
    nounArray.push("House");
    nounArray.push("Cat");
    nounArray.push("Dog");  
    nounArray.push("Phone");
    nounArray.push("Computer");
    nounArray.push("Bottle");
    return function() {
      var randomWord, randomNumber;
      randomNumber = Math.floor(Math.random() * nounArray.length);
      randomWord = nounArray[randomNumber];
      return randomWord;
    }; 
})();


// Function to generate and display to console Word 4 - Random Verb
var word4 = (function () {
    var verbArray = [];
    verbArray.push("Running");
    verbArray.push("Jumping");
    verbArray.push("Walking");  
    verbArray.push("Sleeping");
    verbArray.push("Jogging");
    verbArray.push("Typing");
    return function() {
      var randomNumber, randomWord;
      randomNumber = Math.floor(Math.random() * verbArray.length);
      randomWord = verbArray[randomNumber];
      return randomWord;
    }; 
  })();

    

//Function to create password as one-line string
  function passwordfinal() {
    passwordfinal = (word1 ()  " "   word2 ( )   " "   word3 ( )   " "   word4 ( )   " ");
    return passwordfinal

};

console.log(passwordfinal());

CodePudding user response:

Use a for loop.

function passwordfinal() {
    let passwordfinal = ""
    for (let i = 0; i < 3; i  ) {
      passwordfinal  = word1 ()  " "   word2 ( )   " "   word3 ( )   " "   word4 ( )   " ";
    }
    return passwordfinal
};

// This function generates and displays to the console. Word1 - Random Number
function word1() {
  return Math.floor(Math.random() * 9)   1;
};


//Function to generate and display to console Word 2 - Random Emotion
var word2 = (function() {
  var verbArray = [];
  verbArray.push("Happy");
  verbArray.push("Sad");
  verbArray.push("Angry");
  verbArray.push("Cheerful");
  verbArray.push("Ecastatic");
  verbArray.push("Depressed");
  return function() {
    var randomNumber, randomWord;
    randomNumber = Math.floor(Math.random() * verbArray.length);
    randomWord = verbArray[randomNumber];
    return randomWord;

  };
})();


// Function to generate and display to console Word 3 - Random Plural Noun

var word3 = (function() {
  var nounArray = [];
  nounArray.push("House");
  nounArray.push("Cat");
  nounArray.push("Dog");
  nounArray.push("Phone");
  nounArray.push("Computer");
  nounArray.push("Bottle");
  return function() {
    var randomWord, randomNumber;
    randomNumber = Math.floor(Math.random() * nounArray.length);
    randomWord = nounArray[randomNumber];
    return randomWord;
  };
})();


// Function to generate and display to console Word 4 - Random Verb
var word4 = (function() {
  var verbArray = [];
  verbArray.push("Running");
  verbArray.push("Jumping");
  verbArray.push("Walking");
  verbArray.push("Sleeping");
  verbArray.push("Jogging");
  verbArray.push("Typing");
  return function() {
    var randomNumber, randomWord;
    randomNumber = Math.floor(Math.random() * verbArray.length);
    randomWord = verbArray[randomNumber];
    return randomWord;
  };
})();



//Function to create password as one-line string
function passwordfinal() {
  let passwordfinal = ""
  for (let i = 0; i < 3; i  ) {
    passwordfinal  = word1()   " "   word2()   " "   word3()   " "   word4()   " "   "\n";
  }
  return passwordfinal
};

console.log(passwordfinal());

  • Related