4.11 Exercise: Landscape or Portrait
Here is another exercise. I want you to implement this function. The function we're going to call landscape, it takes two parameters, width and height of an image, and it returns true if the image is in landscape format This means that the width is greater than the height, otherwise it returns false. So I invite you to pause the video to do this exercise and when you are done, come back and continue watching the correction So, if similar to the last video, we can start with a simple if and else. So if the width is greater than the height, we want to return true, otherwise we want to return false. But earlier I told you that in this case we can use the conditional operator So we add the condition if the width is greater than the height, we can return true, otherwise we will return false. And here is our return statement So here is the implementation of this function, but code like this looks a little ugly. we don't want to explicitly return true or false. Why not? Well, let me show you. We can remove this part completely here Just return the value of this expression. So if the width is greater than the height, this expression will evaluate to true. So, you will simply return true. Otherwise, if the width is less than the height, this expression will evaluate to false, And return false. So it's not really necessary to explicitly return true and false here, that's a bad way to write code. So now we have this function, let's test it. We can do a console. log, just call this function here landscape, we're going to pass its dimensions We're going to put 800 by 600. So we expect to be true on the console. Save the changes, and here is true, now let's change the width to 300. We have a vertical image on the console, so we get false on the console. That's it for this exercise on dimensions in JavaScript, let's meet again for a very next demonstration.