Home > Back-end >  Looking at iteration of a function to generate a random password to a maximum of 3 times
Looking at iteration of a function to generate a random password to a maximum of 3 times

Time:10-08

I am new to Programming and am looking for a nudge or some tips into the right direction with an iteration I am trying to create looping for a random password generator I have created.

I need the function to loop 3 times and I'm aware some of the code is incorrect as I was just trying to see what I could get to work, I'm open to anything you guys throw my way - I will be posting the code below. :)

//Function to generate and display to console Word 1 - random number

function Word1() {

var random = Math.floor(Math.random() * 9   1);

return random

}

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

}

//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

}

//function to generate and display to console Word 4 - random verb

function Word4() {

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

return verbs[Math.floor(Math.random()*verbs.length)];

}

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

return `${Word1()} ${Word2()} ${Word3()} ${Word4()}`

//console.log(passWord());

}

CodePudding user response:

Your code wasnt off to a terrible start for being basic. You had some typos... Javascript variable and function names are case sensitive.

I also broke out some of the variables just to make the process more "readable". Its not overall how I would do everything, but I kept to your initial stab at it.

var Word1 = function() {
  var randomNumber = Math.floor(Math.random() * 9   1);
  return randomNumber
};

var Word2 = function() {
  var emotions = ['sad', 'happy', 'cheerful', 'angry', 'fear', 'surprise'];
  var randomNumber = Math.floor(Math.random() * emotions.length);
  var randomEmotion = emotions[randomNumber];
  return randomEmotion;
};

var Word3 = function() {
  var nouns = ['computer', 'day', 'car', 'flower', 'house', 'cat'];
  var plural = 's';
  var randomNumber = Math.floor(Math.random() * nouns.length);
  var randomNoun = nouns[randomNumber];
  var pluralRandomNoun = randomNoun   plural;
  return pluralRandomNoun;
};

var Word4 = function() {
  var verbs = ['running', 'walking', 'sleeping', 'talking', 'singing', 'sitting'];
  var randomNumber = Math.floor(Math.random() * verbs.length);
  var randomVerb = verbs[randomNumber];
  return randomVerb;
};

var passWord = function() {
  var randomPassword = Word1()   Word2()   Word3()   Word4();
  return randomPassword;
};

for (var i = 0; i < 3; i  ) {
  console.log(passWord());
}

  • Related