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
James Lister
6,569 PointsAround line 17, 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.
It keeps telling me that I'm missing a function names 'arrayCounter', what am I doing wrong? function arrayCounter(array) { if typeof array === 'undefined') { return 0; } return array.length; }
29 Answers
James Lister
6,569 PointsI needed to use "else if" & "else" and separate those values
Sean Templeton
12,983 PointsThere is an easier way to do this. What they were expecting, I believe, is to use the || statement to check with "or". You can, for bonus credit, just check if the parameter is an instance of an array. If it is, return the length. Else, return 0
loiclebunetel
4,184 PointsMuch easier :
function arrayCounter (a) { if (typeof(a) !== 'object') { return 0; } else { return a.length; } }
William Peterson
5,103 PointsI worked this out to be:
function arrayCounter (x){
if (typeof x === 'undefined' || typeof x === 'string' || typeof x === 'number') {
return 0;
} else
return x.length;
}
Alexander Stanuga
11,999 PointsThis one worked for me, thanks.
Alex Ignat
8,538 PointsWhat does | | do?
Thanks
Johanne Trippas
7,367 PointsHi Alex,
The || sign means 'OR'. This way you don't have to rewrite code. It is called a logical operator.
More info can be found http://www.w3schools.com/js/js_comparisons.asp
Alli Shea
4,178 PointsVery confused, still. When did we learn the use of ||? Why would we learn typeOf if what we really need here is instanceOf? Else was not mentioned in this lesson, either.
Jim Hoskins may be worth updating this video, or changing the challenge a bit!
Ian Blair
7,411 PointsThis code passed the challenge.
function arrayCounter (array) {
if (typeof array === 'undefined' ||typeof array === 'string'||typeof array === 'number') {
return 0;
}
return array.length;
}
James Jung
4,919 PointsI don't get it.
if (typeof array === 'undefined' || 'string' || 'number') {
return 0;
}
why doesn't this work either? do we have to check typeof for each?
CJ Williams
34,372 PointsThis is kind of a difficult lesson to follow along with.
Michael Thomas
10,910 PointsYea where did || come from? Can any body show us how this is done using what we've been taught up until this point instead of using instanceOf or ||
sakatagintoki
12,624 Points|| means 'or'
So
if(something happens || something else happens || something else else happens) {};
Patrick Jones
5,813 PointsI went with the instanceof operator to see if the argument was an array. Took me at least 20 minutes of googling to figure out a way to get this done. @Zen, you're totally right, feels AMAZING to figure it out by yourself.
function arrayCounter(array) {
if (array instanceof Array === true)
{
return array.length;
}
else
{
return 0;
}
}
Matthew Clark
7,255 PointsCan someone better define to me what instanceof does?
Sean Templeton
12,983 PointsWhat challenge are you on?
James Lister
6,569 PointsStage 5 functions Challege 1 of 1, ah I added the bracket and progress; now it says "wrong value returned from 'arrayCounter'("string")' it should be 0.
Sean Templeton
12,983 PointsYou are testing for undefined. Check what it asks you to look for.
James Lister
6,569 PointsAh ok, more progress, now I've got function arrayCounter(array) { if (typeof array === 'string', 'number', 'undefined') { return 0; } return array.length; }
James Lister
6,569 PointsBummer! If I pass in an array in to 'arrayCounter' of '1,2,3' I get 0 and not the number 3.
Sean Templeton
12,983 PointsLook at your if statement. What exactly are you comparing? Hint: JavaScript isn't THAT intuitive and efficient.
James Lister
6,569 PointsI'm lost, when I type "1, 2 ,3" it into the console I get a return value of 3?
James Lister
6,569 PointsOk I got it! Thanks heaps pal, you're a legend.
Tiffany McAllister
25,806 PointsCan someone please help me with this as well? What I have so far:
function arrayCounter(x) {
if (typeof x === 'undefined' || 'string' || 'number'){
return 0;
}
else {
return x.length;
}
}
Zen Hess
6,700 PointsTiffany, I was just stuck on this as well.
Make sure you double check what your parameter ought to be called.
Also, take a look at Mozilla's developer tools (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof) and see if this helps.
They are looking for the function to be able to return arrays only (or, almost only). So, it may be easier to think about this in terms of negation rather than equality (think !== rather than ===).
Sean Templeton's question "What exactly are you comparing?" is what helped me with this, although I'm not sure I went the way he was leading.
Hope this helps!
Tiffany McAllister
25,806 PointsAh! I got it! Thank you so much for your help :)
Zen Hess
6,700 PointsRad! You're welcome.
Christy Presler
7,887 PointsOk I have no idea what I am doing wrong. I'm getting the message "You're not checking if 'undefined' is being passed in"
function arrayCounter(array) {
if (typeof array !== 'undefined' || 'string' || 'number') {
return array.length
} else {
return 0;
}
}
Jason Taylor
3,059 PointsI think this would have worked if you added typeof !== for every or condition
Christy Presler
7,887 PointsIf I change to this
function arrayCounter(array) {
if (typeof array === 'undefined' || 'string' || 'number') {
return 0;
} else {
return array.length;
}
}
I get this message: If I pass in an array in to 'arrayCounter' of '1,2,3' I get 0 and not the number 3.
Christy Presler
7,887 PointsNever mind, I figured it out. Split up the if statement into three statements and don't use else between them, they aren't actually contingent on each other.
Zen Hess
6,700 PointsDoesn't it feel so gnarly when you figure it out yourself? Good job!
David Taber
Courses Plus Student 3,574 PointsAround line 17, 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.
function arrayCounter(myArray){
if (Array.isArray(myArray))
{
return myArray.length;
}
else if ((typeof myArray==="undefined")||(typeof myArray==='number')||(typeof myArray==="string"))
{
return 0;
}
}
Jordan Partridge
7,441 PointsSpent forever trying to use typeof for array, but an array is an object, so the function never worked right. Switched to using instanceof and it worked...
function arrayCounter (x) {
if (x instanceof Array) {
return x.length;
} else {
return 0;
}
}
Shane Riddle
11,087 PointsOMG... Took me atleast 30 minutes but!!!!
function arrayCounter (myArray) { if (typeof myArray === 'string' ||typeof myArray === 'number' ||typeof myArray === 'undefined') { return 0; } return myArray.length; } //I got it!
Dana Leventhal
Courses Plus Student 13,120 PointsTook a little bit but got it: function arrayCounter(myArray) { if(typeof myArray==='string'){ return 0; } if(typeof myArray==='number'){ return 0; } if(typeof myArray==='undefined'){ return 0; } else { return myArray.length;} } Hope this helps
erick Hernandez
7,060 Points function arrayCounter (array){
if (typeof array==="string"){
return 0;
}
if (typeof array==="number"){
return 0;
}
if(typeof array==="undefined"){
return 0;
}
else{
return array.length;
}
}
```
This worked for me and uses everything learned in this lesson.
Josh Gallagher
3,013 PointsHey Erik, instead of using multiple "if" statements you should use "if else" statements for clearer code or you should explore the logical operator " || " to make your code DRY and easier to read and understand. For example;
function arrayCounter(x) {
if(typeof x !== 'undefined' || typeof x !== 'number' || typeof x !== 'string')
return x.length;
} else {
return 0;
}
In the above example I use both logical operators " || " and " !== "; the first operator I have listed ( || ) means " or ", this can be used to slim down your conditional statements as shown above instead of using multiple statements. Also the second operator I have listed ( !== ) means " is not equal to the type "; again this can be used to check to see if the array isn't a number which means it will return the length and not 0.
Punal Chotrani
8,817 PointsHey guys,
This is the method i used, and seemed to have worked.
function arrayCounter(xyz){
if (typeof xyz === 'undefined'){
return 0;
}
if (typeof xyz === 'number'){
return 0;
}
if (typeof xyz === 'string'){
return 0;
}
else{
return xyz.length;
}
}
but after solving the problem, in order to write clean and readable code, i should have done it this way.
function arrayCounter(xyz){
if (typeof xyz === 'undefined' || typeof xyz === 'number' || typeof xyz === 'string'){
return 0;
}
else{
return xyz.length;
}
}
Both should anyways work, but it is always best to follow best practices.
Ali Amirazizi
16,087 PointsHere's the long way:
function arrayCounter(myArray) {
if (typeof myArray === 'string') {
return 0;
}else if (typeof myArray === 'number') {
return 0;
}else if (typeof myArray === 'undefined') {
return 0;
}else {
return myArray.length;
}
}
Michael Henry
14,601 PointsGreat discussion so far...
I understand how the most recent answer works, but what if "typeof myArray === boolean"? That would not be a string, a number, or undefined, so wouldn't the function try to return the code that is in the else statement (which would lead to an error because it is trying to determining the length of a boolean)?
Thanks, Mike
angel posada
6,284 PointsHey i just tried this code and works pretty smooth.
function arrayCounter (array) { if (typeof array === 'undefined' ||typeof array === 'string'||typeof array === 'number') { return 0; } else { return array.length; } }
Tibor Ruzinyi
17,968 PointsAnother option :
function arrayCounter(x){
if (Array.isArray(x) === true){
return x.length;
}else{
return 0;
}
}
Sean Templeton
12,983 PointsSean Templeton
12,983 PointsYou have a syntax error. Look at your if statement