Skip to content

Instantly share code, notes, and snippets.

@mrysav
Created May 1, 2018 20:44
Show Gist options
  • Select an option

  • Save mrysav/dbcc592bc7940e96b30f962fa5e51666 to your computer and use it in GitHub Desktop.

Select an option

Save mrysav/dbcc592bc7940e96b30f962fa5e51666 to your computer and use it in GitHub Desktop.
Practice with random numbers
'use strict';
// This practice exercise will cover:
// - For loops: For loops are useful when you know you want to run through a bit of code a fixed number of times.
// There are three parts to declare: the variable, the conditional, and the incrementer.
// They look like this:
// for (let i = 0; i < 5; i = i + 1) {
// ...your code here...
// }
//
// - Variables: Variables are like a "box" on the computer that you can put data in to access it later.
// In order to use a variable you must let the computer know that you want to make one. That looks like:
// let myVariable = "Hello world!";
// let myNewArray = [];
// let someJavascriptObject = {};
// After the computer knows about your variable, you can access it without the "let" keyword:
// myVariable = "New variable value!";
//
// - Functions: A function is a single unit of code that does a specific thing. Sometimes, but not always,
// functions need "parameters" or "arguments" to run. Parameters are special variables that the function
// needs to have in order to know exactly what you want it to do. Consider the following:
// goToStore();
// goToPlace("Store");
// What is the difference? Would it make sense to make the function call below?
// goToPlace();
// Note: You may be thinking: "Why not just make separate functions for everything? ie. goToWork(), goToSchool(), so I can avoid arguments altogether?"
// That is a valid way to solve your problem. But, you will find that it will make sense to make functions with arguments so you only have to write *one* function.
// A function looks like this:
// function writeMyNameToConsole() {
// console.log("Mitchell");
// }
// A function with one argument looks like this:
// function writeNameToConsole(someName) {
// console.log(someName);
// }
// A function with two arguments looks like this:
// function writeGreetingAndNameToConsole(aGreeting, someName) {
// console.log(aGreeting + " " + someName);
// }
// Functions can have as many parameters as you need them to, but generally you want to keep the number of parameters low.
// PRACTICE PROBLEMS
// 1. What are Math.random() and Math.floor()?
// A. Variables
// B. Functions
// C. Cats
// D. Numbers
// Answer:
//
// 2. How is this function different from Math.random()? How is it similar?
// Answer:
//
// 3. What do you think this function does? (Hint: Use a pen and paper to work it out like a math problem)
// Answer:
//
// Hint: if you do not know what Math.random() or Math.floor() are, look here:
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor
function getRandomInteger(maximum) {
return Math.floor(Math.random() * Math.floor(maximum));
}
// 4. Create a function called 'logArray' that takes one parameter named 'array' and has one job: log every member of the parameter 'array' to the console.
// 5. Create an empty array variable called 'tenRandomNumbers'
// 6. Generate 10 random numbers, using the function getRandomInteger(maximum) and push them into the array created above. Set the maximum random number to 1000.
// 7. Similar to above, create an empty array variable called 'millionRandomNumbers' and fill it with 1000000 random numbers.
// 8. Examine your answers for 5, 6 and 7. What similarities do you see in your code?
// Answer:
// 9. Usually, a function is created when you have some bit of code that you find yourself repeating in multiple places. In this case, we are
// repeating almost all of the "for" loop to generate random numbers and push them to a list.
// Create a function called 'createArrayOfRandomIntegers' with the following specifications:
// 1. The function should take one parameter called "arraySize"
// 2. Create a new, empty array variable named anything you want
// 3. Generate an amount of random integers equal to "arraySize" and push each one to the array created above
// 4. Return your new array
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment