Skip to content

Latest commit

 

History

History
102 lines (58 loc) · 4.96 KB

recipe-random-number-generator.md

File metadata and controls

102 lines (58 loc) · 4.96 KB

Random Number Generator

const random = (max, howManyNums = 1) => {
  let randomArray = []; 
  while (howManyNums > 0){
    let randomNum = Math.floor(Math.random() * max);
    randomArray.push(randomNum);
    howManyNums -= 1;
  }
  if (randomArray.length > 1) {
  	return randomArray; 
  } else {
  	return randomArray[0];
  }
};

This recipe doesn't create a full program, just a very simple function to use in trying out the Node REPL.

Goals

  • Try out the Node shell

You should already understand

  • Very basic command line skills

     

Getting Started

Open a terminal program and, at the command prompt, type node -v. It should print the version number of Node installed on your system. If you don't get this, go back to the installation instructions and make sure you have Node installed correctly.

Screenshot of above instructions carried out

To open the Node REPL, type node.

Screenshot of instructions carried out. You should get a welcome message, a notice to type ".help" for info, and a prompt.

 

Coding in the REPL

You now have access to the Node environment. At the prompt, you can write JavaScript code, and it will execute when you press enter. Try it out by assigning a few variables and conducting some simple operations. For example, try typing const num1 = 10 and enter; then const num2 = 5 and enter; then num1 + num2. It should output 15. This is because the REPL saves your variables in memory for the length of your session. Once you exit the REPL, though, they're removed from memory and you'll start afresh next time.

Screenshot of above instructions carried out in the terminal

If you start entering a multi-line piece of code and press enter at the end of the first line, Node will realize you haven't, for example, closed the brackets of a function. It will give you a new line starting with three dots instead of evaluating your code immediately.

Screenshot illustrating this

Once you add the final bracket and press enter, it will save your function definition and return to the regular prompt.

Screenshot illustrating this

If you're seeing three dots and you didn't mean for that to happen, type .break to break out of them.

You can also enter code by opening the editor by typing .editor. This gives you a space to enter multiple lines of code.

Screenshot illustrating this

When you're done entering code, press ctrl+D to run it all at once.

Random Number Generator

With or without opening the editor, enter the recipe code at the top of this page and run it. It's a definition for a simple function which will return one or multiple random numbers. It accepts two arguments: a max number (meaning it will generate numbers between 1 and this number), and a quantity of random numbers to return, which defaults to 1.

Your function definition for random is now saved to the memory for this Node session. Call the function and pass in a max number and (optionally) a quantity of random numbers to generate.

Screenshow showing the function called twice. With the optional parameter it returns an array of random numbers; without it it returns a single number

And that's the Node REPL! You're not going to write complicated code in it, and it's a rather clunky editing environment, but it's handy for trying simple things out.

However, the real takeaway from this recipe is that you've just done the single most important thing that Node can do: run JavaScript outside of a web browser. Until Node was released in 2009, JavaScript code only worked inside of browsers, since the browser was what held the software that interpreted and ran JavaScript code. Node recreated all the necessary parts to run JavaScript elsewhere, giving us the freedom to use JavaScript for things other than creating the visible parts of websites, and making it a much more flexible and useful programming language.

Summary

  • The Node REPL lets you write and run JavaScript code in a terminal.
  • If you declare variables and functions, Node will remember them until you exit the REPL.
  • This is an example of JavaScript working outside of a browser.

 


Bonus recipes

  • Tinker in the Node REPL on your own!

 

Resources


Up Next