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

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

Hi all,

Could someone help with this function please?

I am kinda stuck,

:(

5 Answers

Andrew Chalkley
STAFF
Andrew Chalkley
Treehouse Guest Teacher

Hey Daniele Manca

The questions is asking for the values which is val() not val() == "". If that's what you're doing :)

Regards
Andrew

Thanks Andrew, think I am being a little dumb in here, I've been trying to use different variations of the below code, without any success:

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

Dani

Andrew Chalkley
Andrew Chalkley
Treehouse Guest Teacher

No worries Daniele Manca!

There are fields with the class "required".

So to select them you'd use $(".required").

To do what the question is asking you'd need to do this.

// Create the function requiredValues()
    // In the function create an empty array
    // for each of the $(".required") fields
        //push the val() on to the empty array
    // return the array

Hopefully this pseudo-code above will point you in the right direction.

Hey Andrew, thanks again,

But I am still stuck, I do get the concept now, but I am unable to make it work exactly...

This is the code I am now using:

function requiredValues() {
  var myArray = [];
  myArray.push(".required");
  return myArray;  
}
Andrew Chalkley
Andrew Chalkley
Treehouse Guest Teacher

You're almost there...

function requiredValues() {
  var myArray = [];
  $(".required").each(function() {
      myArray.push(//enter the value of $(this) here...);
  });
  return myArray;  
}

Hi Andrew,

Is it possible to use ''' var myArray = new Array; ''' instead of ''' var myArray = [] '''

@Josh, I do agree with you pal, because i felt exactly the same way you described it, oh well...

Hi Andrew,

Is it possible to use ``` var myArray = new Array

instead?

I'm lost. It seems like I'm trying what you suggest, but then I get "task 1 is no longer passing"

function isValidEmail(email){
    return email.indexOf("@") != -1;
}

function requiredValues() {
  var myArray = [];
  $(".required").each(function() {
      myArray.push($(this).val();
  });
  return myArray;  
}

Never mind, I noticed I was missing a bracket.

Andrew Chalkley
Andrew Chalkley
Treehouse Guest Teacher

You'll have a syntax error on this line:

myArray.push($(this).val();

You're missing a )

Thanks, but luckily I caught it before you posted! lol

Andrew Chalkley
Andrew Chalkley
Treehouse Guest Teacher

No worries :) A second pair of eyes is always good to have :)

Have you found that these 3 code tasks cause quite the bit of confusion? I didn't find the material overly difficult in the video, but the way the tasks were structured really threw me off.

I guess what I'm trying to say is that (mostly for task 2 and 3) I wasn't really sure what I was being asked to do.

I dunno, maybe it's just me.