1 00:00:00,495 --> 00:00:01,920 Hi, how'd you do? 2 00:00:01,920 --> 00:00:03,740 Were you able to complete the challenge? 3 00:00:03,740 --> 00:00:05,060 Now I'll show you how I did it. 4 00:00:06,168 --> 00:00:10,740 In the random.js file, the first step is collecting a number from a user. 5 00:00:10,740 --> 00:00:13,050 So I started by declaring a variable and 6 00:00:13,050 --> 00:00:15,980 storing input from the user in the variable. 7 00:00:15,980 --> 00:00:18,920 You learned that the prompt method returns a string, 8 00:00:18,920 --> 00:00:21,800 even if someone types a number into the dialog box. 9 00:00:21,800 --> 00:00:26,360 So the value of inputHigh needed to be converted into a number type. 10 00:00:26,360 --> 00:00:30,770 I used the parseInt method to do that, and I stored the number value in 11 00:00:30,770 --> 00:00:35,400 a variable named highNumber to represent the highest possible random number. 12 00:00:35,400 --> 00:00:38,640 With this, I was able to generate a random number from 1 13 00:00:38,640 --> 00:00:40,320 to whatever the user typed in. 14 00:00:40,320 --> 00:00:44,060 I stored that in a variable named randomNumber. 15 00:00:44,060 --> 00:00:51,040 Remember, the Math.random method generates a number from 0 up to but not including 1. 16 00:00:51,040 --> 00:00:56,930 And it's usually a really long decimal value, like 0.522403, and longer even. 17 00:00:56,930 --> 00:01:02,832 So I multiplied the random decimal number by the top number in the range, 18 00:01:02,832 --> 00:01:05,267 or the number the user typed. 19 00:01:05,267 --> 00:01:09,625 So this right here is going to return a floating point or 20 00:01:09,625 --> 00:01:14,099 a number with a decimal in it, like 3.407394. 21 00:01:14,099 --> 00:01:19,055 So next, I used the Math.floor method to round that number down to 22 00:01:19,055 --> 00:01:24,466 the nearest integer, for example, from 3.407394 to just 3. 23 00:01:24,466 --> 00:01:29,702 And like earlier, I added 1 to Math.floor to make sure that the lowest 24 00:01:29,702 --> 00:01:34,868 number is 1 and the highest number is the value stored in highNumber. 25 00:01:34,868 --> 00:01:38,801 Without the +1, the random number range would be from 0 up to but 26 00:01:38,801 --> 00:01:42,968 not including the number stored in the highNumber variable. 27 00:01:42,968 --> 00:01:47,570 If the highNumber was 25, for example, the range would be from 0 to 24. 28 00:01:47,570 --> 00:01:52,320 So the +1 makes sure that you'll get a number from 1 to 25. 29 00:01:52,320 --> 00:01:55,800 Finally, I printed a message displaying the random number. 30 00:01:55,800 --> 00:02:00,787 I used the console.log method and provided it a template literal with the message 31 00:02:00,787 --> 00:02:05,020 randomNumber is a random number between 1 and highNumber. 32 00:02:05,020 --> 00:02:08,880 I used string interpolation to insert the value of randomNumber and 33 00:02:08,880 --> 00:02:11,840 the highNumber value supplied by the user. 34 00:02:11,840 --> 00:02:13,028 Let's see how it works. 35 00:02:13,028 --> 00:02:20,444 I'll preview index.html in the browser, type the number 15, and click OK. 36 00:02:20,444 --> 00:02:26,950 In the console, I see the message 15 is a random number between 1 and 15. 37 00:02:26,950 --> 00:02:31,361 I'll try another number, let's say 30. 38 00:02:31,361 --> 00:02:36,990 That displays the message 12 is a random number between 1 and 30, good. 39 00:02:36,990 --> 00:02:41,200 So hopefully you were able to complete all or most of the steps in this challenge. 40 00:02:41,200 --> 00:02:42,460 If not, that's okay. 41 00:02:42,460 --> 00:02:45,260 Why not start over and write it again without looking at my version? 42 00:02:47,320 --> 00:02:52,300 The random number generator works as expected when a user supplies a number. 43 00:02:52,300 --> 00:02:57,340 But the program does not work if the input is something like the word seven or 44 00:02:57,340 --> 00:02:59,030 if the input field is left blank. 45 00:03:00,670 --> 00:03:03,040 In the console, notice the NaN value. 46 00:03:03,040 --> 00:03:07,090 It says NaN is a random number between 1 and NaN. 47 00:03:07,090 --> 00:03:10,280 Earlier, you learned that NaN stands for not a number, and 48 00:03:10,280 --> 00:03:12,910 it's JavaScript's way of saying that it can't find a number. 49 00:03:12,910 --> 00:03:16,270 NaN is often the result of an incorrect mathematical operation. 50 00:03:16,270 --> 00:03:20,920 In this case, the parseInt method can't convert a string of letters or 51 00:03:20,920 --> 00:03:26,770 an empty string into a number that can be used here in Math.random times highNumber. 52 00:03:26,770 --> 00:03:30,760 So for the random number generator, it would be a good idea to first check 53 00:03:30,760 --> 00:03:34,450 if the input supplied by the user is a number. 54 00:03:34,450 --> 00:03:38,640 If it is, then generate a random number and print it to the console. 55 00:03:38,640 --> 00:03:42,220 If it's not a number, then print a message like, you need to provide a number, 56 00:03:42,220 --> 00:03:42,770 try again. 57 00:03:43,930 --> 00:03:46,740 To do that, I need to use a conditional statement, 58 00:03:46,740 --> 00:03:48,820 which you learned about in a previous course. 59 00:03:48,820 --> 00:03:52,530 A conditional statement can make a program react to different situations. 60 00:03:52,530 --> 00:03:53,960 I'll start with an if statement. 61 00:03:55,480 --> 00:03:58,870 And what should the condition be in this case? 62 00:03:58,870 --> 00:04:03,110 Remember, a condition evaluates to either true or false. 63 00:04:03,110 --> 00:04:06,610 The if statement executes the code between the curly braces 64 00:04:06,610 --> 00:04:09,570 if the specified condition is considered true. 65 00:04:09,570 --> 00:04:11,530 If the condition is considered false, 66 00:04:11,530 --> 00:04:15,980 another statement is executed, in an else clause, for example. 67 00:04:15,980 --> 00:04:20,880 So in this condition, I can check the value stored in highNumber. 68 00:04:20,880 --> 00:04:24,880 If highNumber is a true number value that I can use in my program, 69 00:04:24,880 --> 00:04:26,570 then generate a random number with it. 70 00:04:27,760 --> 00:04:32,403 Now I'll move the random number variable inside the if code block, 71 00:04:32,403 --> 00:04:35,183 as well as the console.log statement. 72 00:04:40,912 --> 00:04:42,299 NaN, or not a number, 73 00:04:42,299 --> 00:04:47,460 is a value that is always considered false when encountered in a condition. 74 00:04:47,460 --> 00:04:50,520 So if the value of highNumber is NaN, 75 00:04:50,520 --> 00:04:55,400 then the condition evaluates to false, and the code in the else clause runs instead. 76 00:04:56,610 --> 00:05:00,475 Here I'll use console.log to display a message that says, 77 00:05:00,475 --> 00:05:03,144 you need to provide a number, try again. 78 00:05:11,996 --> 00:05:14,020 And now I'll test my latest changes. 79 00:05:14,020 --> 00:05:17,070 I'll save my file, refresh the page. 80 00:05:18,950 --> 00:05:23,300 And this time type three, the word three cannot be parsed to a number. 81 00:05:23,300 --> 00:05:26,360 So the console outputs you need to provide a number, try again. 82 00:05:28,160 --> 00:05:32,840 This time I'll leave the input field blank, and I get the same message. 83 00:05:32,840 --> 00:05:35,125 But if I enter a number, like 12, 84 00:05:35,125 --> 00:05:40,830 the console outputs 4 is a random number between 1 and 12, perfect. 85 00:05:42,160 --> 00:05:45,770 To take this challenge even further, why don't you adjust the program so 86 00:05:45,770 --> 00:05:50,260 that it accepts two numbers from a user, a low number and a high number. 87 00:05:50,260 --> 00:05:55,413 And then generates a random number from the lowest number to the highest number, 88 00:05:55,413 --> 00:05:59,978 for example, a number that's at least 10, but no bigger than 25. 89 00:05:59,978 --> 00:06:02,142 In the next video, I'll show you my solution.