1 00:00:00,512 --> 00:00:04,300 All right, now I'll walk you through how I would write the random number function. 2 00:00:04,300 --> 00:00:08,200 You learned how to create a random number between two values in a previous course. 3 00:00:08,200 --> 00:00:11,760 The difference is that now you're using a function that accepts arguments. 4 00:00:13,300 --> 00:00:17,155 For starters, I'll create a function named getRandomNumber. 5 00:00:19,040 --> 00:00:20,890 I'm using a function decoration, but 6 00:00:20,890 --> 00:00:25,100 you could have written this as a function expression or as an arrow function. 7 00:00:25,100 --> 00:00:29,210 The function should produce a random number value between two numbers. 8 00:00:29,210 --> 00:00:31,810 So I need to add two parameters to the function. 9 00:00:31,810 --> 00:00:34,190 I'll call them lower and upper. 10 00:00:36,190 --> 00:00:40,210 Next, I'll move the random number code into the function and 11 00:00:40,210 --> 00:00:41,990 place the parameters where they need to go. 12 00:00:43,600 --> 00:00:50,994 The upper value goes here, and the lower value should go here and here. 13 00:00:55,076 --> 00:00:59,804 I'll assign the result to a variable named randomNumber, 14 00:01:01,701 --> 00:01:04,060 Then return the value of randomNumber. 15 00:01:08,601 --> 00:01:11,035 You could make this code even more compact. 16 00:01:11,035 --> 00:01:14,630 You don't need to assign the formula to the variable, 17 00:01:14,630 --> 00:01:16,674 and then return the variable. 18 00:01:16,674 --> 00:01:20,167 Because the random number generating code produces a value, 19 00:01:20,167 --> 00:01:22,527 you can return its result, like this. 20 00:01:27,543 --> 00:01:29,640 Let's look at why we can do this. 21 00:01:29,640 --> 00:01:33,697 This code accepts information, the lower and upper values, 22 00:01:33,697 --> 00:01:38,309 and runs it through a couple of JavaScript methods and does some math. 23 00:01:38,309 --> 00:01:43,540 All of this code evaluates, as programmers say, to a single value, a random number. 24 00:01:44,540 --> 00:01:47,090 All a return statement does is return a value. 25 00:01:47,090 --> 00:01:49,170 It doesn't matter where that value comes from. 26 00:01:49,170 --> 00:01:52,900 It can be stored in a variable, or it can be the result of another function, or 27 00:01:52,900 --> 00:01:55,610 a mathematical operation, as it is in this case. 28 00:01:57,260 --> 00:02:00,492 All right, now to test this, I'll call the function a few times and 29 00:02:00,492 --> 00:02:02,146 print the results to the console. 30 00:02:05,344 --> 00:02:11,586 First, I'll call getRandomNumber, passing it the values 1 and 6. 31 00:02:13,008 --> 00:02:17,527 Below that, I'll call getRandomNumber again, 32 00:02:17,527 --> 00:02:20,977 this time passing it 10 and 100. 33 00:02:20,977 --> 00:02:26,989 And I'll call getRandomNumber a third time, passing it the values 200 and 500. 34 00:02:29,322 --> 00:02:34,006 I'll save my file, and over in the console, 35 00:02:34,006 --> 00:02:37,719 I get three random numbers, good. 36 00:02:37,719 --> 00:02:41,465 So now I can use my random number function in all sorts of ways. 37 00:02:41,465 --> 00:02:44,515 For example, display it in a message, like this. 38 00:02:56,130 --> 00:02:57,980 Remember in a template literal, 39 00:02:57,980 --> 00:03:02,744 you can include any JavaScript expression between the dollar sign and curly braces. 40 00:03:02,744 --> 00:03:05,422 And its value will be inserted into the string. 41 00:03:05,422 --> 00:03:09,594 By expression I mean any code that evaluates to a single value, 42 00:03:09,594 --> 00:03:14,490 like a variable, math operation, or in this case, a call to a function. 43 00:03:14,490 --> 00:03:20,989 In the console, we see the message, 41 is a random number between 10 and 100. 44 00:03:23,754 --> 00:03:26,333 You might've added a default parameter to your function too. 45 00:03:26,333 --> 00:03:34,295 For example, set the upper parameter to a default value of 100. 46 00:03:42,393 --> 00:03:46,050 The getRandomNumber function is working as expected. 47 00:03:46,050 --> 00:03:50,540 However, there's at least one situation in which the function won't work. 48 00:03:50,540 --> 00:03:54,860 The two arguments passed to the function must be numeric values. 49 00:03:54,860 --> 00:03:59,530 If you, for example, call the function with a string like six, 50 00:03:59,530 --> 00:04:03,830 instead of the number 6, the function will not work. 51 00:04:05,240 --> 00:04:06,630 Notice the value NaN. 52 00:04:07,730 --> 00:04:12,130 Part of being a good programmer is looking for ways that your programs might break 53 00:04:12,130 --> 00:04:14,990 and then coming up with a plan to handle those problems. 54 00:04:14,990 --> 00:04:20,050 To fix this, we can use an if statement to make sure that both arguments 55 00:04:20,050 --> 00:04:25,450 are numbers and then display an error if one or both of the values are not numbers. 56 00:04:25,450 --> 00:04:27,480 In the next video, we'll implement the solution.