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 trialMUZ140252 Prudence Khupe
8,921 Pointsfunctions
failing to understand the code requirement
<!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(){
if (typeof arrayCounter === 'undefined'){
arraycounter.length = 0;
} Else{
return array.length;
}
}
</script>
</body>
</html>
Marcus Parsons
15,719 PointsCan you please choose a best answer, Prudence? Thank you!
2 Answers
Marcus Parsons
15,719 PointsHey Prudence,
This challenge wants you to check whether an argument within arrayCounter is an array or not, and if it is, return the length of the array, otherwise, return 0. You have to have an argument for the arrayCounter to check so you need to put a variable name within the function, like "arg" which is just short for "argument".
Since you can't check whether a variable is an array or not (because arrays are actually just specific objects), you can, however, check to see if it is a string, number, or undefined. That means you need to add two more conditions to your if statement.
You also need to just return 0, not do whatever it is you're attempting to do in the if statement. So, your code should look like this:
function arrayCounter(arg){
if (typeof arg === 'undefined' || typeof arg === 'string' || typeof arg === 'number'){
return 0;
} else{
return arg.length;
}
}
Jonathan Grieve
Treehouse Moderator 91,253 PointsThe function is checking for a string of certain characters but it first wants to know if there is a string to be read. If it isn't a string it'll return because there are no characters but if there is a string it'll send back to the function how many characters there are in the string... in other words the length of the string. :-)
Happy .
11,414 PointsHappy .
11,414 Pointsyou have an upper case E on Else, it should read else. as for any other problems i'm not sure sorry