Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

Help with return values code challenge plz!

Hey folks,

I am having a tough time in resolving the return values code challenge, could anyone help please?

Thanks!

James Barnett
James Barnett
39,199 Points

Post your code we'll see if we can figure out what issues you are having.

This is what I've been trying to use, without any success...

function arrayCounter (name, surname, greeting) {
        if(typeof name === 'Dani') {
         return name.0; 
        }
        if (typeof surname === 'undefined') {
         surname = 'Manca'; 
        }
      }

1 Answer

Miriam Tocino
Miriam Tocino
14,031 Points

Hi Daniele,

In this case, you should just passed in one argument to the function, which is an array. That doesn't mean you should add several arguments. Just one with the name "array" (for example) would be ok.

function arrayCounter (array) {
}

Next you can set a variable "returnValue" with the value "0". Its value will be returned at the end of the function.

function arrayCounter (array) {
  var returnValue = 0;

  return returnValue;
}

Next you should just specify your conditions so that the value of the variable "returnValue" is accordingly modified. In that case we should be returning the length of the argument just in case it is an array. We can tell the function to keep its value "0" if it is a 'string' or a 'number' or 'undefined. The following code should work:

function arrayCounter (array) {
  var returnValue = 0;

  if (typeof array !== 'string' &&
     typeof array !== 'number' &&
     typeof array !== 'undefined') {
     returnValue = array.length;
  }

  return returnValue;
}

It is also a better practice to make your function return just once at the end. I hope it helped! Good luck with the following!

Miriam

Amazing stuff Miriam, you're a star! Thanks so much for your help! :)

it definitely did, hehe