1 00:00:00,510 --> 00:00:03,866 Now we need to make sure that the getRandomNumber function 2 00:00:03,866 --> 00:00:06,405 receives two numeric values as arguments. 3 00:00:06,405 --> 00:00:08,390 And no other type of values. 4 00:00:08,390 --> 00:00:11,081 If the function is called with a string for example, 5 00:00:11,081 --> 00:00:14,031 then the program should display a custom error message. 6 00:00:14,031 --> 00:00:18,310 Inside the function add an if statement just above the return statement. 7 00:00:19,890 --> 00:00:22,270 But what should we test here? 8 00:00:22,270 --> 00:00:25,400 Well, we need to make sure that the two arguments passed to the function 9 00:00:25,400 --> 00:00:26,510 are numbers. 10 00:00:26,510 --> 00:00:28,150 There's a few ways we might do this. 11 00:00:28,150 --> 00:00:31,330 I’ll teach you a new method for testing number values. 12 00:00:31,330 --> 00:00:36,138 JavaScript provides a special function called isNaN, or is not number. 13 00:00:36,138 --> 00:00:39,597 That takes one argument and returns a Boolean value. 14 00:00:39,597 --> 00:00:44,310 It returns true if the value is not a number, and false if it is. 15 00:00:44,310 --> 00:00:49,920 For example, if you pass the string 'six' to the function, you get the value true. 16 00:00:49,920 --> 00:00:53,460 However if you pass a number, like six, to the function, you get false. 17 00:00:54,520 --> 00:00:58,354 So in the if statement, I'll first test that the lower argument 18 00:00:58,354 --> 00:01:01,201 passed to the function is a number, like this. 19 00:01:04,214 --> 00:01:07,339 In other words, if the lower value is not a number, 20 00:01:07,339 --> 00:01:11,190 then the code block with this if statement will run. 21 00:01:11,190 --> 00:01:13,330 But we want to test both arguments. 22 00:01:13,330 --> 00:01:17,430 If either is not a number, then we'll display an error. 23 00:01:17,430 --> 00:01:22,838 To test both you can use the logical or operator, by typing two pipe characters. 24 00:01:22,838 --> 00:01:25,740 Then add a test for the upper argument like this. 25 00:01:30,140 --> 00:01:35,130 So now if either argument is not a number, we can display a custom error. 26 00:01:36,610 --> 00:01:41,394 We could maybe use console.log to display a message in the console. 27 00:01:41,394 --> 00:01:43,637 Like 'Both arguments must be numbers.'. 28 00:01:50,160 --> 00:01:53,814 In this case, it's probably best to throw a custom error. 29 00:01:53,814 --> 00:01:57,770 So the programmer can immediately see that they've used the function incorrectly. 30 00:01:57,770 --> 00:02:01,670 JavaScript lets you create your own errors which print directly to the JavaScript 31 00:02:01,670 --> 00:02:04,630 console, just like regular syntax errors do. 32 00:02:04,630 --> 00:02:05,460 It looks like this. 33 00:02:06,590 --> 00:02:10,350 The throw keyword, followed by JavaScript's Error object. 34 00:02:12,300 --> 00:02:14,176 This is a built in keyword and 35 00:02:14,176 --> 00:02:17,780 object JavaScript provides to create a custom error. 36 00:02:17,780 --> 00:02:20,900 Or, as developers say, throw an exception. 37 00:02:20,900 --> 00:02:24,100 Since it's inside the if statement, the error is thrown 38 00:02:24,100 --> 00:02:27,870 only when the function receives anything but a number for either argument. 39 00:02:29,890 --> 00:02:33,570 All right, now I'll test the latest changes by calling the function correctly 40 00:02:33,570 --> 00:02:36,060 with numbers a couple of times. 41 00:02:37,080 --> 00:02:40,794 Then call it with a string value like 'three hundred'. 42 00:02:46,397 --> 00:02:47,960 I'll save the file. 43 00:02:47,960 --> 00:02:52,977 And over in the console, I get two random numbers, 4 and 17. 44 00:02:52,977 --> 00:02:56,819 And below them is the Uncaught Error, displaying my custom error message, 45 00:02:56,819 --> 00:02:59,070 Both arguments must be numbers. 46 00:02:59,070 --> 00:03:02,810 Notice how it looks just like a regular error thrown by the JavaScript engine. 47 00:03:02,810 --> 00:03:06,950 It even displays the line number producing the error, great. 48 00:03:06,950 --> 00:03:09,800 So now you've learned how to program defensively. 49 00:03:09,800 --> 00:03:14,650 Or looking out for problems that might arise in your program and taking action. 50 00:03:14,650 --> 00:03:16,440 As with many things in programming, 51 00:03:16,440 --> 00:03:19,960 getting comfortable with the details of functions takes time and practice. 52 00:03:19,960 --> 00:03:23,608 But once you get to hang of working with functions, you'll be well on your way to 53 00:03:23,608 --> 00:03:26,690 mastering one the most essential features of JavaScript. 54 00:03:26,690 --> 00:03:31,820 In fact, functions are often referred to as first-class citizens in JavaScript. 55 00:03:31,820 --> 00:03:35,044 Which means that you have the ability to do almost anything with functions. 56 00:03:35,044 --> 00:03:39,160 Like store them in variables, pass them as arguments to other functions. 57 00:03:39,160 --> 00:03:41,890 You can even set a function to return another function. 58 00:03:41,890 --> 00:03:45,170 This lets you take advantage of powerful design patterns you'll learn about in 59 00:03:45,170 --> 00:03:46,480 later courses. 60 00:03:46,480 --> 00:03:47,887 So you should feel excited and 61 00:03:47,887 --> 00:03:51,607 proud of reaching another important milestone in your programming journey. 62 00:03:51,607 --> 00:03:53,004 And remember, we're here to help. 63 00:03:53,004 --> 00:03:56,226 So if you have questions about anything covered in this course, you can always get 64 00:03:56,226 --> 00:03:59,590 in touch with the friendly Treehouse staff or other students in the community. 65 00:03:59,590 --> 00:04:01,037 Thanks everyone, and happy coding.