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 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,201 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