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

naming functions.

''' function arrayCounter(array){ if (typeof array = 'undefined'){ return 0; } return array.length; } '''

is not parsing, error = missing a function arrayCounter.

What am I doing wrong?

7 Answers

Christian Oseguera
Christian Oseguera
1,680 Points

Hi, check the == This one seems to work:

function arrayCounter(array){
  if (typeof array == 'undefined'){ return 0; } return array.length;
}
Christian Oseguera
Christian Oseguera
1,680 Points

Check if you typed script type="text/javascript" before your function with the closing /script after it. If your function is loaded in a file, check it is properly loaded.

this question is related to the code challenge. The actual question is:

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.

Can anyone see the problem with my code?

<!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(array){
            if (typeof array == 'undefined'){ 
          return 0; 
        } 
        return array.length;
            }

    </script>
  </body>
</html>
Christian Oseguera
Christian Oseguera
1,680 Points

Where you typed < script > you have to type < script type = "text/javascript" >

Christian Oseguera
Christian Oseguera
1,680 Points

" < script type="text/javascript" > function arrayCounter(array){ if (typeof array == 'undefined' || typeof array == 'string' || typeof array == 'number'){ return 0; } return array.length; } < /script > "

I tried < script type="text/javascript" > but it didn't make a difference.

Does anyone else have any ideas?

Christian Oseguera
Christian Oseguera
1,680 Points

This page works: (remove spaces after every < ).

< !DOCTYPE html> < html lang="en"> < head> < meta charset="utf-8"> < title>JavaScript Foundations: Functions< /title> < style> body { background: #FAFAFA; font-family: sans-serif; } < /style> < script>

  function arrayCounter(array){
    if (typeof array == 'undefined'|| typeof array == 'string' || typeof array == 'number'){
      return 0;
    }
    return array.length;
  }  
< /script>

< /head> < body> < h1>JavaScript Foundations< /h1> < h2>Functions: Return Values< /h2> < script> var ay; var miArray = new Array(); miArray[0] = 290; miArray[1] = 330; document.write("<br>Array passed:" + arrayCounter(miArray)); document.write("<br>String passed:" + arrayCounter("Hi!")); document.write("<br>Number passed:" + arrayCounter(4)); document.write("<br>Nothing passed:" + arrayCounter()); < /script> < /body>

< /html>