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 Loops, Arrays and Objects Tracking Multiple Items with Arrays Build a Quiz Challenge, Part 1

Sean Flanagan
Sean Flanagan
33,232 Points

No good at challenges

Hi.

I can do the quizzes and code challenges but I'm not very good at challenges like this one, I'm ashamed to say.

I've made a start on my JS:

var quiz = [
  ['How many states does the USA have?', 50],
  ['How many legs does a spider have?', 8],
  ['How many continents are there?', 6]
];
function print(message) {
  document.write(message);
}

I'd appreciate any input.

Sean :-)

Sean, what are you trying to do? Your 2D array looks fine, and you have a print function. are you trying to create the quiz game? Or is there another aspect of the challenge that you are having trouble working with?

Sean, before you go any further, wrap those numbers in quotes so that they're strings otherwise you're going to end up having to parse the numbers from the string based prompt and you will limit yourself to only being able to have number based answers...

var quiz = [
  ['How many states does the USA have?', '50'],
  ['How many legs does a spider have?', '8'],
  ['How many continents are there?', '6']
];
function print(message) {
  document.write(message);
}

Also, to show you how you can make the quiz game dynamic and accept more than one right answer, check out this codepen I made: http://codepen.io/anon/pen/WvppZX?editors=001

Sean Flanagan
Sean Flanagan
33,232 Points

Hi Jacob. Hi Marcus. Thank you both for your attempts to help. Sorry for the delay but I wanted to consider my answer very carefully. Marcus, I've converted the numbers to strings like you said.

Is the next step now to create all the variables?

It all depends on your approach. Are you planning on outputting what they did wrong in addition to what they did right? Are you going to show the questions they got wrong and the questions they got right? These are questions to ask yourself to see how you're going to code the rest of this little program. And, don't worry about getting everything you need right then and there; you'll probably think of something you want to implement later on.

Have you ever written pseudo code before? If you've never heard of that, all pseudo code is writing out the steps you need to go through a program in your own terms, not specific to any language, so that you can build a program in whatever language you choose with the logical steps you put down.

For example, if I wanted to write a very quick program to add two numbers of a user's input:

//Receive first input from user and store in variable - num1
//Receive second input from user and store in variable - num2
//Add num1 to num2 and store in variable - total
//Tell user that num1 plus num2 equals total

That's just an example of how to write pseudo code because pseudo code is all about how you want to write your layout to the program, but it allows you to get a structured approach to your program without having to write specific code for it. It's a great technique for when you can't visualize how the code should go.

Sean, I agree with Marcus on using pseudo code to create a structure to the program you want to build. It's a great way to see what you want to do. I also highly recommend that if you're having issues on where to start write some pseudo code, then re-watch the videos and see what code you need to use first and so on to build this program. remember, the challenges are all based on what you know so far, its just the use of implementing it.

Sean Flanagan
Sean Flanagan
33,232 Points

I think I'll watch the preceding videos again so I get the ideas down.

Thank you both

Sean :-)

You're welcome, Sean. Be sure to practice writing pseudo code. It'll be a big help.

I also forgot to mention that using your print function that way is slower than just using document.write() by itself. There's no reason to override the default print function in this way. The way Dave does it in his course is to use a custom print function to output information to an element on the page, not to use document.write() which should only be used for testing code.

Sean, That's a great idea! taking notes and re-watching videos is a solid way to go! I would suggest while watching the video take some handwritten notes and form a plan of how you want you program to run. It never hurts. Then before you code use the pseudo code as an outline of where/what code needs to go in order for the program to execute. if you have anymore questions after that, please post them on the forum. That's what its there for. Happy coding!

What Jacob said =]

Keith Greatz
Keith Greatz
4,377 Points

Hi Sean, to give you a visual look at what's happening when trying to access parts or your 2 dimensional array I made a little reference for you

var abc = [ 
 [1,2,3],
 [4,5,6],
 [7,8,9],
];

to access these particular values you would use

abc[0][0]   This would =    1
abc[0][1]   This would =    2
abc[0][2]   This would =    3
abc[1][0]   This would =    4
abc[1][1]   This would =    5
abc[1][2]   This would =    6
abc[2][0]   This would =    7
abc[2][1]   This would =    8
abc[2][2]   This would =    9

This certainly helped me.

1 Answer

Sean Flanagan
Sean Flanagan
33,232 Points

I've started watching earlier videos to brush up and I'm also taking notes.

Thank you all

Sean :-)