Algorithm Book - Chapter 2 - Fundamentals - Part II Flashcards

1
Q

What is a library?

A

A ibrary is a related set of functions that have been grouped together under a common name.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How would we call a function within a math library?

A

Example: Math.___ Math.____ Math. ___

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Libraries can use values?

A

Yes, such as Math.PI

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Calling the function is also referred to as running or ?

A

Executing the function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the javascript Math library? What does it contain?

A

A group of related functions and values related to Math that have been grouped together. random(), floor(), ceil(), trunc().

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Why aren’t all the math library functions just included in JavaScript automatically?

A

Traditionally done for less common functions, so they can be excluded from certain minimized versions of a language

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

If I call Math.random(), what will it return?

A

It will return a decimal number from 0 to ALMOST one.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What do the following functions do? Math.floor, Math.ceil, Math.trunc, Math.round?

A

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 -

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

When do math.floor and math.trun not return the same value?

A

if the argument is a positive number, Math.trunc() is equivalent to Math.floor(), otherwise Math.trunc() is equivalent to Math.ceil().

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the % operator? When is it useful?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you concatate strings in JavaScript?

A

You can use the + operator

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is you want a random integer as low as 51 and as high as 100?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How to extract a digit?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Variables that live longer than a single function call

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly