Javascript Flashcards

1
Q

Code for

Exponent or Power

A

(**) or Math.pow(baseNumber, powerNumber)
Example: 2 ** 5 = 32

For squared exponent or power will always be 2
(6 ** 2), (20 ** 2), (3 ** 2)

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

Find remainder

A

(%)
example: 11 % 2 = 1

finds the remainder
(Since 2 goes into 11, 5 times, the remainder is 1)
If using % 2 and remainder is 1, number is odd. If remainder is 0, number is even

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

Sum (Plus-equals) (Increment)

Minus-equals (Decrement)

Product (Times-equals) (Multiplier)

Divide-equals (Divider)

A

Sum (+=)
Example: 11 += 2 = 13

Decrement (-=)
Example: 11 -= 2 = 9

Product (*=)
Example: 11 *= 2 = 22

Divider (/=)
Example: 12 /= 2 = 6

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

Find Absolute Value

A

Math.abs()

Example: Math.abs(-295) = 295

Measures distance from 0
Answer will always be positive

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

Round Up

A

Math.ceil()

Example: Math.ceil(33.7) = 34

Rounds up

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

Round Down

A

Math.floor()

Example: Math.floor(33.7) = 33

Rounds down

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

Code for

Parsing an integer from a string

A

Number.parseInt()

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

Code for

Parsing a float from a string

A

Number.parseFloat()

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

Generate a random Number

A

Math.random() * (max - min) + min)
(leave () blank)

Example: Math.random() * (10 - 1) + 1 =
Random number between 10 and 1

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

Boolean Operator for

Not

A

(!)

Example: (!true = false), (!boolean = notBoolean)

Creates Opposite

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

Boolean Operator for

Or

A

(||)

Example: (true || true = true), (true || false = true)
(false || false = false)

Both variables must be false to return false
All else is true

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

Boolean Operator for

And

A

(&&)

Example: (false && false = false), (true && false = false)
(true && true = true)

Both variables must be true to return true
All else is false

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

Boolean Operator for

Equal to

A

(===)

Example: (true === true = true), (false === false = true)
(true === false = false)

Both variables must be the same to return true
All else is false

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

Boolean Operator for

Not equal to

A

(!==)

Example: (true !== false = true), (false !== true = true)
(true !== true = false)

Both variables must be different to return true
All else is false

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

Find a Character in a string

A

“string”[index]

Example: “computer”[3] = p

Gets a specific character in a string
index always starts at 0

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

Add together two or more strings without a space

Add together two or more strings with a space

A

(without a space)
“string1” + “string2”

Example: “comp” + “uter” = computer

(with a space)
“string1” + “ “ + “string2”

Example: “John” + “ “ + “Smith” = John Smith

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

Interpolate a string

A

+ parameters +

Example:
“We will go “ + hiking + “ on “ + Tuesday + “.”
=
we will go hiking on Tuesday

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

Get the length of a string

A

“string”.length

Example: “Apple”
“string”.length = 5

Tells you the length of the string

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

Get the last character of a string

A

“string”[“string”.length - 1]

Example: “banana”
“string”[“string”.length - 1] = a

Tells you the last character in the string

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

Get a portion of a string

A

“string”.substring(parameters)

Example: “Queue”
“string”.substring(1, 4) = ueu

Tells you a specific portion of a string
Starts at 0, ends before 2nd parameter

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

Find the beginning index of a portion of a string

A

“string”.indexOf(substring)

Example: “Quicksort”
“string”.indexOf(“sort”) = 5

Tells you the index where a substring starts
Starts at 0

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

Change not a string into a string

A

parameter.toString()
(fill in parameter), (leave () blank)

Example: 54.toString = “54”

converts parameter into single string

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

Accessing an element in an Array

A

Array[index]

Example:
[water, coffee, tea][2]
array[index] = tea

Tells you the element at the given index
Index starts at 0

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

Get the length of an Array

A

Array.length

Example:
[1, 3, 4, 5, 7]
array.length = 5

