In exercise nine, we're going to have you work with an arrow function. Here's the assignment, create an arrow function that will accept a number and return that number with an indication of whether it is odd or even. Now, an extra challenge with this exercise is see if you can define the entire function on a single line. And to do that, you'll need to use the shortened conditional operator the question mark and colon. pause the movie, give that a try and start it up again when you're ready to go through it. All right, we're going to create an arrow function that returns whether a number is odd or even, and we're going to try to do it on a single line.
That extra bit of challenge causes you to think about what is possible with JavaScript different ways to use the things you've already learned in JavaScript. So since it is an arrow function, I'll be creating a function expression. and name the variable odd even set it equal to imprint inside a parentheses. This is the variable that will be passed in. Now remember when we talked about arrow functions, since there's only one parameter to accept the argument which is passed in, technically, I wouldn't have to use the parentheses. Those are not required in this situation.
However, I simply prefer the parentheses for arrow functions, because it makes it more readable. I can quickly tell what it is when the parentheses are left out. Sometimes I have to look at it for a little bit before I can tell so that's the reason I include it. Now the arrow And now let's start our statement which is inside of this function is going to return what we want. So first we want to do a comparison. And that comparison is going to be of num modulo two.
Want to do that first, and then see if that is equal to zero. Now if it's equal to zero, then we know it's even. So in that case, we will return num. Remember, I don't have to use the return keyword, because it is an arrow function on a single line. Whatever the results are, that's what gets returned. And so if non modular two is equal to zero, the results will be what follows the question mark.
So I'll simply return the non concatenated to the phrase is even. Otherwise it will return what follows the colon. All right, there is our arrow function on a single line. Now notice in the function, I did not log to the console. Once again important concept about functions, we simply want to return the value, we want to return the results of what that function is supposed to do, then the programmer can reuse that function, and they can decide what they want to do with the value if we simply want to log it to the console, which is what we do with all of our exercises here. Then we go ahead and log into the console.
In real situations, you're not going to be logging things to the console, you're going to be using it in other parts of the program. All right, alright, let's go ahead and test this. So console dot log. Call odd even. And I'm going to pass in six. See what that is.
And then I'll do another one. Odd even again, and I'm going to pass in seven this time. So we'll have an example of both types. Save it. Let's run this in the browser so we can see what the JavaScript returns open up the console. Six is even seven is odd.
So jump back to the arrow function one more time, just a quick review. So here's where the function starts right at this point. Prior to that, we're simply establishing a variable that is going to contain the function. This allows us to pass in a number. This defines it as a function and here's the statement we have inside the function We we first, divide num by two getting the remainder, we then check to see if that remainder is equal to zero if it is equal to zero, we return this. If it's not equal to raise it to zero, we return that.
Hopefully you're able to solve that even if you weren't able to solve it on a single line. If you're able to figure it out, I would call that successful. That concludes our section on functions. Let's move on to the next section.