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 JavaScript Foundations Functions Return Values

Stage 5: Functions Code Challenge

I am quite stuck on this, and have been for a while. For some reason, Arrays just isn't clicking with me. The challenge is as follows:

"Around line 17, create a function named 'arrayCounter' that takes in a parameter which is an array. The function must return the length of an array passed in or 0 if a 'string', 'number' or 'undefined' value is passed in."

Thus far, this is the code I have...

      var x = ['string', '3', true];
      function arrayCounter (x) {

        if (typeof x === 'array') {
          return x.length;
        } else {
         return 0; 
        }        

      }

Any feedback would be greatly appreciated.

Thanks!

index.html
<!DOCTYPE html>
<html lang="en">
  <head>
    <title>JavaScript Foundations: Functions</title>
    <style>
      html {
        background: #FAFAFA;
        font-family: sans-serif;
      }
    </style>
  </head>
  <body>
    <h1>JavaScript Foundations</h1>
    <h2>Functions: Return Values</h2>
    <script>
      var x = ['string', '3', true];
      function arrayCounter (x) {

        if typeof x === 'array' {
          return x.length;
        } else {
         return 0; 
        }        

      }

    </script>
  </body>
</html>

The issue is that

  if (typeof x === 'array') {

is never going to return true. When an array goes through the typeOf operator, it returns a value of 'object'.

One possible solution is to replace it with

if (Object.prototype.toString.call( x ) === '[object Array]' ) {

Arrays that are tested with the toString method here should return '[object Array]', and you can build your code from there.

Hope this helps!

3 Answers

Chris Shaw
Chris Shaw
26,650 Points

Hi William,

You have a good idea of what's required but aren't following what the task is asking for, see the below.

  1. You've created a variable x which isn't needed, this can be omitted
  2. There's no typeof value array, you need to check against string, number and undefined
function arrayCounter(array) {
  if (typeof array === 'string' || typeof array === 'number' || typeof array === 'undefined') {
    return 0;
  }

  return array.length;
}

Happy coding!

Thanks everyone for all the assistance!

William Li
PLUS
William Li
Courses Plus Student 26,865 Points

Your condition check missed something.

return 0 if a 'string', 'number' or 'undefined' value is passed in.