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
Tracey Dolby
5,638 PointsHelp understanding arguments and return values
This is the question i'm not understanding. Why is the answer the way it is. The videos before don't go through this example for me to know how to answer this without going to the forum. If someone can elaborate why the || are used and why the code works this way. Any help is appreciated. Thank you
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.
<!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(array); {
if(typeof array === 'undefined' || typeof array === 'string' || typeof array === 'number') {
return 0;
}
</script>
</body> </html>
3 Answers
Christian Lüthy
18,715 PointsThe || is a Logical Operator. If either one of these results are true, the if statement executes. It's the "or" Operator.
function arrayCounter(array); { if(typeof array === 'undefined' || typeof array === 'string' || typeof array === 'number') { return 0; }
other Logical Operator are:
- && -> and
- ! -> not
Jonathan Grieve
Treehouse Moderator 91,254 PointsI'm not sure which part of the JS code is returning the length of the array, but it looks like what's happening is that the array is being passed into the function as an argument, which is basically a variable, being made available to a function.
This will typically then be a value that can be returned back.
What the if condition is doing is checking if the array that's been passed in is either undefined, so not passed in properly, whether it's a string array or a number array.
Hope this helps. :-)
By the way I fixed the code markdown for you. You can check out out to use the markdown code in forums from the link below. :-)
Tracey Dolby
5,638 PointsSo why isn't the .length use in the code at all. The question says the function must return the length of an array passed in or 0..... How is function: arrayCounter(array); { if(typeof array === 'undefined' || typeof array === 'string' || typeof array === 'number') { return 0; } going to give the length? All it's going to show is 0 for a string, undefined or number value. I don't understand how this will calculate the length. Am I missing something?