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

form validation & manipulation > checking values "question #2" NEED HELP

HEY AGAIN LOL

OK THE QUESTION IS..

Create a method called 'requiredValues' that returns an array of all the values of inputs with the class of 'required'.

IM HAVING ALOT OF TROUBLE WITH THIS ONE..

MY ANSWER IS

function requiredValues(){
    return $("input.required").val();
};

I know its probably way off but can you help me out..thanks

17 Answers

James Barnett
James Barnett
39,199 Points

There are a lot of capital letters in that post.

Online writings such as forums and emails words in all capital letters are usually read as YELLING

... so please don't write whole phrases/sentences in all caps. Also you can edit your posts if the all caps was a mistake.

Thanks I'm well aware of what capitals means I was on phone n didn't intentionally do that...

Andrew Chalkley
STAFF
Andrew Chalkley
Treehouse Guest Teacher
function requiredValues(){ return $("input.required").val(); };

You need to create an array in the method first.

var requiredVals = new Array();

Then loop over the required inputs with the jQuery each() method pushing the val() of each element on to the array.

Let me know how you get on.

Thank you so much! I'm at work but a soon as I get home I'm gonna get rite back to it...thanks I will

hey man..im going crazy i cant figure this out ive tried a million things and now im just confusing myself..can you help me out a little more. im not sure whats right whats not whats even real jquery im all confused :(

YESSSSS!!!!!!!! I DID IT!!!!! :) :) :)

o man this one was really making me use my thinking cap lol...thats why i love jquery n javascript. because it makes me think and problem solve...

final answer...

function requiredValues (){ var requiredVals = new Array(); $("input.required").each(function (){ requiredVals.push($(this).val()); }); return requiredVals; }

thanks !!

Andrew Chalkley
STAFF
Andrew Chalkley
Treehouse Guest Teacher
function requiredValues(){ 
//Create a new array here
//The following line will iterate or loop over each required input
$("input.required").each(function(){
  //You can use $(this).val() to select the value 
  //of the "current" input that the loop is going over
  //You can push the value on to the array.
}); 
//At the end of the function you want to use the keyword _return_ 
//to return the array when the requiredValues() method is called
};

Above is some comments with the skeleton code you need. Let me know if you're still having troubles.

Andrew Chalkley
STAFF
Andrew Chalkley
Treehouse Guest Teacher

You did it! Well done!

I'm glad your learning. Keep doing what you're doing, keep experimenting and it'll come to you!

yes thank you

I think that was too big a step from the previous challenges to this one that I simply could not do it. If it wasnt for the above member Domenico getting it right - I would have had to just abandon this lesson. Im going from absolute zero knowledge of jQuery and the lessons got too complicated too fast. Its frustrating as Im paying to learn from scratch and there seems to be an element of the teachers presuming the students already have a previous understanding of jQuery

Andrew Chalkley
STAFF
Andrew Chalkley
Treehouse Guest Teacher

Hello Eoin,

I see your making your way through JavaScript Foundations which is part of the becoming a Web Designer learning adventure, and that a prerequisite for absolute beginners before Building an Interactive Website island.

Do you understand what you need to do in this challenge now with what's been said on here? Or if you don't I can try an clarify.

If you're understanding is more complete now, what do you think was missing that made the challenge feel like a big step.

Thanks Andrew

Mike Shirk
Mike Shirk
6,916 Points

I have to agree with Eoin, I'm currently on JQuery Utility Methods and I think the last few lessons have been too much of a leap.

As a matter of fact I have spent almost a whole day on at trying to solve task 1.

Here's my code:

var $required = $(".required");
function mapTest(){
    var values = $required.map(function(){
        return $(this).val();
    });
}

I keep getting an error message that the "values" variable isn't defined. If I define the values array:

var values = new Array();

then I get an error message saying I have not assigned the map function to $required.

Help!

Mike

I hear what you guys are satin cuz I was also having trouble with some of them but I actually think its better like that because it made me really have to use my brain n I kept not getting it so then I researched more on it n read more stuff re watched the video over n over took notes tried again n when u finally get it ur like ooooo that's why n it's a sense of accomplishment n u actually learn because the best way to learn is mess up n realize why n how n continue ...idk I get what ur sayin but its not suppose to be super easy ..learning anything takes time..

Andrew Chalkley
STAFF
Andrew Chalkley
Treehouse Guest Teacher

Hi Mike,

The issue you're having is to do with scope. Jim goes over scope here.

var values is being defined in the mapTest(). Remove the mapTest() function and it should pass. I believe the code challenge is expecting var values to be in the global scope, i.e. not defined in a function.

I don't want the challenges to be the exact same thing as the video content I slightly change the content of the challenge from the video so it isn't a copy and paste exercise. But I should cover the things in the challenge in the video. If you feel there's something missing or we've missed something please let us know.

To aid students I include further reading links in the notes with each video. There's this forum if you get stuck and we're more than happy to help.

I am willing to listen to any suggestions that may relieve your frustrations, but I promise if you keep at it you'll get it.

Regards Andrew

Mike Shirk
Mike Shirk
6,916 Points

Andrew,

Thanks!

That was the answer:

var $required = $(".required");
var values = $required.map(function(){
  return $(this).val();
});

Guess you can teach an art brain some programming skills after all...

Andrew Chalkley
STAFF
Andrew Chalkley
Treehouse Guest Teacher

Good job Mike!

Keep working those neurons and they'll be with you for life :)

Hi Andrew. Thanks for being there to reply to everyones questions so quickly. Good to know you guys are that passionate about helping us. After sleeping on it I still think this code challenge was a bridge to far for the noobs among us. There were too many new and quite complex ideas in play all at once for me to grasp what I needed to be able to work it out. I have to say I admire Domenico for grinding it out but i feel like there should be a challenge before this one where it builds up the new ideas bit by bit so we can all grasp it instead of just copying the one guy who managed to do it.

I will go through it again tonight to see if i can understand it