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 trialMark Crimi
4,653 PointsFunctions Challenge task 1 of 1
Can anybody supply the code for this challenge? I'm completely stuck. Thanks.
5 Answers
Guled A.
10,605 PointsThe Instructions state to create a function that takes in an input and returns 0 if it is either a string, a number or an undefined value. If the input is an array it should return it's length;
Step 1:
Create the function with a parameter. In this case I have chosen 'item'.
function arrayCounter(item){
//Empty function
}
Step 2:
Check to see if the 'item' is an array or not. I will use the typeof operator in order to identify a string, a number or a an undefined value. if the value is an array I will simply return it's length using the length method otherwise I will simply return the number 0.
//Final Function
function arrayCounter(item){
if(typeof item === "string" || typeof item === "number" ||typeof item === "undefined")
{
return 0;
}else
{
return item.length;
}
}
Hope this helped. Happy programming. Have a good day.
Mark Crimi
4,653 PointsWhat are these signs | | that you're using? I haven't learned these yet.
Guled A.
10,605 Points||
Stands for "OR".
Mark Crimi
4,653 PointsThis is probably why I'm having trouble with this. I haven't even learned this yet. I really appreciate your help. Thank you.
Guled A.
10,605 PointsNo problem.
Guled A.
10,605 PointsSo in short, the program, in english would simply state:
If the item's "type" is a string OR(||) a number OR(||) an undefined value then return 0.
Mark Crimi
4,653 PointsI really appreciate your help. Thank you man!