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

HTML

Having trouble in the interactive website module

I am having trouble with the last coding challenge on the Form Validation and Manipulation badge. The part of the coding challenge requires me to "Create a method called 'requiredValues' that returns an array of all the values of inputs with the class of 'required'." I am not able to do this. I will provide a screenshot below of my markup and would like some help.

Here is the code I have written.

var $required = $(".required") function requiredValues(){ var required = new Array(); $required.each(function(){ required.push($(this).val() == ""); }); return required.sort().pop(); } </script>

21 Answers

Hey Scott - mind emailing me a screenshot of the code challenge while you're working on it and a link to the code challenge directly? help@teamtreehouse.com :) thanks!

Andrew Chalkley
STAFF
Andrew Chalkley
Treehouse Guest Teacher

Hi Scott,

You only need to return the values, not check for blanks.

Hope that helps.

Regards Andrew

Thanks Andrew,

I am having trouble with returning the values. I took in account your email earlier but I am still having trouble .

<script type="text/javascript"> var $required =$(".required");

function requiredValues(Array){
    var required = new Array();
  required.push($required)
};

I took the check blanks out and then I figured I needed to var the class "required" and then create the method required values. Then I thought it would be create a variable for the new Array just like in the video. But when I do that the code checker tells me I am not defining "requiredValues". That's why put Array in the argument and then do .push at the end of the method for $required.

Sorry for having so much trouble with this code challenge it's just making my head hurt trying to figure it out.

Andrew Chalkley
STAFF
Andrew Chalkley
Treehouse Guest Teacher

You're getting closer. I'll add a basic structure:

var $required =$(".required");

function requiredValues(Array){
    var required = new Array();
    //Cycle over each required field (user the each() method on $required
        //In an anonymous function
          //Select the current required field with $(this) and push the .val() on it.

    //Finally return required
};

If you need any more help I'm here :)

I feel like I'm so close.

var $required =$(".required");

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

 return required;
};

It is still giving me "You didn't define the 'requiredValues' method". I understand want I'm doing now I just don't understand the reason behind why it's not working.

Thanks!

Andrew Chalkley
STAFF
Andrew Chalkley
Treehouse Guest Teacher

It may be some kind of syntax error on the third line so the function isn't been define properly. The reason could be is that your passing in Array which is the same as the class being initialized on line 4.

If you start typing this in to Chrome's JavaScript console:

function requiredValues(Array){
  var required = new Array();

You get this error:

SyntaxError: Unexpected end of input

If you ditch the Array out of the requiredValues declaration you should be good :)

I'm also talking with Jim to improve error warning on syntax issues.

Ok well I think we are making progress on this. I'm still getting the same error as before but I have done everything correct. Here is my syntax for my code.

var $required =$(".required");

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

I took the Array out of the method but it's still giving me an error. Just wanted to let you know, I moved onto Ruby so I can keep on working. Thanks for all the help.

Andrew Chalkley
STAFF
Andrew Chalkley
Treehouse Guest Teacher

Looks like you're calling a blanks() method on required.

  return required.blanks();

It doesn't exist so another syntax error. You can remove it and you should be ok.

Ok everything is fixed, triple and quadrupled checked. No null error but still getting the syntax error that I am not naming the method. Should I use a different browser or am I doing something else wrong. I will put my code in again.

var $required =$(".required");

    function requiredValues(){
        var required = new Array();
      $required.each(function(){
        required.push($(this).val());
      });
      return.required();
    };
Andrew Chalkley
STAFF
Andrew Chalkley
Treehouse Guest Teacher

return is a keyword and methods can't be called on it. The following code is calling required() on return and wouldn't work.

  return.required();

So you need a space () rather than a dot (.).

Also you are still using parentheses () at the end of required which means you're attempting to invoke a method. All you need to do is just return required;.

Thanks Andrew!

Another question working on the last coding challenge on the Form Validation and Manipulation Badge. On the first objective, "Call 'map' on the inputs with the class 'required' and return each of their values and store it in a variable named 'values'." I am getting an error of, "You haven't set the variable 'values'"

Here is my code.

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

I love the challenge but my brain hurts working on these LOL

Sorry for the last curly bracket. Didn't mean to put that or maybe I am suppose to make it a function.

Andrew Chalkley
STAFF
Andrew Chalkley
Treehouse Guest Teacher

What happens when you just do this?

var required = $(".required");
var values = required.map(function(){
    return $(this).val(); //I removed a ( at before the $
});

Also you can do this for short hand:

var values = $(".required").map(function(){
    return $(this).val(); //I removed a ( at before the $
});

Thanks, it's always nice to have other eyes look over your code. On to part 2 of this coding challenge and am getting an error. I thought I was doing it right but it is giving me an error and saying part 1 is no longer working.

var required = $(".required");
   function containsAndrew() {
    var values = required.map(function(){
            return $(this).val(); 
});
  return $.inArray(Andrew, values) != -1;
};  
Andrew Chalkley
STAFF
Andrew Chalkley
Treehouse Guest Teacher

"Using the 'inArray' method check if the array contains the string 'Andrew'"

You don't need to create a function.

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

All you need to do is call the inArray method afterwards and pass in the string "Andrew" remember strings have quotation marks around it. You missed that off.

Working on the second part of the code challenge. This what I have put in for using the .inArray method. Slightly confused if it is suppose to be in the function or if it is suppose to be separated like was done in the video tutorial.

var required = $(".required");
var values = required.map(function(){
    return $(this).val();
});
return $.inArray(true, values) != "Andrew";
Andrew Chalkley
STAFF
Andrew Chalkley
Treehouse Guest Teacher

The return can only be used inside a function. You're only required to call inArray on the method on the jQuery or $ object.

So $.inArray(_needle_, _haystack_) where needle is the thing you want to search for, i.e. "Andrew" and the haystack is the array of values.

Here's the inArray method documentation too.

Ok this is what I have.

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

I understand your last post but every time I do the inArray like you showed but using "Andrew" and values I still can't get it to work right.

Andrew Chalkley
STAFF
Andrew Chalkley
Treehouse Guest Teacher

This link is embedded in the map() function.

$.inArray("Andrew", values);

This needs to be outside and called afterwards.