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 trialJoe Moors
3,334 PointsCant seem to get this right
Here is the link to the code challenge I am not able to pass.
My code is :
<!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(randomness) {
if (typeOf randomness === 'undefined') {
return 0;
}
if (typeOf randomness === 'string') {
return 0;
}
if (typeOf randomness === 'number') {
return 0;
}
return randomness.length;
}
</script>
</body>
</html>
</script>
</body>
</html>
I have also tried inserting
function arrayCounter(randomness) {
if (typeOf randomness === 'undefined' || 'string' || 'number') {
return 0;
} else {
return randomness.length;
}
}
and
function arrayCounter(randomness) {
if (typeOf randomness === 'undefined') {
return 0;
} else if (typeOf randomness === 'string') {
return 0;
} else if (typeOf randomness === 'number') {
return 0;
} else {
return randomness.length;
}
}
and a few other variations thereof. But every time I keep getting an error on my function declaration. I even opened up sublime and got a console generated error on the line my function was declared on. I also read the other post on this particular code challenge and soon after tried declaring an array to pass into the function. Still nothing. So flustered, please help me figure out what I'm doing wrong.
5 Answers
Chris McKnight
11,045 PointsYou are missing parentheses for the typeof
calls and it must be typeof
instead of typeOf
. Javascript is case sensitive. In your second example you are missing the parentheses and you are missing the left hand side of the comparison for the additional conditions.
function arrayCounter(randomness) {
if (typeof(randomness) === 'undefined' || typeof(randomness) === 'string' || typeof(randomness) === 'number') {
return 0;
} else {
return randomness.length;
}
}
Joe Moors
3,334 PointsSome of those code examples didn't insert right, but hopefully you get the picture.
The code challenge responder says "Bummer! You're missing a function named 'arrayCounter'" but I can't tell how or where I am not telling it that my function is named arrayCounter!
Joe Moors
3,334 PointsI can't believe I missed that for over an hour. Driving myself nuts. Got it now.
Thanks so much.
James Barnett
39,199 PointsThat's what jshint/jslint is for, checking for those syntax errors.
It's built into jsbin, there's an analyze JS feature in codepen, there's also a JSLint extension for notepad++.
Joe Moors
3,334 PointsSweet james - I have tinkered with JSfiddle before, but I'm not familiar with jshint or jslint. Call me silly but I'm hooked on Sublime, any such extension that you would know of for Sublime 2?
James Barnett
39,199 PointsYep, in fact the sublime package is available on the jshint.com
Also there's a jslint feature on jsfiddle as well.
Chris McKnight
11,045 PointsI am a fan of sublime as well, however, I haven't been a fan of using jshint/jslint. I have found it to be a nuisance when run against 3rd party libraries.
Joe Moors
3,334 Pointswhich libraries are you running and is it common or even possible to use more than one at a time? I haven't gotten to that point yet, still on the basics, but I will be there shortly.
Chris McKnight
11,045 PointsMostly 3rd party jQuery plugins. Some of them give warnings because they missing semicolons, etc.
Oliver Monk
1,867 PointsI also use SublimeLinter which seems to do a similar job.
James Barnett
39,199 PointsOliver Monk - SublimeLinter is a collection of various lint libraries including JSLint.
Joe Moors
3,334 Pointsu da man. Thank you.