简体   繁体   English

如何使我的 function 进入迭代?

[英]How do I make my function into an iteration?

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. function 从数组变量和 function 上面得到它的字,我试图让密码 function 在每一行打印 3 个密码。 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.我已经观看了 Code Geek JS 初学者的迭代视频以实现这一目标,但我尝试在我的代码中实现它,但它没有工作,我不明白如何让它工作。 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.我认为最好上传完整的代码,因为它在这个 state 中工作,这样每个人都可以理解它应该如何 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());

Use a for loop.使用for循环。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM