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 trialDaniel Chisu
Full Stack JavaScript Techdegree Student 4,321 PointsI can not just figure this out...
I tried all options but nothing works...
<!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 === array) {
return array.length;
}
if(typeof array === "undefined") {
return 0;
}
}
</script>
</body>
</html>
2 Answers
William Li
Courses Plus Student 26,868 PointsBecause this line typeof array === array
is never going to work, typeof [1,2,3]
will return object
, use instanceof
instead in this comparison .
[1, 2, 3] instanceof Array // => true
Daniel Chisu
Full Stack JavaScript Techdegree Student 4,321 PointsI will check later as for now I have to pack the luggage for a trip...Thanks William!
Daniel Chisu
Full Stack JavaScript Techdegree Student 4,321 PointsDaniel Chisu
Full Stack JavaScript Techdegree Student 4,321 Pointsthanks but this we haven't learn yet about this type of keyword...Hope it works!
William Li
Courses Plus Student 26,868 PointsWilliam Li
Courses Plus Student 26,868 Pointssorry about that, Daniel, I didn't take this course so I didn't know; but I look at the code challenge, according to description of problem, looks like you don't need to worry about checking whether it's Array. (since you mention you haven't learned
instanceof
yet)You should first check if the input is 'string', 'number' or 'undefined', if so, return 0; otherwise, just return the array's length. Doing so should get you pass this challenge. hope that helps