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

Why are my "answer" and function for "sum_values() " both coming up as undefined?

More than a bit frustrated with my code. I've looking at this all day to no avail on my own. I'm still not seeing where the problem lies in my JavaScript. Take a look https://w.trhou.se/yj3gcu0g11

2 Answers

Steven Parker
Steven Parker
243,670 Points

The actual variable name is "answers" (with an "s"), "answer" really is undefined.

On the other hand, sum_values IS defined, at least in the JavaScript file. But you have some redundant script code in your HTML file. ALL the JavaScript should be in the external file. Anything needed from the HTML should be moved over, and anything redundant (I think it might ALL be redundant) should just be eliminated. The only <script> tag should be the one that references the external file.

And are you ever going to use that nice, compact replacement for dropUser that I gave you before? It will make your program a whole lot smaller!

Another thing, does this tally score properly on your browser?

        return $(this).css('background-color') == 'green';

Because, on mine, it only works this way:

        return $(this).css('background-color') == 'rgb(0, 128, 0)';

Thanks once again Steven Parker ! It started working after I fixed the answer/ answers problem and changed green back to the rgb. I also converted to the compact code for dropUser you sugguested.

Hey there Steven Parker ! Not sure why the scoring didn't work for me at all even using the coding you provided. I've taken out the color and just want to place value on the right column combinations. Having a few issues with that and I believe it maybe be with how this is currently defined. If I include question1 in anyway it comes up undefined or NaN.

function sum_values(){
var the_sum = 0;
for (var question in answers){

    the_sum = the_sum  + parseInt(answers[question]) ;
}
return the_sum
}
Steven Parker
Steven Parker
243,670 Points

The drag-and-drop score was working for me in the code I gave you previously, which included the last line shown above. Using that filter, the surrounding code gave question1 the score of the total of green answers.

I'd say that 'answer' is undefined because your variable is 'answers' (missing the 's'). This means that you are looking for undefined in answers, which is undefined. Adding the 's' should fix it up, try the below instead.

for (var question in answers){
    the_sum = the_sum + parseInt(answers[question]);
}

Thank you!!