Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops `do ... while` Loops

how do i create a do while loop with multiple inputs

create a do while loop that builds strings with multiple inputs from user , just awsnering yes or no. .like example: do you want to play? and user inputs yes creates a loop until he awsners no

2 Answers

Steven Parker
Steven Parker
229,732 Points

I'm not sure what you mean by "builds strings". It sounds like what you want is very similar to the "guess" game in this video, but even easier since you don't need to convert the answer to a number or keep track of a count:

do {
  answer = prompt("Do you want to play?");
} while (answer.toLowerCase() != "no");

But this might be rather annoying to a user, since the question implies a choice but there really isn't one.

Or did you mean you wanted to put this around the game, so the user can play over and over if they want? In that case, a normal "while" might do a better job:

while (prompt("Do you want to play?").toLowerCase() != "no") {
  // game code would go in here
}

i want the user when asked if they want to play. they would awnser yes and enter A word no matter what word, then they would be prompt again if they want to play again and so on until they awsner no and then i output what they entered all togther.

Steven Parker
Steven Parker
229,732 Points

Would someone understand what was happening? But it's also pretty easy. Just use the concepts shown here and in the video for creating the loop, and then use concatenation inside to build the string which you display after the loop ends.

no, its for self purpose instead of working with numbers. i thought working with inputs from users and make an do while loop. because i dont see that anywhere. im still learning and understanding JavaScript. i have a passion for coding and technology, that my goal is to become a software engineer or full stack web developer someday. sorry back to topic.

var User= prompt("do you want to play");

if (User == "yes") { prompt("Enter a word") }

this is what i had before i was completely confused, i havent messed with .toLowercase yet.

Steven Parker
Steven Parker
229,732 Points

Using 'toLowerCase" just allows the answer to be accepted even if it's typed differently. It's optional.

In your code, remember to assign some variable when you do the 2nd "prompt" if you want to use the answer. Then it sounds like you want to concatenate that answer onto another variable to accumulate words. And don't forget to put it all inside a loop.

And when posting code, use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. :arrow_heading_down:   Or watch this video on code formatting.

ok, seems like i kinda get it. so how would i start the next prompt if i want to ask "do you want to play again?" after they entered a word. do i need to need to go to "else. man im so confused. so here is what im asking the user prompt: 'do you want to play?' > 'yes' > prompt: 'enter a word' > 'dog' prompt: 'do you want to play again?' 'yes' > prompt: 'enter a word.' > 'cat' > prompt: 'do you want to play again?' > 'no' output: 'dog cat'

Steven Parker
Steven Parker
229,732 Points

I'm not sure what you mean by "need to go to else", but I understand what you want the program to do. Give it a try. Be sure to include a loop, and have every "prompt" assign to a variable. Then post it with proper formatting if you still have trouble.