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

Majid Mokhtari
Majid Mokhtari
10,506 Points

function

Question asks: "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."

I don't know why it says incorrect:

<!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(name){

    name = ['Hello']

    if(typeof name === 'undefined'){


    return 0;
    } }
  console.log(arrayCounter());






</script>

</body> </html>

2 Answers

Nicholas Olsen
seal-mask
.a{fill-rule:evenodd;}techdegree
Nicholas Olsen
Front End Web Development Techdegree Student 19,342 Points

Your function takes in an argument called, "name" and redefines "name" to equal ['Hello']. Instead, it should probably check the original argument. Also, your 'if' statement only checks to see if name is undefined, but the challenge asks you to check if it is a string or a number as well. Try something like this:

function arrayCounter(name){

    // This shouldn't be here
    // name=['Hello']

    // Check if name is undefined, a string or a number
    if (typeof name === undefined || typeof name === 'string' || typeof name === 'number'){
        return 0;
    }

    // You need to return the length of the array
    return name.length;
}

I'm not sure if this will work exactly, but I think it is at least closer to the solution.