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

Return Value

Can anyone please explain to me what am I doing wrong in this code?

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>
      function  arrayCounter(array){
 if (typeof array==='string') || (typeof array ==='number') || (typeof array==='undefined')) {
   return 0
}

else {return.arraylength;

};
}
    //i understand it now ...just correcting myself
    </script>
  </body>
</html>

7 Answers

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Layeesolo;

The post Hugo Paz mentioned covers things quite well, and you will notice that there are a few different ways of completing the challenge given what has been taught thus far. As one progresses through JavaScript there are multiple ways of achieving the desired results.

Post back if you are still stuck.

Happy coding,

Ken

Hugo Paz
Hugo Paz
15,622 Points

Hi layeesolo,

Have a look here, it provides really good explanations regarding this challenge.

ok,ty guys

i tried those methods still not working

Hugo Paz
Hugo Paz
15,622 Points

Can you post what you tried?

i just did

Hugo Paz
Hugo Paz
15,622 Points

I didnt notice that you updated your post.

function  arrayCounter(array){
        if (typeof array ==='string' || typeof array ==='number' || typeof array ==='undefined') {
                  return 0;
          }

      else {
        return array.length;
          }
      };

opps forgot to add the function name. thanks a lot if (typeof array==='string') I understand it now

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Layeesolo;

You can also break it out into a more verbose way of doing it, and getting the same results with if...else statements and not just or conditions. The or conditionals work for the challenge requirements, but you would potentially have more flexibility with the if...else. Again, this is based upon what we "should know" at this point, or at least I think if...else statements were covered.

Something like:

function arrayCounter(x) {

    if (typeof x === 'string') {
        //do something magical here -or for this challenge, return 0-
        return 0;
    } else if (typeof x === 'number') {
        //do something magical here -or for this challenge, return 0-
        return 0;
    } else if (typeof x === 'undefined') {
        //do something magical here -or for this challenge, return 0-
        return 0;
    } else {
        return x.length;
    }
}

Both ways pass the challenge and, as long as your goal is to return 0 for each of the conditions, are functionally the same with the conditional coding option in the other post obviously being cleaner.

Just another approach to consider going forward.

Ken