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 trialLi Xia
4,671 Pointsnaming functions.
''' function arrayCounter(array){ if (typeof array = 'undefined'){ return 0; } return array.length; } '''
is not parsing, error = missing a function arrayCounter.
What am I doing wrong?
7 Answers
Christian Oseguera
1,680 PointsHi, check the == This one seems to work:
function arrayCounter(array){
if (typeof array == 'undefined'){ return 0; } return array.length;
}
Li Xia
4,671 Pointsthis question is related to the code challenge. The actual question is:
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.
Can anyone see the problem with my code?
<!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'){
return 0;
}
return array.length;
}
</script>
</body>
</html>
Christian Oseguera
1,680 PointsWhere you typed < script > you have to type < script type = "text/javascript" >
Christian Oseguera
1,680 Points" < script type="text/javascript" > function arrayCounter(array){ if (typeof array == 'undefined' || typeof array == 'string' || typeof array == 'number'){ return 0; } return array.length; } < /script > "
Li Xia
4,671 PointsI tried < script type="text/javascript" > but it didn't make a difference.
Li Xia
4,671 PointsDoes anyone else have any ideas?
Christian Oseguera
1,680 PointsThis page works: (remove spaces after every < ).
< !DOCTYPE html> < html lang="en"> < head> < meta charset="utf-8"> < title>JavaScript Foundations: Functions< /title> < style> body { background: #FAFAFA; font-family: sans-serif; } < /style> < script>
function arrayCounter(array){
if (typeof array == 'undefined'|| typeof array == 'string' || typeof array == 'number'){
return 0;
}
return array.length;
}
< /script>
< /head> < body> < h1>JavaScript Foundations< /h1> < h2>Functions: Return Values< /h2> < script> var ay; var miArray = new Array(); miArray[0] = 290; miArray[1] = 330; document.write("<br>Array passed:" + arrayCounter(miArray)); document.write("<br>String passed:" + arrayCounter("Hi!")); document.write("<br>Number passed:" + arrayCounter(4)); document.write("<br>Nothing passed:" + arrayCounter()); < /script> < /body>
< /html>
Christian Oseguera
1,680 PointsChristian Oseguera
1,680 PointsCheck if you typed script type="text/javascript" before your function with the closing /script after it. If your function is loaded in a file, check it is properly loaded.