Algorithm Book - Chapter 2 - Fundamentals - Part II Flashcards
What is a library?
A ibrary is a related set of functions that have been grouped together under a common name.
How would we call a function within a math library?
Example: Math.___ Math.____ Math. ___
Libraries can use values?
Yes, such as Math.PI
Calling the function is also referred to as running or ?
Executing the function
What is the javascript Math library? What does it contain?
A group of related functions and values related to Math that have been grouped together. random(), floor(), ceil(), trunc().
Why aren’t all the math library functions just included in JavaScript automatically?
Traditionally done for less common functions, so they can be excluded from certain minimized versions of a language
If I call Math.random(), what will it return?
It will return a decimal number from 0 to ALMOST one.
What do the following functions do? Math.floor, Math.ceil, Math.trunc, Math.round?
Math.floor - lowest integar value of a value (negs more neg, and positive less posit
Math.ceil - highest integer value of a value (pos more pos, and negs less neg
Math.trunc - eliminates all decimal places from a value
Math.round -
When do math.floor and math.trun not return the same value?
if the argument is a positive number, Math.trunc() is equivalent to Math.floor(), otherwise Math.trunc() is equivalent to Math.ceil().
What is the % operator? When is it useful?
Modulo is a companion operator to divide - think of it as ‘remainder’. Given two numbers, modulo divides the second number into the first umber an integer number of times, and returns the remainder.
How do you concatate strings in JavaScript?
You can use the + operator
What is you want a random integer as low as 51 and as high as 100?
Math.random() is ‘from 0 to almost 1’. Math.random() * 50, then, is ‘from 0 to almost 50’. Let’s turn those decimal ranges into integers: Math.trunc(Math.random()*50) i ‘50 possible integers from 0 tp 49. Let’s add an offset, so we start at 51: Math.trunc(Math.random() * 50) + 51 is perfect.
How to extract a digit?
Take a number and divide it by the digits you would like to extract. Then use Math.floor to get rid of the decimals then use the remainder by 10.
Variables that live longer than a single function call
If you declare a var within a function, it is created when entering and destroyed when exiting that function . For a var to stay alive after you leave, you must declare it OUTSIDE. That declaration will be called only once when the file is loaded- including any initialization done on that variable. This can be useful if you want functions to ‘remember’ values between successive calls to them. You should contain variables within a function when possible, but you can declare them outside if needed.