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

How can I improve it? (Beginner food script)

Hey guys! Just finished my first food java script. How can I imporve it? Or is it good? In my opinion there are to much repeat? Thanks for help! :)

var drink;
var food;
var drinkCost=0; // In Dollars
var foodCost=0; // In Dollars
var foodList=['pizza' , 'burger']; //2 types of food what our kitchen have
var drinkList=['cola' , 'water']; //2 type of drink that our kitchen have
var answer=false;
var foodPriceList=[10,5];
var drinkPriceList=[3,2];

function finalPrice(){
  var final=drinkCost + foodCost;
  return final;
}

//Start with the message
var msg=alert('Welcome to our kitchen! Today we got: ' + foodList[0] + ' which cost ' + foodPriceList[0] + ' $ ');
msg+=alert('And also we got ' + foodList[1] + ' which cost ' + foodPriceList[1] + ' $ ');

//Script (Loop with do-while)
do{

  //Food Section -> Asking which food + how many would it cost
  var food=prompt('Please type your food that would like to order');

  //Add value to Cost
    if (food.toLowerCase()===foodList[0]){
      foodCost+=foodPriceList[0];
    }else if(food.toLowerCase()===foodList[1]){
      foodCost+=foodPriceList[1];
  }

  //What if user didn't typed anything?
    if (food.toLowerCase()===''){
      alert("It seems that you typed something wrong. Please try again!");
      answer=false;
      break;
    }

  //Check the food if it is available
    if (food.toLowerCase()===foodList[0]){
      answer=true;
      alert('You have added ' + food + ' to your chart. That would you cost: ' +foodCost + ' $');
    }else if (food.toLowerCase()===foodList[1]){
      answer=true;
      alert('You have added ' + food + ' to your chart. That would you cost: ' +foodCost + ' $');
    }else if (food.toLowerCase()!==foodList[0]){
      answer=false;
      alert("We don't have that food on our chart. Please select something else");
    }else if (food.toLowerCase()!==foodList[1]){
      answer=false;
      alert("We don't have that food on our chart. Please select something else");
    }


//Drink Section -> Ask for which drink, tells the price, and check if the answers are correct!
var drink=prompt('Please type your drink that you would like to order');

//Add value to cost!
if (drink.toLowerCase()===drinkList[0]){
  drinkCost+=drinkPriceList[0];
}else if(drink.toLowerCase()===drinkList[1]){
  drinkCost+=drinkPriceList[1];
}

//What the user didn't typed anything?
if (drink.toLowerCase()===''){
  alert("It seems that you typed something wrong. Please try again!");
  answer=false;
  break;
}

//Check the drink if it is available

if (drink.toLowerCase()===drinkList[0]){
  answer=true;
  alert('You have added ' + drink + ' to your chart. That would you cost: ' +drinkCost + ' $');
}else if (drink.toLowerCase()===drinkList[1]){
  answer=true;
  alert('You have added ' + drink + ' to your chart. That would you cost: ' +drinkCost + ' $');
//If is not on the list
}else if (drink.toLowerCase()!==drinkList[0]){
  answer=false;
  alert("We don't have that drink on our list. Please select something else");
}else if (drink.toLowerCase()!==drinkList[1]){
  answer=false;
  alert("We don't have that drink on our list. Please select something else");
}


}while(!answer)
  answer=true;
  document.write('You have ordered ' + food + ' and ' + drink  + '.' + ' That would cost ' + finalPrice() + ' $');

console.log('The script is done!');

1 Answer

BRAD STYES
BRAD STYES
13,421 Points

Maybe you could combine the '//What if user didn't typed anything?' if block with the '//Add value to Cost' if-else block. It would be more readable that way. Also, I didn't try your code but maybe you want to use continue; instead of break;. Same for the drink section