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 trialAlejandro Rodriguez
3,020 PointsReturn 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
231,268 PointsI 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).