I have an object which contains questions and each question has a different difficulty denoted by allQuiz[x].difficulty.
I have written a for loop which will sort the questions in their relative difficulty categories. For example difficulty 25 = easy , difficulty 50 = medium etc.
Here's a portion of the object structure:
{
"id": "41",
"category": "TV & Movies",
"locales": "GB#US",
"difficulty": "50",
"question": {
"Voice": "Which fictional city is the home of Batman?",
"Display": "Which fictional city is the home of Batman?"
},
"answer": {
"Voice": "Gotham City",
"Display": "Gotham City"
}
},
{
"id": "42",
"category": "TV & Movies",
"locales": "GB#US",
"difficulty": "50",
"question": {
"Voice": "The Hunchback of Notre Dame was also known as?",
"Display": "The Hunchback of Notre Dame was also known as?"
},
"answer": {
"Voice": "Quasimodo",
"Display": "Quasimodo"
}
},
{
"id": "43",
"category": "TV & Movies",
"locales": "GB#US",
"difficulty": "75",
"question": {
"Voice": "The title role of the 1990 movie Pretty Woman was played by which actress?",
"Display": "The title role of the 1990 movie Pretty Woman was played by which actress?"
},
"answer": {
"Voice": "Julia Roberts",
"Display": "Julia Roberts"
}
},
{
"id": "44",
"category": "TV & Movies",
"locales": "GB#US",
"difficulty": "75",
"question": {
"Voice": "Which Tom Hanks movie won the Academy Award for Best Picture in 1994?",
"Display": "Which Tom Hanks movie won the Academy Award for Best Picture in 1994?"
},
"answer": {
"Voice": "Forrest Gump",
"Display": "Forrest Gump"
}
},
{
"id": "45",
"category": "TV & Movies",
"locales": "GB#US",
"difficulty": "100",
"question": {
"Voice": "Jonny Depp starred as Jack Sparrow in which series of movies?",
"Display": "Jonny Depp starred as Jack Sparrow in which series of movies?"
},
"answer": {
"Voice": "Pirates of the Caribbean",
"Display": "Pirates of the Caribbean"
}
},
{
"id": "46",
"category": "TV & Movies",
"locales": "GB#US",
"difficulty": "100",
"question": {
"Voice": "Marion Crane is the tragic figure in which cult horror film?",
"Display": "Marion Crane is the tragic figure in which cult horror film?"
},
"answer": {
"Voice": "Psycho",
"Display": "Psycho"
}
},
{
"id": "47",
"category": "Science & Technology",
"locales": "GB#US",
"difficulty": "25",
"question": {
"Voice": "Alexander Graham Bell invented which communication tool?",
"Display": "Alexander Graham Bell invented which communication tool?"
},
"answer": {
"Voice": "Telephone",
"Display": "Telephone"
}
},
{
"id": "48",
"category": "Science & Technology",
"locales": "GB#US",
"difficulty": "75",
"question": {
"Voice": "Steve jobs was the founder of which tech giant?",
"Display": "Steve jobs was the founder of which tech giant?"
},
"answer": {
"Voice": "Apple",
"Display": "Apple"
}
},
{
"id": "49",
"category": "Science & Technology",
"locales": "GB#US",
"difficulty": "75",
"question": {
"Voice": "Which year was Amazon founded?",
"Display": "Which year was Amazon founded?"
},
"answer": {
"Voice": "1994",
"Display": "1994"
}
},
{
"id": "50",
"category": "Science & Technology",
"locales": "GB#US",
"difficulty": "100",
"question": {
"Voice": "What is the natural habitat of an arboreal creature?",
"Display": "What is the natural habitat of an arboreal creature?"
},
"answer": {
"Voice": "Trees",
"Display": "Trees"
}
},
{
"id": "51",
"category": "Science & Technology",
"locales": "GB#US",
"difficulty": "25",
"question": {
"Voice": "What is a geiger counter used to detect?",
"Display": "What is a geiger counter used to detect?"
},
"answer": {
"Voice": "Radiation",
"Display": "Radiation"
}
},
{
"id": "52",
"category": "Science & Technology",
"locales": "GB#US",
"difficulty": "50",
"question": {
"Voice": "What element on the periodic table has the chemical symbol H?",
"Display": "What element on the periodic table has the chemical symbol H?"
},
"answer": {
"Voice": "Hydrogen",
"Display": "Hydrogen"
}
},
{
"id": "53",
"category": "Science & Technology",
"locales": "GB#US",
"difficulty": "50",
"question": {
"Voice": "What is the more common name for Nitrous Oxide? �",
"Display": "What is the more common name for Nitrous Oxide? �"
},
"answer": {
"Voice": "Laughing gas",
"Display": "Laughing gas"
}
},
{
"id": "54",
"category": "Science & Technology",
"locales": "GB#US",
"difficulty": "75",
"question": {
"Voice": "In computing, how many bits are in a byte?",
"Display": "In computing, how many bits are in a byte?"
},
"answer": {
"Voice": "Eight",
"Display": "Eight"
}
},
{
"id": "55",
"category": "Science & Technology",
"locales": "GB#US",
"difficulty": "50",
"question": {
"Voice": "Who discovered Penicillin?",
"Display": "Who discovered Penicillin?"
},
"answer": {
"Voice": "Alexander Fleming",
"Display": "Alexander Fleming"
}
},
{
"id": "56",
"category": "Science & Technology",
"locales": "GB#US",
"difficulty": "25",
"question": {
"Voice": "Which planet in our solar system is closest to the sun?",
"Display": "Which planet in our solar system is closest to the sun?"
},
"answer": {
"Voice": "Mercury",
"Display": "Mercury"
}
},
{
"id": "57",
"category": "Science & Technology",
"locales": "GB#US",
"difficulty": "25",
"question": {
"Voice": "What subject did Mark Zuckerburg study at university?",
"Display": "What subject did Mark Zuckerburg study at university?"
},
"answer": {
"Voice": "Computer Science",
"Display": "Computer Science"
}
},
And here's my attempt:
var allQuiz= myObject;
var easyQuiz =[];
var mediumQuiz =[];
var hardQuiz =[];
var veryhardQuiz =[];
for(let x=0;x<allQuiz.length;x++){
if(allQuiz[x].difficulty==25){
let tmp = [];
tmp.push((allQuiz[x]).id);
tmp.push((allQuiz[x]).question.Display);
tmp.push((allQuiz[x]).answer.Display);
easyQuiz.push(tmp);
}
else if(allQuiz[x].difficulty==50){
let tmp =[];
tmp.push((allQuiz[x]).id);
tmp.push((allQuiz[x]).question.Display);
tmp.push((allQuiz[x]).answer.Display);
mediumQuiz.push(tmp);
}
else if(allQuiz[x].difficulty==75){
let tmp =[];
tmp.push((allQuiz[x]).id);
tmp.push((allQuiz[x]).question.Display);
tmp.push((allQuiz[x]).answer.Display);
hardQuiz.push(tmp);
}
else {
let tmp = [];
tmp.push((allQuiz[x]).id);
tmp.push((allQuiz[x]).question.Display);
tmp.push((allQuiz[x]).answer.Display);
veryhardQuiz.push(tmp);
}
}
However, I have noticed there is a lot of code duplication here, the only factors which change are the quiz category im pushing to , and the difficulty of the question.
How could I go about making this for loop more elegant and or more efficient?