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 trialRichard Zili
6,451 PointsIn an array, how does one pass a value of "string" or "number" without actually specifying a string or number?
This is in regards to the code challenge in the "Return Values" section of the JavaScript lessons
3 Answers
Kristen Law
16,244 PointsTo get the value type of a variable, you would use the typeof
operator before the variable.
Within your function, you want to find the value type of the parameter that is passed into it, so you would use typeof parameter_variable
and then check to see if it is equal to 'string'
, 'number'
, or 'undefined'
.
See below:
function arrayCounter(array){
if (typeof array === 'string' || typeof array === 'number' || typeof array === 'undefined'){
return 0;
}
return array.length;
}
Yannis Antonopoulos
Courses Plus Student 1,372 PointsThanks also by me!
Jose Guerra
Courses Plus Student 9,338 Pointsthanks :D
Richard Zili
6,451 PointsRichard Zili
6,451 PointsThanks for your help!