Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialAdam Futrell
11,976 PointsNot sure I am using objects correctly. Looking for feedback...
Here is my code...
var Test = {
question:'',
answer:''
};
var correctAnswerCount = 0;
var bronzeCrown = 2;
var silverCrown = 4;
var goldCrown = 5;
var testConstructor = function (inputQuestion, inputAnswer){
var test = new Object();
test.question = inputQuestion;
test.answer = inputAnswer;
return test;
};
var arrTests = new Array();
for (var i = 1; i < 6; i++)
{
arrTests.push(testConstructor('This is question ' + i, i));
}
arrTests.forEach(function(element, index, array){
var input = prompt(element.question);
if (input == element.answer){
correctAnswerCount++;
};
})
document.write('You got ' + correctAnswerCount + ' questions correct.');
if (correctAnswerCount == 0)
{
document.write('You got nothing!');
}
else if (correctAnswerCount <= bronzeCrown)
{
document.write('You got the bronze');
}
else if (correctAnswerCount <= silverCrown && correctAnswerCount >= bronzeCrown)
{
document.write('You got the silver crown');
}
else
{
document.write('You got the gold, baby!')
}
2 Answers
Steven Parker
231,271 PointsLooks good with one small exception where you defined an object named Test at the top that you never use.
It's not needed anyway since you create the required objects with your constructor as you build the array. <div style="color:teal">I'm glad I saw it after the blockquote fixup thanks, Jonathan</div>
Jonathan Grieve
Treehouse Moderator 91,253 PointsHi there Adam,
I fixed your forum markup so we could see it a bit better. Checkout the forum cheatsheet for how to post code and other cool things to the forum.
Your code looks good to me though. :)
Adam Futrell
11,976 PointsAwesome! Thanks for fixing the formatting. I will have to remember to do that from now on.
Adam Futrell
11,976 PointsAdam Futrell
11,976 PointsAh I must have forgotten to remove that from my work space. I was wanting to define the object before constructing it, but I guess the constructor function was enough for creating the object. Thanks!