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 JavaScript Basics (Retired) Creating Reusable Code with Functions Giving Information to Functions

Alejandro Rodriguez
Alejandro Rodriguez
3,020 Points

Return and get TOTAL PRICE...

Hello, i get this code... the main idea is get the return of what user select and at the same time get the correct price for the item selected... but my code works fine in order items but DO NOT work for get the total price, the result is ever ¢0

Please Help

//*** Mi ejercicio de Return ***
alert('Bienvenido a AutoMAC. Por favor haga click en OK para realizar su orden.')
var item1 = prompt ('Seleccione un alimento (Hamburguesa, Pollo, Ensalada)');
var item2 = prompt ('Seleccione una bebida (Coca Cola, Te frio, Agua)');
var item3 = prompt ('Seleccione un postre (Pastel de Manzana, Helado, Fruta)');

var precio = 0;

if (item1.toUpperCase() === 'Hamburguesa') {
  precio1 += 1200;
}else if(item1.toUpperCase() === 'Pollo') {
  precio1 += 750;
}else if(item1.toUpperCase() === 'Ensalada') {
  precio1 += 750;
} else {
  precio1 = 0;
}

if (item2.toUpperCase() === 'Coca Cola') {
  precio2 += 750;
}else if(item2.toUpperCase() === 'Te frio') {
  precio2 += 600;
}else if(item2.toUpperCase() === 'Agua') {
  precio2 += 500;
} else {
  precio2 = 0;
}

if (item3.toUpperCase() === 'Pastel de Manzana') {
  precio3 += 600;
}else if(item3.toUpperCase() === 'Helado') {
  precio3 += 500;
}else if(item3.toUpperCase() === 'Fruta') {
  precio3 += 300;
} else{
  precio3 = 0;
}

var precio = precio1 + precio2 +precio3 ;

function orden (alimento, bebida, postre){
  var cajafeliz = alimento + ' + ' + bebida + ' + ' + postre;
  return cajafeliz;
}

document.write ('<h2>Perfecto su orden sería: ' + orden(item1, item2, item3) + '. Todo en una Cajita Feliz ;)' + ' su total a pagar es de: ' + '¢' + precio + ' colones </h2>')

1 Answer

Steven Parker
Steven Parker
217,602 Points

I don't understand how this code works for any selection. A word converted to upper case should not match a word in mixed case:

} else if (item3.toUpperCase() === 'Fruta') {  // this will never match
} else if (item3.toUpperCase() === 'FRUTA') {  // but this will

Also, the addition assignment operator ("+=") can only be used on variables that have previously been initialized.

var precio = precio1 = precio2 = precio3 = 0;  // ALL of these must be initialized

But based on how these are used, a simple assignment might do instead of addition assignment (pre-declaring them is still a good practice).