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 triallayeesolo
7,316 PointsReturn Value
Can anyone please explain to me what am I doing wrong in this 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==='string') || (typeof array ==='number') || (typeof array==='undefined')) {
return 0
}
else {return.arraylength;
};
}
//i understand it now ...just correcting myself
</script>
</body>
</html>
7 Answers
Ken Alger
Treehouse TeacherLayeesolo;
The post Hugo Paz mentioned covers things quite well, and you will notice that there are a few different ways of completing the challenge given what has been taught thus far. As one progresses through JavaScript there are multiple ways of achieving the desired results.
Post back if you are still stuck.
Happy coding,
Ken
Hugo Paz
15,622 PointsHi layeesolo,
Have a look here, it provides really good explanations regarding this challenge.
layeesolo
7,316 Pointsok,ty guys
layeesolo
7,316 Pointsi tried those methods still not working
Hugo Paz
15,622 PointsCan you post what you tried?
layeesolo
7,316 Pointsi just did
Hugo Paz
15,622 PointsI didnt notice that you updated your post.
function arrayCounter(array){
if (typeof array ==='string' || typeof array ==='number' || typeof array ==='undefined') {
return 0;
}
else {
return array.length;
}
};
layeesolo
7,316 Pointsopps forgot to add the function name. thanks a lot if (typeof array==='string') I understand it now
Ken Alger
Treehouse TeacherLayeesolo;
You can also break it out into a more verbose way of doing it, and getting the same results with if...else
statements and not just or
conditions. The or
conditionals work for the challenge requirements, but you would potentially have more flexibility with the if...else
. Again, this is based upon what we "should know" at this point, or at least I think if...else
statements were covered.
Something like:
function arrayCounter(x) {
if (typeof x === 'string') {
//do something magical here -or for this challenge, return 0-
return 0;
} else if (typeof x === 'number') {
//do something magical here -or for this challenge, return 0-
return 0;
} else if (typeof x === 'undefined') {
//do something magical here -or for this challenge, return 0-
return 0;
} else {
return x.length;
}
}
Both ways pass the challenge and, as long as your goal is to return 0
for each of the conditions, are functionally the same with the conditional coding option in the other post obviously being cleaner.
Just another approach to consider going forward.
Ken