ramochoa ramochoa - 7 months ago 12
Javascript Question

For loop not accessing JSON Objects properly?

For some reason when I finish the quiz the proper JSON object property isn't being accessed. Once the string is identified its supposed to access the property. If I hard code it . [0], [1], etc it works. How can I fix this? Also, how can I make this code more succinct? I feel there might be another way besides using so many if statements. Thanks.

The object in question

var personTypes = [{
type : "INTJ",
typeInfo: "Imaginative and strategic thinkers, with a plan for everything.",

},
{type : "INTP",
typeInfo: "Innovative inventors with an unquenchable thirst for knowledge.",

},
{type : "ENTJ",
typeInfo: "Bold, imaginative and strong-willed leaders, always finding a way – or making one.",

},
{type : "ENTP",
typeInfo: "Smart and curious thinkers who cannot resist an intellectual challenge.",

},
{type : "INFJ",
typeInfo: "Quiet and mystical, yet very inspiring and tireless idealists",

},
{type : "INFP",
typeInfo: "Poetic, kind and altruistic people, always eager to help a good cause.",

},
{type : "ENFJ",
typeInfo: "Charismatic and inspiring leaders, able to mesmerize their listeners.",

},
{type : "ENFP",
typeInfo: "Enthusiastic, creative and sociable free spirits, who can always find a reason to smile.",

},
{type : "ISTJ",
typeInfo: "Practical and fact-minded individuals, whose reliability cannot be doubted.",

},
{type : "ISFJ",
typeInfo: "Very dedicated and warm protectors, always ready to defend their loved ones.",

},
{type : "ESTJ",
typeInfo: "Excellent administrators, unsurpassed at managing things – or people.",

},
{type : "ESFJ",
typeInfo: "Extraordinarily caring, social and popular people, always eager to help.",

},
{type : "ISTP",
typeInfo: "Bold and practical experimenters, masters of all kinds of tools.",

},
{type : "ISFP",
typeInfo: "Flexible and charming artists, always ready to explore and experience something new.",

},
{type : "ESTP",
typeInfo: "Smart, energetic and very perceptive people, who truly enjoy living on the edge",

},
{type : "ESFP",
typeInfo: "Spontaneous, energetic and enthusiastic people – life is never boring around them.",

},
];


Accessing the object

// once done with quiz
if (questionNum === 3) {

//concat radio inputs
var typeConcat = this.ei + this.sn+ this.tf+ this.pj;
//output concat to screen
$("p").show();
$("h2").text("Your type is " + typeConcat);



//use this data inside the <p>
for (i = 0; i < personTypes.length; i++) {
if (typeConcat=="INTJ") {
$("p").text(personTypes[i].typeInfo);
}
if (typeConcat=="INTP") {
$("p").text(personTypes[i].typeInfo);
}
if (typeConcat=="ENTJ") {
$("p").text(personTypes[i].typeInfo);
}
if (typeConcat=="ENTP") {
$("p").text(personTypes[i].typeInfo);
}
if (typeConcat=="INFJ") {
$("p").text(personTypes[i].typeInfo);
}
if (typeConcat=="INFP") {
$("p").text(personTypes[i].typeInfo);
}
if (typeConcat=="ENFJ") {
$("p").text(personTypes[i].typeInfo);
}
if (typeConcat=="ENFP") {
$("p").text(personTypes[i].typeInfo);
}
if (typeConcat=="ISTJ") {
$("p").text(personTypes[i].typeInfo);
}
if (typeConcat=="ISFJ") {
$("p").text(personTypes[i].typeInfo);
}
if (typeConcat=="ESTJ") {
$("p").text(personTypes[i].typeInfo);
}
if (typeConcat=="ESFJ") {
$("p").text(personTypes[i].typeInfo);
}
if (typeConcat=="ISTP") {
$("p").text(personTypes[i].typeInfo);
}
if (typeConcat=="ISFP") {
$("p").text(personTypes[i].typeInfo);
}
if (typeConcat=="ESTP") {
$("p").text(personTypes[i].typeInfo);
}
if (typeConcat=="ESFP") {
$("p").text(personTypes[i].typeInfo);
}
}
}

Answer

I think you don't need all of the if statements, when you have the type specified, right? This would work just the same.

EDIT: And don't forget to include var before the i = 0.

for (var i = 0; i < personTypes.length; i++) {    
    if (typeConcat==personTypes[i].type) {
        $("p").text(personTypes[i].typeInfo);
    }
}