Jump to content

How do I Loop multiple functions using the for loop in JavaScript

Go to solution Solved by PurpleCodes,
for(let i = 0; i < 10; i++){
    console.log( Word1() + " " + Word2() + " " + Word3() + " " + Word4());
}

If you want to run the log 10 times you would do something like this. Anything you put inside a for loop will run as many times as the for loop runs.

So I have 4 functions all of them are purely functions to grab words or numbers randomly and creating a random password. this is for a course I'm doing and i remember my adviser telling me it was as simple as changing the console.log line on 25 but I can remember and I can ask him till tomorrow and its just eating at me knowing it could be so simple. I need it to loop 3 times 

 

<

//function to generate and display to console Word 1 - random number
function Word1() {
    return Math.floor(Math.random() * 9 + 1);
}
 
//function to generate and display to console Word 2 - random emotion
function Word2() {
let emotions = ['Happy', 'Confused', 'Scared', 'Sad', 'Angry', 'Surprise'];
return emotions[Math.floor(Math.random()*emotions.length)];
}
 
//function to generate and display to console Word 3 - random plural noun
function Word3() {
let Nouns = ['Cat', 'Buse', 'Photo', 'Potatoe'];
return Nouns[Math.floor(Math.random()*Nouns.length)] + "s";
}
 
//function to generate and display to console Word 4 - random verb
function Word4() {
let verbs = ['Tolerate', 'Surprise', 'Sleeping', 'Jumping', 'Approach', 'Identify'];
return verbs[Math.floor(Math.random()*verbs.length)]
}
 
console.log( Word1() + " " + Word2() + " " + Word3() + " " + Word4());

 

Link to post
Share on other sites

for(let i = 0; i < 10; i++){
    console.log( Word1() + " " + Word2() + " " + Word3() + " " + Word4());
}

If you want to run the log 10 times you would do something like this. Anything you put inside a for loop will run as many times as the for loop runs.

Link to post
Share on other sites

Unless there's some other restriction, why are you adding an "s" to nouns to try to make them plural instead of just making an array of plural nouns to begin with? Never mind that "Buse" and "Potatoe" are wrong.

 

Anyway, to repeat a section, you need to wrap it in a for loop. You know the name of the thing you want, a for loop, but you don't know how to use it. When you get into this situation, that's the perfect time to look up documentation. MDN Web Docs are a great place to learn about JavaScript. Here's the page on for loops.

¯\_(ツ)_/¯

 

 

Desktop:

Intel Core i7-11700K | Noctua NH-D15S chromax.black | ASUS ROG Strix Z590-E Gaming WiFi  | 32 GB G.SKILL TridentZ 3200 MHz | ASUS TUF Gaming RTX 3080 | 1TB Samsung 980 Pro M.2 PCIe 4.0 SSD | 2TB WD Blue M.2 SATA SSD | Seasonic Focus GX-850 Fractal Design Meshify C Windows 10 Pro

 

Laptop:

HP Omen 15 | AMD Ryzen 7 5800H | 16 GB 3200 MHz | Nvidia RTX 3060 | 1 TB WD Black PCIe 3.0 SSD | 512 GB Micron PCIe 3.0 SSD | Windows 11

Link to post
Share on other sites

6 minutes ago, PurpleCodes said:
for(let i = 0; i < 10; i++){
    console.log( Word1() + " " + Word2() + " " + Word3() + " " + Word4());
}

If you want to run the log 10 times you would do something like this. Anything you put inside a for loop will run as many times as the for loop runs.

Thanks so much mate. I had that for loop code written similar but i just wasnt getting it right and it was erroring out

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×