0

my name's Mike and my question is two-fold:

  1. How can I access the objects in my array so that they properly appear in my question prompt, and
  2. How can I access the properties of the randomely selected object in an if/else statement?

I'm trying to make a simple flashcard program to help me memorize different kinds of sound equipment. The list of equipment is large but I'm only including three different kinds to keep this example simple. I want each object to have two properties: answer and desc. This first part defines three objects, places them in an array, creates a variable for picking one of the array items randomely, and another variable for prompting the user for an answer:

var newFlash = function() {
    var A827 = {
        answer: "T",
        desc: "Multitrack Tape Recorder"
    };
    var LA2A = {
        answer: "O",
        desc: "Classic Leveling Amplifier"
    };
    var SonyC800G = {
        answer: "M",
        desc: "Tube Condenser Microphone"
    };
    var list = [A827, LA2A, SonyC800G];
    var rand = Math.floor(Math.random() * list.length);
    var question = prompt("What kind of equipment is " + list[rand] + "?");
};

Now, if I make my three items in my array all strings, they show up no problem in the question prompt correctly replacing list[rand] with the appropriate array item. However, using objects in my array, my prompt says "What kind of equipment is [object Object]?.

My end goal is for the user to enter the appropriate one- or two-letter response (M for Microphone, C for Console, O for Outboard Gear, T for Tape Machine, S for Software, and CH for Computer Hardware) where upon entering the successful letter(s) yields an alert that displays both the object's answer and desc. My n00b instinct tells me this second part should be an if/else statement in the form of

if (question == list[rand.answer]) {
    alert("Correct, Answer: " + list[rand.answer] + ", a " + list[rand.desc] + "!");
}
else {
    alert("Wrong, try again.");
}

but I'm very certain that this isn't the right way to access these object properties.

So, again, my question has two parts:

  1. How can I access the objects in my array so that they properly appear in my question prompt, and
  2. How can I access the properties of the randomely selected object in an if/else statement?

I'm sure some piece of logic is escaping me. Thanks for reading.

4

2 回答 2

0

你想用var question = prompt("What kind of equipment is " + list[rand].desc + "?");. list[rand]将为您生成一个具有结构的对象{answer: "", desc: ""},因此您需要另外访问代码中的描述。

同样,您想要:

if (question == list[rand].answer) {
    alert("Correct, Answer: " + list[rand].answer + ", a " + list[rand].desc + "!");
}
else {
    alert("Wrong, try again.");
}
于 2013-04-25T03:45:23.027 回答
0

要在 Javascript 中访问对象的属性,请使用点表示法,这在许多具有对象的语言中很常见。 list是一个对象数组,因此当您键入时,list[rand]您将返回其中一个对象。一旦你有了一个对象,你只需要使用点符号来访问你需要的任何属性,在这种情况下是descanswer

所以而不是

var question = prompt("What kind of equipment is " + list[rand] + "?");

尝试

var question = prompt("What kind of equipment is " + list[rand].desc + "?");

将您尝试访问的属性放置在括号之外。这也解决了您的第二个问题,只需更改:

if (question == list[rand.answer]) {
    alert("Correct, Answer: " + list[rand.answer] + ", a " + list[rand.desc] + "!");

至:

if (question == list[rand].answer) {
    alert("Correct, Answer: " + list[rand].answer + ", a " + list[rand].desc + "!");

这个小提琴将有助于演示。

于 2013-04-25T03:46:44.240 回答