8. Working with Numbers Público

8. Working with Numbers

Kostas Diakogiannis
Curso por Kostas Diakogiannis, actualizado hace más de 1 año Colaboradores

Descripción

An introduction to the math object module, and it's functionalities.

Información de los módulos

Sin etiquetas

Contexto

Course's objectives   1. To learn how  to make math calculations faster by using  the math module's functions. 2. To learn how to create a random number, within a given range just between 0 and 1. 3. To round a number to it's nearest integer with the round, floor or ceil methods 4. To convert a string into a number by parseInt or parseFloat methods. 5. What is the spread operator and why should we care?
Mostrar menos
Sin etiquetas

Elemento multimedia

Contexto

The Math Object The Math object is a native Object that is given to us by JavaScript in order to deal easier with math operations. It contains methods that we can use when working with numbers or with lists of numbers and thus we don't need to re-invent the wheel every time. Let's see some of this functionality in the following lectures. For full documentation and purpose, take a look here!
Mostrar menos
Sin etiquetas

Contexto

Math Functions Some of the commonest functions used with the Math object are: 1. Math.random() => This returns a random number between 0 and 1. ex. 0.58346. It is heavily used for generating random ticket numbers, lottery, and other equivalent stuff. 2. Math.round(x) => rounds the decimal number X to the closest integer. ex. 0.5678 gets 1. 3. Math.ceil(x) =>  rounds the decimal X to the next integer. ex. 4.3 becomes 5 4. Math.floor(x) => rounds the decimal X to the previous integer. ex. 4.9 becomes 4. 5. Math.pow(x, y) => returns the result of X to the power of Y. ex Math.pow(10,3) = 1000 6. Math.sqrt(x) => returns the square root of number X. 7. math.abs(x) => returns the absolute value of the number X 8. Math.min(x, y, z) => returns the lowest number of a list of numbers.  9. Math.max(x, y, z) => returns the biggest number of a list of numers   For full documentation list of functions see always here:
Mostrar menos
Sin etiquetas

Elemento multimedia

Contexto

M-1 Create a random quote generator Create 10 objects that contain quotes or sayings as strings from famous peoples, and the names for every person who has credit for this quote, and store each object into an array. ex. let quoteDatabase = [    {author: 'Jesus Christ',  quote: 'Ask and you shall receive'},    {author: 'Julius Caesar', quote: 'Veni, Vidi, Vici'},    {author: 'Tony Montana', quote: 'Say hello to my little friend!'} ] etc. Then create a function that any times it executes selects a random quote and prints the quote and the name of the man who said the quote after it as a credit. ex. 'Ask and you shall Receive, Jesus Christ'.
Mostrar menos
Sin etiquetas

Elemento multimedia

Contexto

The Spread operator Spread operator splits all the contents of a collection (could be either array or object) into a series of individual elements. Thus they can be put inside functions as arguments or as elements to pollute other arrays. For example:  const testScores = [20, 25, 35]; Imagine you have a function that accepts three arguments and returns the biggest number of them. For example: function findBiggest(firstNumber, secondNumber, thirdNumber) { } If you put the testScores there as a argument when calling the function you get an error, because the function expects three arguments, and testScores is a single variable (an Array!). What you can do is to SPREAD these values the testScores array contains and assign them separately to each argument. You can do it by spreading these values out of the array. How: By using the three dots syntax that spreads them out. So when calling the function instead of doing it by index like : findBiggest(testScores[0], testScores[1], testScores[2]). This is going to work but is inneficient. #dont_do_that Alternatively you can do: findBiggest(...testScores) .This will individually take EACH value of the testScores and assign it to each function's argument.  It is equivalent to typing: findBiggest(20, 25, 35); This is very useful when you want to find the max or min of an array because JavaScript provides already function that accept a sequence of numbers and not an array. If you already have everything inside an array you can spread all the values as you saw. Note: It is a new ES6 feature. Check documentation here!
Mostrar menos
Sin etiquetas

Elemento multimedia

Contexto

M-2 Find the biggest Number in an Array of Strings An error has occurred and you have found yourself with data of 10 students result tests that are of string data type. So the results you have is a list of : ['63', '55.5', '72', '77',  '35.5', '81', '95', '29.5', '44.5',  '80'] Find the biggest and the smallest result in this list and print the margin between them by printing the absolute value of the difference between them.  Hint; Find a way to to convert each string into a number, or float. Check again parseFloat and parseInt functions to help yourself. Hint: Use the Math.max and Math.min methods and pass the spread version of the array as an argument. Either way, round the floated numbers into the biggest nearest integer. You may have learned some functions in the previous lectures  that could be helpful.
Mostrar menos
Sin etiquetas

Elemento multimedia

Contexto

M-3 Create a Lottery Create a lottery that creates a  random number from 1 to 100. This is the lucky number. Create a random number from 1 to 100 for the user. This is users number. If the user's number is the lucky number then the user wins 100$. If the absolute difference between user's number and the lucky number is less or equal to ten, then the user wins 100$ minus 10$ for every number in between.  Example: The lucky numbers is 85 and the usersNumber is 77. The absolute difference is 8, so the user wins 20$ (100 - 8 * 10). If the usersNumber is 84, the user wins 90$ and so on. In any other case the user wins nothing. Print the lucky number, the user's number and the winnings of the user.  Extra bonus: Run this lottery 100 times and calculate how much money have you given to winnings.
Mostrar menos
Sin etiquetas

Elemento multimedia

Contexto

M-4 Create a random color Create a random color by creating three different random number integers from 0 to 255. Store them into an array.  Then try to paint the background body of your document with the combination of colors you created by putting the randomly generated numbers into an rgb() function as arguments.   hint: Check the document.body.style.background = 'rgb(three numbers here)'
Mostrar menos
Sin etiquetas

Elemento multimedia

Contexto

M-5 Pick a random food to eat Are you hungry but you don't know what to eat for today? Create an array with 7 different favorite foods of yours. ex. favFoods = ['bananas', 'rice', 'chocolate', 'pizza', 'mett', 'bread', 'onions']; Create a function that picks randomly one of this seven foods and prints it to the console. Bonus: Create a function that actually selects randomly one of these for every day of the week and  prints: 'On Monday i will have bananas' 'On Tuesday i will have pizza' 'On Wednesday i will have onions' etc. hint: You may want to create another array to hold there, the days of the week. Also when a food is selected it cannot be reselected, that means you need to remove it from the array. Maybe the splice method that any array provides will help in this case. (Check the array methods inside the array section). arr.splice(which index to start removing, how many elements)
Mostrar menos
Sin etiquetas

Elemento multimedia

Contexto

M-6 Create a random Number within a range Create a function that accepts a low number and a high number and creates a random number between these two thresholds. ex. createRandom(1, 10) creates an integer between 1 and 10. Make this re-usable with many combinations to be sure that it works. At the end create a number between 50 and 55 for example.  The final random number should be an integer.
Mostrar menos
Mostrar resumen completo Ocultar resumen completo