Javascript Flashcards
Code for
Exponent or Power
(**) or Math.pow(baseNumber, powerNumber)
Example: 2 ** 5 = 32
For squared exponent or power will always be 2
(6 ** 2), (20 ** 2), (3 ** 2)
Find remainder
(%)
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
Sum (Plus-equals) (Increment)
Minus-equals (Decrement)
Product (Times-equals) (Multiplier)
Divide-equals (Divider)
Sum (+=)
Example: 11 += 2 = 13
Decrement (-=)
Example: 11 -= 2 = 9
Product (*=)
Example: 11 *= 2 = 22
Divider (/=)
Example: 12 /= 2 = 6
Find Absolute Value
Math.abs()
Example: Math.abs(-295) = 295
Measures distance from 0
Answer will always be positive
Round Up
Math.ceil()
Example: Math.ceil(33.7) = 34
Rounds up
Round Down
Math.floor()
Example: Math.floor(33.7) = 33
Rounds down
Code for
Parsing an integer from a string
Number.parseInt()
Code for
Parsing a float from a string
Number.parseFloat()
Generate a random Number
Math.random() * (max - min) + min)
(leave () blank)
Example: Math.random() * (10 - 1) + 1 =
Random number between 10 and 1
Boolean Operator for
Not
(!)
Example: (!true = false), (!boolean = notBoolean)
Creates Opposite
Boolean Operator for
Or
(||)
Example: (true || true = true), (true || false = true)
(false || false = false)
Both variables must be false to return false
All else is true
Boolean Operator for
And
(&&)
Example: (false && false = false), (true && false = false)
(true && true = true)
Both variables must be true to return true
All else is false
Boolean Operator for
Equal to
(===)
Example: (true === true = true), (false === false = true)
(true === false = false)
Both variables must be the same to return true
All else is false
Boolean Operator for
Not equal to
(!==)
Example: (true !== false = true), (false !== true = true)
(true !== true = false)
Both variables must be different to return true
All else is false
Find a Character in a string
“string”[index]
Example: “computer”[3] = p
Gets a specific character in a string
index always starts at 0
Add together two or more strings without a space
Add together two or more strings with a space
(without a space)
“string1” + “string2”
Example: “comp” + “uter” = computer
(with a space)
“string1” + “ “ + “string2”
Example: “John” + “ “ + “Smith” = John Smith
Interpolate a string
+ parameters +
Example:
“We will go “ + hiking + “ on “ + Tuesday + “.”
=
we will go hiking on Tuesday
Get the length of a string
“string”.length
Example: “Apple”
“string”.length = 5
Tells you the length of the string
Get the last character of a string
“string”[“string”.length - 1]
Example: “banana”
“string”[“string”.length - 1] = a
Tells you the last character in the string
Get a portion of a string
“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
Find the beginning index of a portion of a string
“string”.indexOf(substring)
Example: “Quicksort”
“string”.indexOf(“sort”) = 5
Tells you the index where a substring starts
Starts at 0
Change not a string into a string
parameter.toString()
(fill in parameter), (leave () blank)
Example: 54.toString = “54”
converts parameter into single string
Accessing an element in an Array
Array[index]
Example:
[water, coffee, tea][2]
array[index] = tea
Tells you the element at the given index
Index starts at 0
Get the length of an Array
Array.length
Example:
[1, 3, 4, 5, 7]
array.length = 5
Tells you the number of elements in an array
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
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
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
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
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
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
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
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
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
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
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
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
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
Average
Sum of numbers divided by amount of numbers
Example: 12 + 27 + 5 + 9 = 53. 53/5 = 10.6
Average is 10.6
Area of a Triangle
(Base x Height) / 2
Number Cubed
Any number to the power of 3
Example: 3 ** 3 = 27
Number Squared
Any number to the power of 2
Example: 3 ** 2 = 9
Area of a Rectangle
Length * Width
Perimeter of a Circle
2 * Math.PI * radius
Math.PI is code for Pi
Code for Pi
Math.PI
Area of a Circle
Math.PI * radius ** 2
Pi times the Radius to the Power of 2 (or squared)
Square Root
Math.sqrt(number)
Remove from front of an array
array.shift()
(leave () blank)
Example: [1, 2, 3] array.shift() ; return array ; = [2, 3]
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
.length includes spaces when measuring a string
var string = "what is up" string.length = 10
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)
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.
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.
Property
key: value
A property can only have one key and one value
Keys are strings
{
madeByAWizard: true
}
madeByAWizard is a string
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
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 “”
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"]
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
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
Function
A set of instructions that we want the computer to execute
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.
Convert an Object into an Array
…
Convert an Array into an Object
…
You know something is a function because it has parenthesis ()
array. pop()
array. unshift()
Type of
if (typeof(input) === “number”)
if (typeof(input) === “string”)
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
factorial
Factorial of the number 4
multiply all the numbers below 4
432*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
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; }
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