Why is my position_1 not defined in the loop but it is in the menu_position object and it is also in the normal alert call. What's wrong here? I ran this code in FireFox if that makes any difference
let position_Name = prompt('Enter position name please', 'Coffee');
let price = prompt('Enter price on this position', '3$');
let menu_position = {
position_1: {
position_Name,
price
}
};
alert(menu_position.position_1.price);
let position_Name2 = prompt('Enter position name please', 'Steak');
let price2 = prompt('Enter price on this position please', '10$');
menu_position.position_2 = {position_Name2, price2};
let question = prompt('Select position please (position_1, position_2)', 'position_1');
alert(menu_position.position_1.position_Name)
if (question == position_1){
alert(menu_position.position_1.position_Name)
}
Why is my position_1 not defined in the loop but it is in the menu_position object and it is also in the normal alert call. What's wrong here? I ran this code in FireFox if that makes any difference
let position_Name = prompt('Enter position name please', 'Coffee');
let price = prompt('Enter price on this position', '3$');
let menu_position = {
position_1: {
position_Name,
price
}
};
alert(menu_position.position_1.price);
let position_Name2 = prompt('Enter position name please', 'Steak');
let price2 = prompt('Enter price on this position please', '10$');
menu_position.position_2 = {position_Name2, price2};
let question = prompt('Select position please (position_1, position_2)', 'position_1');
alert(menu_position.position_1.position_Name)
if (question == position_1){
alert(menu_position.position_1.position_Name)
}
Share
Improve this question
edited Mar 29 at 19:47
desertnaut
60.5k32 gold badges155 silver badges181 bronze badges
asked Mar 29 at 18:38
AlexanderAlexander
332 bronze badges
0
1 Answer
Reset to default 2Because in the if statement, you are referring to the position_1
variable and not to menu_position.position_1
.
if (question == position_1) { // Error: position_1 is not defined
alert(menu_position.position_1.position_Name)
}
let position_Name = prompt('Enter position name please', 'Coffee');
let price = prompt('Enter price on this position', '3$');
let menu_position = {
position_1: {
position_Name,
price
}
};
alert(menu_position.position_1.price);
let position_Name2 = prompt('Enter position name please', 'Steak');
let price2 = prompt('Enter price on this position please', '10$');
menu_position.position_2 = {position_Name2, price2};
let question = prompt('Select position please (position_1, position_2)', 'position_1');
alert(menu_position.position_1.position_Name)
if (question == menu_position.position_1){
alert(menu_position.position_1.position_Name)
}
Or did you want to put it in a string as "position_1"
? It's not very clear.
let position_Name = prompt('Enter position name please', 'Coffee');
let price = prompt('Enter price on this position', '3$');
let menu_position = {
position_1: {
position_Name,
price
}
};
alert(menu_position.position_1.price);
let position_Name2 = prompt('Enter position name please', 'Steak');
let price2 = prompt('Enter price on this position please', '10$');
menu_position.position_2 = {position_Name2, price2};
let question = prompt('Select position please (position_1, position_2)', 'position_1');
alert(menu_position.position_1.position_Name)
if (question == "position_1"){
alert(menu_position.position_1.position_Name)
}