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
Nick Lohr
2,973 PointsCode Challenge 1.1 Functions; Syntax for an array as a parameter in a function?
This is the challenge: 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.
am i on the right track with this so far??
function arrayCounter = (["a", "b", "c", "d"]) { return arrayCounter.length }
John W
21,558 PointsThis is just how the question in the code challenge defines the function's behavior. You can define your function differently to return the length of the string if one is passed in. But with that said, javascript treats string (a primitive type) and array (an object) differently.
sdfasdfagasdgasdg
475 PointsYes - I know that. The problem is the name of the function / how the task is worded. Even if "string" is a primitive type, it still has a length and it is still an array internally. You can even get the individual characters using the [n] operator, just like you would for any other array. typeof "Test" does return 'string' but it still follows many of the same rules as any other array.
4 Answers
John W
21,558 PointsHere's a step-by-step break down. Let's start with the basic function definition syntax. There are several ways to define a function in Javascript, but here is one:
function arrayCounter( ){ }
We want to give it a parameter:
function arrayCounter( someArray ) { }
Then we check to see if it's an array or not
function arrayCount ( someArray ) {
if ( someArray instanceof Array ) {
// it is an array! return something
} else {
// it is NOT an array! return something else
}
}
Finally you fill in the inner bracers to return the proper values as instructed by the code challenge. Hope this helps.
Alternatively, if you want to be literal about answering the code challenge question, you can test for equivalence (==) using typeof as Michael suggested. (Edit: Since he redacted his code, here's something similar to what he was going for)
function arrayCount (someArray) {
if ( typeof someArray == "string" ||
typeof someArray == "number" ||
typeof someArray == "undefined") {
// if a string, number, or undefined is passed in, you return ...
} else {
// otherwise, let's just assume it's an array and return ...
}
}
terrymartin
2,343 PointsHi John. I'm curious, why did I need to instance typeof 3 times? Is it just a quirk with the use of typeof and || ?
John W
21,558 Points== can only test 1 equality at a time. This is, for what it's worth, a fundamental property of ALL computations. If you want to ask "is this any of that," in code, most of the time you have to ask "is this A or is this B or this C or ...." Even constructs providing test-all functionalities are simply abstracting this breakdown in the background. There's no way around it.
Oliver Monk
1,867 PointsI might have missed something but typeof and | | weren't discussed in the previous tutorials at all...
John W
21,558 PointsThey might not be discussed by the videos... the content of the videos does not reflect the full language set and I was honestly just answering your question based on what I know, not limited to what the course taught.
The double bar || is the or operator, it means only one of the conditions need to be satisfied for the whole thing to be true. So (1==2 || 3==3) and (5==5 || 2==2) evaluate to true, but (1==4 || 3==5) evaluates to false.
The typeof keyword lets you know what type of your non-object variable is with limitations: See http://javascript.info/tutorial/type-detection
Oliver Monk
1,867 PointsI just meant as a lot of people here such as myself are pretty new to this language it might be a bit confusing but thank you for the explanation!
John W
21,558 PointsYea, sorry about that. It's sometimes hard for me to remember what's taught in which class at which stage. Though these two operations (||, typeof) are both very common in all high-level languages, so they are good to know anyway.
Aaron A.
4,094 PointsThe thing that threw me on this challenge is the keyword 'object'. You don't check if typeof is an 'array', you check if it's an 'object'. Thanks Google!
Soodeh Gholamzadehdevin
3,343 PointsThanks Aaron.A
Soodeh Gholamzadehdevin
3,343 PointsThanks everybody your answers helped me too!
sdfasdfagasdgasdg
475 Pointssdfasdfagasdgasdg
475 PointsWhy does the function need to return 0 if a string is passed in? A string is simply an array of characters and thus qualifies as an array.