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 trial

JavaScript JavaScript Loops, Arrays and Objects Tracking Multiple Items with Arrays Build a Quiz Challenge, Part 2 Solution

Christopher Gardner
Christopher Gardner
12,719 Points

Feedback

I was just curious if I could get some feedback on my code.

I will admit, that some of this was a little challenging and I took some of what Dave did. I decided to mix some of it (like the list building function) with mine. I also made my function flexible in that any 2D array with a single question/answer can be ran through it, and not just the array Dave made called 'questions'.

However, I was wondering if there was a way I could add a conditional that would check and see if one of the answers from the array is a number, and if so, parse the answer for that to match. For example, I noticed that if I change the '50' to 50 it won't run correctly, and will say that the first answer is incorrect.

Here is my workspace for this: https://w.trhou.se/kd3ba68pg5

1 Answer

Steven Parker
Steven Parker
230,995 Points

Why convert? The input always comes in as a string, so if you keep all your results as strings you can compare then directly with no conversion.

But what you are asking could be done using the "typeof" operator:

if (typeof answer == "number")      // if the answer is a number
    response = parseInt(response);  // then convert the response into one also
Christopher Gardner
Christopher Gardner
12,719 Points

Awesome! Thank you! Was just thinking of the hypothetical. Someone editing my code, adding another array to be ran through, or adding more to my array and they didn't make a numerical answer a string.