Tells you the number of elements in an array

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Remove the last element of an array
array.pop() (leave () blank) ``` Example: [1, 2, 3] array.pop() ; return array ; = [1, 2] ``` Tells you the last element in an array
26
Add an element to the front of an array
array.unshift(newElement) ``` Example: [1, 2] array.unshift(3) ; return array ; = [3, 1, 2] ``` Adds new element to front of array
27
Add an element to the back of an array
array.push(newElement) ``` Example: [1, 2] array.push(3) ; return array ; = [1, 2, 3] ``` Adds new element to back of array
28
Remove an element from an array
array.splice(index where to delete, how many to delete) Example: [8, 9, 10, 10000, 11] array.splice(3, 1) = [8, 9, 10, 11] Removes indicated element Index starts at 0
29
Add/remove an element to an array
array.splice (index add/delete, how many delete, newElements) Example: Add [8, 9, 10, 12].splice(3, 0, 11) = [8, 9, 10, 11, 12] Example: add and delete [8, 9, 10, 24, 17, 13] array.splice(3, 2, 11, 12) = [8, 9, 10, 11, 12, 13] Adds or Adds/Removes elements in an array Index starts at 0
30
Determine if value is an array
Array.isArray(input) Example: Array.isArray([1, 2, 3]) = true Tells you whether or not the input is an array
31
Copy an array or part of an array
array.slice(start index, end index) Example: copy part of an array ['q', 'u', 'e', 'u', 'e'].slice(1, 4) = ['u', 'e', 'u'] Example: copy entire array ['q', 'u', 'e', 'u', 'e'] array.slice() = ['q', 'u', 'e', 'u', 'e'] Leave () blank to copy entire array
32
Add one array to another array
result.concat(array) ``` Example: var result = [] array of arrays[ ['a', 'b'], ['c', 'd'], ['e', 'f'] ] result = result.concat(array[i]) return result = ['a', 'b', 'c', 'd', 'e', 'f'] ``` Combines multiple arrays
33
Turn an array into a string
array.join(element to replace comma in array) Example: ["my", "name", "is"] array.join(" ") = my name is Turns an array into a string
34
Turn a string into an array
string.split(element to replace with a comma) Example: var name: "my name is" name.split(" ") = ["my", "name", "is"] Turns a string into an array
35
Find the index of an element of an array
array.indexOf(element) Example: ["this", "is", "crazy"] array.indexOf("is") = 1 Tells you the index of the element Index starts at 0
36
Access a value in an object
object[key] ``` Example: {who: "me", when: "now", what: "tea"} ["when"] obj[key] = "now" Tells you the property at the key in the object ```
37
Remove a property (key: value) from an object
delete object[key] ``` Example: {who: "me", when: "now", what: "tea"} ["when"] delete obj[key] = {who: "me", what: "tea"} ``` Deletes both key and "property" at given key in object
38
Average
Sum of numbers divided by amount of numbers Example: 12 + 27 + 5 + 9 = 53. 53/5 = 10.6 Average is 10.6
39
Area of a Triangle
(Base x Height) / 2
40
Number Cubed
Any number to the power of 3 Example: 3 ** 3 = 27
41
Number Squared
Any number to the power of 2 Example: 3 ** 2 = 9
42
Area of a Rectangle
Length * Width
43
Perimeter of a Circle
2 * Math.PI * radius Math.PI is code for Pi
44
Code for Pi
Math.PI
45
Area of a Circle
Math.PI * radius ** 2 Pi times the Radius to the Power of 2 (or squared)
46
Square Root
Math.sqrt(number)
47
Remove from front of an array
array.shift() (leave () blank) ``` Example: [1, 2, 3] array.shift() ; return array ; = [2, 3] ```
48
Strings have length properties. Numbers do not have length properties. Arrays do have a length property.
``` var string = "hello" string.length = 5 ``` ``` var number = 1 number.length = undefined ```
49
.length includes spaces when measuring a string
``` var string = "what is up" string.length = 10 ```
50
What is an object
An object is a datatype that helps us collect and organize many pieces of data. Inside is going to be Key: Value (Property)
51
What is an Array
A piece of data that organizes many pieces of data. Can be anything from strings, to booleans, to numbers. Or even more nested arrays in an array. You can also have an object in an array.
52
What is an Array
One piece of data that organizes many pieces of data. Can be anything from strings, to booleans, to numbers. Or even more nested arrays in an array. You can also have an object in an array.
53
Property
key: value | A property can only have one key and one value
54
Keys are strings
{ madeByAWizard: true } madeByAWizard is a string
55
``` Dot Notation (shortcut) ```
``` Dot Notation will find the named key and output the value at named key. shortcut to bracket notation var magicObj = { secretCode: "open sesame" } magicObj.secretCode returns open sesame ```
56
Bracket Notation | best to use
``` You can use bracket notation for any situation trying to get a value from an obj. var magicObj = { secretCode: "open sesame" } magicObj["secretCode"] returns open sesame ``` If putting a variable in Brackets you do not need ""
57
Using a variable to represent a key | Bracket Notation
``` var magicObj = { familiars: ["frog", "bat", "cat", "owl"] } var animals = "familiars" magicObj[animals] returns ["frog", "bat", "cat", "owl"] ```
58
console.log
kind of works as notes. You cant actually use it. Maybe your in your code and are wondering if the var howMuch is equal to 11, you can console.log. ``` var howMuch = 11 console.log(11 === howMuch) ``` it will return true of false console.log outside of function
59
Return
Returns the result or something that can be used in your code return is always the last thing that your function does. Nothing else can happen after that
60
Function
A set of instructions that we want the computer to execute
61
typeof cannot tell the difference between an array and an object. How do you figure out if something is an array?
Which is why we must use Array.isArray to figue out if it is an array.
62
Convert an Object into an Array
...
63
Convert an Array into an Object
...
64
You know something is a function because it has parenthesis ()
array. pop() | array. unshift()
65
Type of
if (typeof(input) === "number") | if (typeof(input) === "string")
66
Sumnation
Sumnation of the number 6 add all number below 6 1+2+3+4+5+6 = 21 ``` How to: n = 6 var sumnation = 0 -- iterate over the single number -- there is no length for (var i = 0; i <= n; i++) -- i needs to be <= to n because i is what we are adding -- add i to sumnation sumnation += i return sumnation; = 21 ```
67
factorial
Factorial of the number 4 multiply all the numbers below 4 4*3*2*1 = 24 var must be 1 because of multiplication ``` How to: n = 4 var factorial = 1 -- iterate over single number for (var i = 1; i <= n; i++) -- i must be <= n -- multiply i by factorial factorial *= i return factorial; = 24 ```
68
Compound Interest
``` // I = P(1 + (r/n)) ^ (n*t) - P // n is compoundingFrequency // t is timeInYears // r is interestRate ``` ``` Example: var compoundedInterest = (principal * Math.pow(1 + (interestRate/compoundingFrequency) , (compoundingFrequency * timeInYears))) - principal ``` return compoundedInterest; }
69
Multiply between/ Sum between | starts at first number
(num1, num2) (2, 5) ``` var product = 1 for (var i = num1; i < num2; i++) product = product * i return product; = 24 ``` //starts at first number