最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why is my position 1 not defined in the loop? - Stack Overflow

programmeradmin1浏览0评论

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
Add a comment  | 

1 Answer 1

Reset to default 2

Because 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)
}

发布评论

评论列表(0)

  1. 暂无评论