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
Joe Williams
4,014 PointsExtra credit help for functions badge
For the extra credit in learning JavaScript Functions, the assignment is to create a function that accepts three arguments (a, b, c), and if a is even print "even," and if it's odd print, "odd."
Easy enough.
var EvenOdd= function (a, b, c) {
if (a % 2==0) {
return (console.log("a is even"));
}
else {
return (console.log("a is odd"))
}
}
EvenOdd (3, 5, 10);
Besides the fact I don't understand the point of asking to define a, b, and c if we're only focusing on a, I decided to take it further and actual give a value to b and c. However, this didn't work.
var EvenOdd= function (a, b, c) {
if (a % 2==0) {
return (console.log("a is even"));
}
else {
return (console.log("a is odd"))
}
if (b % 2==0) {
return (console.log("b is even"));
}
else {
return (console.log("b is odd"))
}
if (c % 2==0) {
return (console.log("c is even"));
}
else {
return (console.log("c is odd"))
}
}
EvenOdd (3, 5, 10);
What am I doing wrong?
2 Answers
Michael Hulet
47,913 PointsIf by "it's not working" you mean "only a is evaluated", that's because the return value does is stop the execution of the function at the point it is encountered and send back whatever value is next to it. So what's happening is that execution is only evaluating the first if/else statement before exiting. I think this still passes the requirements of the first part, but will actually evaluate everything:
//I like defining functions with the "function" keyword, and I use camel case. That's a preference thing, so it's your choice if you want to use it
function evenOdd(a, b, c){
if(a % 2 == 0){
console.log("a is even");
}
else{
//Also, you were missing semicolons in all of your else statements. I fixed that
console.log("a is odd");
}
if(b % 2 == 0){
console.log("b is even");
}
else{
console.log("b is odd");
}
if(c % 2 == 0){
console.log("c is even");
}
else{
console.log("c is odd");
}
}
evenOdd (3, 5, 10);
Notice that the only real change (other than a little punctuation/spacing cleanup I did) is that all the return statements are missing. Now your function should run, but evaluate each parameter that's passed in
Rich Donnellan
Treehouse Moderator 27,741 PointsJoe,
Sweet job trying more than what was asked of you! Isn't this fun?!
I took a different approach that doesn't assume only a, b, and c arguments (use as many as you'd like). It loops through any arguments present and handles them accordingly. This took me a few to figure out, as I'm still learning myself, but I find this to be a more practical solution.
Whaddya think?
P.S. - an invaluable tool for Javascript execution/debugging is Remy Sharp's jsbin
function evenOdd() {
var num,
msg = "",
arg = arguments.length;
for ( var i = 0; i < arg; i++ ) {
num = arguments[i];
if ( num % 2 === 0 ) {
msg += num + " is even!\n";
} else {
msg += num + " is odd!\n";
}
}
console.log( msg );
}
evenOdd( 3, 5, 10 );
Joe Williams
4,014 PointsJoe Williams
4,014 PointsIf you removed every instance of "return" and it still works, what was the point of using it in the first place?
Michael Hulet
47,913 PointsMichael Hulet
47,913 Pointsreturnis used when you want to assign a value the function generates to a variable or compare it to something else. Let's take this function, for example:That's a super-primitive example function that will, as the name states, add two numbers together and
returnthat value. In other words, you could do something like this:That would cause
sumto be equal to 22. Let's say some stuff happens in the program, and we want to check ifsumis still equal toa + b. We could use this code to check, then log whether or not it is:However, when you're writing a function that does the same arbitrary thing every time, and there's no change that you'd ever need to use a value generated by it, there's no need to use any return statements. In fact, it can often be detrimental to the functioning of said function. For example, in the case of your original function. All you want to do with that is check whether a value is even or odd, then log that to the console. In that case, there's no value generated by your function, so there's nothing that should be returned.
Make sense?