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 trialCaitlin Cadieux
2,374 PointsCan't figure out why array length won't return.
Been working on this for a little bit and I'm completely stumped. Not sure what's going wrong but I can't get the array length to return (I'm not sure I'm getting any of this to actually work correctly). Can anyone shed some light?
<!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 (thing) {
if (typeof thing === 'string', 'number', 'undefined') {
return 0;
}
return thing.length;
};
</script>
</body>
</html>
1 Answer
Gareth Borcherds
9,372 PointsYou have to write out each statement separately and connect them with || to simulate OR. Like this:
function arrayCounter (thing) {
if (typeof thing == 'string' || typeof thing == 'number' || typeof thing == 'undefined') {
return 0;
}
return thing.length;
};
Caitlin Cadieux
2,374 PointsCaitlin Cadieux
2,374 PointsI'm actually getting a syntax error with this for some reason. The main error I get back with the code in my post is that the array length is returning as 0 for some reason as well...
Gareth Borcherds
9,372 PointsGareth Borcherds
9,372 PointsI just ran this in the console and it worked fine. What other code are you using with it?
Caitlin Cadieux
2,374 PointsCaitlin Cadieux
2,374 Pointsthat's it, I just copy pasted your code in to see what would happen and it returned a syntax error :(
Caitlin Cadieux
2,374 PointsCaitlin Cadieux
2,374 Pointswelp! Just handtyped it in and somehow it worked. Thank you so much! Not sure what exactly went wrong there but I'm sure it was user error, haha.