Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

Clinton Hopgood
Clinton Hopgood
7,825 Points

Loop with user input

I'm trying to create a loop that waits for user input before each turn.

I would like the i to increase by 1 each turn up to a maximum of 9.

This is how my code looks without the loop.

<code> var turnByTurn = function() { i = 1; // loop turns if (i%2 == 0) { console.log("even"); turn1(); } else { console.log("odd") turn2(); } }; </code>

1 Answer

Clinton Hopgood
Clinton Hopgood
7,825 Points

I moved i outside of the function and added i++ to the function. This has solved the problem, however I'm interested to know if I can add user input to a loop.