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

Cant seem to get this right

Here is the link to the code challenge I am not able to pass.

My code is :

<!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>

      function arrayCounter(randomness) {
        if (typeOf randomness === 'undefined') {
            return 0;
        } 
        if (typeOf randomness === 'string') {
            return 0;
        } 
        if (typeOf randomness === 'number') {
            return 0;
        }
            return randomness.length;
      }

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

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

I have also tried inserting

      function arrayCounter(randomness) {
        if (typeOf randomness === 'undefined' || 'string' || 'number') {
            return 0;
        } else {
            return randomness.length;
       }
      }

and

      function arrayCounter(randomness) {
        if (typeOf randomness === 'undefined') {
            return 0;
        } else if (typeOf randomness === 'string') {
            return 0;
        } else if (typeOf randomness === 'number') {
            return 0;
        } else {
            return randomness.length;
        }
      }

and a few other variations thereof. But every time I keep getting an error on my function declaration. I even opened up sublime and got a console generated error on the line my function was declared on. I also read the other post on this particular code challenge and soon after tried declaring an array to pass into the function. Still nothing. So flustered, please help me figure out what I'm doing wrong.

5 Answers

Chris McKnight
Chris McKnight
11,045 Points

You are missing parentheses for the typeof calls and it must be typeof instead of typeOf. Javascript is case sensitive. In your second example you are missing the parentheses and you are missing the left hand side of the comparison for the additional conditions.

function arrayCounter(randomness) {
    if (typeof(randomness) === 'undefined' || typeof(randomness) === 'string' || typeof(randomness) === 'number') {
        return 0;
    } else {
        return randomness.length;
   }
}

Some of those code examples didn't insert right, but hopefully you get the picture.

The code challenge responder says "Bummer! You're missing a function named 'arrayCounter'" but I can't tell how or where I am not telling it that my function is named arrayCounter!

I can't believe I missed that for over an hour. Driving myself nuts. Got it now.

Thanks so much.

James Barnett
James Barnett
39,199 Points

That's what jshint/jslint is for, checking for those syntax errors.

It's built into jsbin, there's an analyze JS feature in codepen, there's also a JSLint extension for notepad++.

Sweet james - I have tinkered with JSfiddle before, but I'm not familiar with jshint or jslint. Call me silly but I'm hooked on Sublime, any such extension that you would know of for Sublime 2?

James Barnett
James Barnett
39,199 Points

Yep, in fact the sublime package is available on the jshint.com

Also there's a jslint feature on jsfiddle as well.

Chris McKnight
Chris McKnight
11,045 Points

I am a fan of sublime as well, however, I haven't been a fan of using jshint/jslint. I have found it to be a nuisance when run against 3rd party libraries.

which libraries are you running and is it common or even possible to use more than one at a time? I haven't gotten to that point yet, still on the basics, but I will be there shortly.

Chris McKnight
Chris McKnight
11,045 Points

Mostly 3rd party jQuery plugins. Some of them give warnings because they missing semicolons, etc.

I also use SublimeLinter which seems to do a similar job.

James Barnett
James Barnett
39,199 Points

Oliver Monk - SublimeLinter is a collection of various lint libraries including JSLint.

u da man. Thank you.