diff --git a/02_Day/02_day_data_types.md b/02_Day/02_day_data_types.md index cb886b3e..dffeb443 100644 --- a/02_Day/02_day_data_types.md +++ b/02_Day/02_day_data_types.md @@ -20,6 +20,10 @@ - [Escape Sequences in string](#escape-sequences-in-string) - [Template Literals(Template Strings)](#template-literalstemplate-strings) - [String Methods](#string-methods) + - [Numbers](#numbers-1) + - [Declaring number data types](#declaring-number-data-types-1) + - [Math Object](#math-object-1) + - [Random number generator](#random-number-generator) - [💻 Day 2: Exercises](#%f0%9f%92%bb-day-2-exercises) - [Exercises: String Part](#exercises-string-part) @@ -653,6 +657,85 @@ let string = 'love' console.log(string.repeat(10)) // lovelovelovelovelovelovelovelovelovelove ``` +## Numbers + +Numbers are integers and decimal values which can do all the arithmetic operations. +Lets' see some examples of Numbers. + +### Declaring number data types + +```js +let age = 35 +const gravity = 9.81 //we use const for non-changing values, gravitational constant in m/s2 +let mass = 72 // mass in Kilogram +const PI = 3.14 // pi a geometrical constant + +//More Examples +const boilingPoint = 100 // temperature in oC, boiling point of water which is a constant +const bodyTemp = 37 // oC average human body temperature, which is a constant +console.log(age, gravity, mass, PI, boilingPoint, bodyTemp) +``` + +### Math Object + +In JavaScript the Math Object provides a lots of methods to work with numbers. + +```js +const PI = Math.PI +console.log(PI) // 3.141592653589793 +console.log(Math.round(PI)) // 3; to round values to the nearest number +console.log(Math.round(9.81)) // 10 +console.log(Math.floor(PI)) // 3; rounding down +console.log(Math.ceil(PI)) // 4; rounding up +console.log(Math.min(-5, 3, 20, 4,5, 10)) // -5, returns the minimum value +console.log(Math.max(-5, 3, 20, 4,5, 10)) // 20, returns the maximum value + +const randNum = Math.random() // creates random number between 0 to 0.999999 +console.log(randNum) +// Let create random number between 0 to 10 +const num = Math.floor(Math.random () * 11) // creates random number between 0 and 10 +console.log(num) + +//Absolute value +console.log(Math.abs(-10)) //10 +//Square root +console.log(Math.sqrt(100)) // 10 +console.log(Math.sqrt(2)) //1.4142135623730951 +// Power +console.log(Math.pow(3, 2)) // 9 +console.log(Math.E) // 2.718 + +// Logarithm +//Returns the natural logarithm of base E of x, Math.log(x) +console.log(Math.log(2)) // 0.6931471805599453 +console.log(Math.log(10)) // 2.302585092994046 + +// Trigonometry +Math.sin(0) +Math.sin(60) + +Math.cos(0) +Math.cos(60) +``` + +#### Random number generator + +The JavaScript Math Object has a random() method number generator which generators number from 0 to 0.999999999... + +```js +let randomNum = Math.random() // generates 0 to 0.999 +``` + +Now, let us see how we can use random() method to generate a random number between 0 and 10 inclusive. + +```js +let randomNum = Math.random() // generates 0 to 0.999 +let numBtnZeroAndTen = randomNum * 11 +console.log(numBtnZeroAndTen) // this gives: min 0 and max 10.99 +let randomNumRoundToFloor = Math.floor(numBtnZeroAndTen) +console.log(randomNumRoundToFloor) // this gives between 0 and 10 +``` + 🌕 You are awesome. You have just completed day 2 challenge and you are two steps a head in to your way to greatness. Now do some exercises for your brain and for your muscle. # 💻 Day 2: Exercises @@ -706,6 +789,11 @@ The quote 'There is no exercise better for the heart than reaching down and lift "Love is not patronizing and charity isn't about pity, it is about love. Charity and love are the same -- with charity you give love, so don't just give money but reach out your hand instead." ``` +32. Generate a random number between 0 and 100 inclusive. +33. Generate a random number between 50 and 100 inclusive. +34. Generate a random number between 0 and 255 inclusive. +35. Access the 'JavaScript' string characters using a random number. + 🎉 CONGRATULATIONS ! 🎉 [<< Day 1](https://github.com/Asabeneh/30DaysOfJavaScript/blob/master/readMe.md) | [Day 3 >>](https://github.com/Asabeneh/30DaysOfJavaScript/blob/master/03_Day/03_booleans_operators_date.md)