Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                
              
            
!

CSS

              
                
              
            
!

JS

              
                ///////////////////////////////// Math Methods ////////////////////////////////

// The JavaScript Math object has a lot of useful methods that are worth mentioning.

// To use the Math methods, we don't have to construct a new Math object.

// All we do is include the Math object name, and then invoke the method, such as:

let roundUp = Math.round(9.8); // here we invoked the round method. This would round 9.8 to 10.

// There are a few constants loaded into the JS Math object's properties.

// A few of these include:
// 1. PI : Math.PI

// 2. Euler's constant : Math.E

// 3. Natural log base 10: Math.LN10

// More : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math

// The Math object also has many methods, but we will only cover the ones you will likely use most

// If you'd like to see a full list of the Math object's methods, go to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math

// 1. Exponents:
// Syntax: Math.pow(x,y) where x is the base and y is the exponent
// Examples:
let value = Math.pow(5, 2);
let newValue = Math.pow(value, 2);

// 2. Random:
// Syntax: Math.random()
// When Math.random() is used without any arguments, it returns a random floating point value between 0 and 1:
// Examples:
console.log("Here is a random number " + Math.random());
// However, we can specify the values we want the random number to be within range of using the next method:

// 3. Floor:
// Syntax: Math.floor()
// Math.floor() is used to return the largest whole number that is less than or equal to a specified value. We use this method to get a random whole number within a specific range of numbers, such as 1-10:
// Examples:
let randomNum = Math.floor(Math.random() * 10) + 1 ;
// the syntax of using .floor with .random is:
// Math.floor((Math.random * maxRangeValue) + minRangeValue)
console.log(randomNum);
console.log(
  "A new random number between 1 and 10 is " +
    Math.floor(Math.random() * 10) + 1 
);
console.log(
  "Another random number between 1 and 10 is " +
    Math.floor(Math.random() * 10) + 1 
);

// 4. Round:
// Syntax: Math.round(x) rounds the value x to the nearest whole number
// Examples:
let roundMeUp = Math.round(5.7);
let roundMeDown = Math.round(7.3);

// 5. Exponential Function:
// Syntax: Math.exp(x) raises E to the x power
// Examples:
let raised = Math.exp(5);

// 6. Absolute Value:
// Syntax: Math.abs(x) returns the absolute value of the variable or value in parenthesis
// Examples:
let positive = Math.abs(-24343525);
let negative = -24124214;
let absoluteVal = Math.abs(negative / 2);

// 7. Square Root:
// Syntax: Math.sqrt(x) returns the square root of the value or variable in parenthesis
// Examples:
let sqrt = Math.sqrt(25);
sqrt = Math.pow(sqrt, 2);
console.log(Math.sqrt(sqrt));

// 8. Trigonometric Functions:
// Syntax:
// Sin : Math.sin(x)
// Tan : Math.tan(x)
// Cos : Math.cos(x)

// Arcsin : Math.asin(x)
// Arctan : Math.atan(x)
// Arccos : Math.acos(x)

// Though there are a number of methods for the Math object that we didn't cover, you likely won't need to use them as a beginner programmer. Still, you can read about them here:
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math
// and:
// https://www.w3schools.com/js/js_math.asp

///////////////////////// Exercises /////////////////////////
//1. Declare a new variable named lottery and assign it to a random number between 1 and 3 using the floor and random methods

//2. Write a conditional statement that checks if the value of lottery is equal to 2
// If so, we win the lottery!
// Print the statement to the console with a new random number between 1 and 5 million:
//"You won the lottery! You won $___________"
// Fix the winning value to 2 decimal places

//3. Use the square root function to print the square root of 128,164

//4. Print the absolute value of (-351561 * 15) / 8 to the console

// Declare a new variable named income that is equal to 2500. Declare 4 variables: groceries, rent, utilities, savings. Store varying numeric values in these variables. Ensure at least 2 of the values contain decimal points. Update the 'income' variable to reflect the amount of income left after the four values are 'paid' and therefore subtracted from the income. Write a conditional statement that prints "You have $_____.__ left this month to spend on whatever you want!" if the left over income value is greater than $50.50. Otherwise, print "You should save your $____.__ in case of an emergency this month." Ensure that wherever a monetary value is being printed, the value has two decimal places.

//////////////////////Extra Exercises:

//1. Create two different variables storing two different numbers. 
// Find out the value of one of the numbers raised to the other number, using the variable names and the Math.pow() method. 

//2. Create a new variable that stores a random number between 1 and 100. Print out your variable. 
// Create a second variable that stores the square root of your random number. Print out your second variable. 
// Print out your second variable rounded to the nearest whole number.
// Print out your variable as a number with 3 digits.

//3. Create an array that stores 5 numbers.
// In a new variable, multiply the first element of your array by PI (use Math.PI!). 
// Print it out with 3 decimal places using the toFixed method 
// Print out the value of 5 to the power of 4. 
// Print out a random number between 1 and 10 using Math.floor and Math.random. 



              
            
!
999px

Console