Reputation:
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);
}
}
}
Upvotes: 0
Views: 60
Reputation: 1452
A more succinct version would be putting the types in a hashmap and access the type by the typeConcat given:
var personTypes = {
INTJ: "Imaginative and strategic thinkers, with a plan for everything.",
INTP: "Innovative inventors with an unquenchable thirst for knowledge.",
ENTJ: "Bold, imaginative and strong-willed leaders, always finding a way – or making one.",
ENTP: "Smart and curious thinkers who cannot resist an intellectual challenge.",
INFJ: "Quiet and mystical, yet very inspiring and tireless idealists",
INFP: "Poetic, kind and altruistic people, always eager to help a good cause.",
ENFJ: "Charismatic and inspiring leaders, able to mesmerize their listeners.",
ENFP: "Enthusiastic, creative and sociable free spirits, who can always find a reason to smile.",
ISTJ: "Practical and fact-minded individuals, whose reliability cannot be doubted.",
ISFJ: "Very dedicated and warm protectors, always ready to defend their loved ones.",
ESTJ: "Excellent administrators, unsurpassed at managing things – or people.",
ESFJ: "Extraordinarily caring, social and popular people, always eager to help.",
ISTP: "Bold and practical experimenters, masters of all kinds of tools.",
ISFP: "Flexible and charming artists, always ready to explore and experience something new.",
ESTP: "Smart, energetic and very perceptive people, who truly enjoy living on the edge",
ESFP: "Spontaneous, energetic and enthusiastic people – life is never boring around them."
}
$("p").text(personTypes[typeConcat]);
Upvotes: 0
Reputation: 393
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);
}
}
Upvotes: 1