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

Danny Percy
Danny Percy
3,719 Points

Is my code okay? Do.. while loops?

Hey guys! I just finished the 'do, while loops' and I am not sure that this code is good enough? How would you improve it? Thanks for help! :)

var drink;
var food;
var answer=false;

do{
  drink=prompt('What do you want to drink sir?');
  food=prompt('What do you want to eat sir?');
  answer=true;
//If the prompt is empty, than answer stays false
  if (drink===''){
    alert('Type something to drink!');
    answer=false;
  }else if(food===''){
    alert('Type something to eat!');
    answer=false;
  }

}while(!answer)
  document.write('You have ordered ' + drink + ' and ' + food);

1 Answer

Cory Harkins
Cory Harkins
16,500 Points

That's pretty good, now build a more complex program that takes your customers order, and spits back a total price for the goods requested.

Build an array of items and prices, then test the user input against the items in the array to calculate the total. :)

Danny Percy
Danny Percy
3,719 Points

Thanks for the fast reply! Right now I am learning about Arrays.. can't wait to finish the code haha