Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Basics (Retired) Making Decisions with Conditional Statements The Conditional Challenge

Adam Futrell
Adam Futrell
11,976 Points

Not 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
Steven Parker
216,057 Points

Looks good :+1: 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 :wink: thanks, Jonathan</div>

Adam Futrell
Adam Futrell
11,976 Points

Ah 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!

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 90,705 Points

Hi 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
Adam Futrell
11,976 Points

Awesome! Thanks for fixing the formatting. I will have to remember to do that from now on.