JS101 Flashcards
instead of:
if (x > 3) return true
You should?
return x > 3
Instead of:
variable <= 0 || variable >= 5 || Number.isNaN(Number(variable))
break;
you should?
Check it’s existence on an array:
If (![‘1’, ‘2’, ‘3’, ‘4’].includes(variable))
Instead of:
variable <= 0 || variable >= 5 || Number.isNaN(Number(variable))
break;
to check if the number is between two numbers and is a number, you should?
Check it’s existence on an array:
If (![‘1’, ‘2’, ‘3’, ‘4’].includes(variable))
How does Try/Catch work?
When should you use it? (2 considerations)
try {
// Do something that might fail here and throw an exception.
} catch (error) {
// This code only runs if something in the try clause throws an exception. // “error” contains the exception object.
} finally {
// This code always runs even if the above code throws an exception. }
Only use try/catch/finally blocks when the following conditions are both true:
A built-in JavaScript function or method can throw an exception and you need to handle or prevent that exception.
A simple guard clause is impossible or impractical to prevent the exception.
What’s the difference between String and .toString()
String
> String(null) ‘null’
> String(undefined) ‘undefined’
.toString()
> null.toString();
Error
What happens with:
` ${ undefined} ` ?
undefind is converted into a string.
This is why you don’t do String() or .toString() in template literal.
What is returned from:
+[]
+[1]
+[1, 2]
0
1
NaN
When should you do implicit conversion (at launch school)?
Never
When should you use ==
never
When a string and number are compared, what is coerced to what?
‘12’ == 12
String to number
When Boolean is compared to something, what is converted to what?
true == ‘apple’
Boolean always converted to number
Above is false;
true == ‘1’
true
true + true == ‘2’
rue
When an Object is compared to a primitive, what is converted to what?
When an object is compared with a primitive value, the object is coerced into a primitive value and compared again using the == operator.
> ’’ == {}
false
‘[object Object]’ == {}
true
[] == ‘’
true
> [] == 0
true
[] becomes ‘’
which then is compared again and becomes 0
Null == undefined?
Null === undefined?
True
False
true >= 1
> [] == 0
> 1 == true
true
true
true
> 3 == true
0 == false
false
true
> ‘[object Object]’ == {}
‘[object Object]’ == {rt: 21}
[] == ‘’
String([1, [1, 2]])
true
true
true
1,1,2
String + something?
Becomes a string
Number, boolean, null, undefined, + together?
true + true
null + 3 + undefined
Becomes number
2
NaN
Object/Array + something?
Becomes a string
Three rules for conversion/comparison at launch school
One rule for conversion in template literals
Always use explicit
Always use strictcomprison
Can use + for technically implicit number coersion
Don’t use String() or toString() inside ${…} expressions in template literals (just redundant).
What are the elements of PEDAC
Problem
Examples
Data Structures
Algorithm
Code
What is involved with PROBLEMS in PEDAC? (4 things)
Understand the problem
- read the problem description
- examine all given examples for info
- ask clarifying questions
- identify INPUTS/OUTPUTS
- identify RULES/REQUIREMENTS
- mental model problem. Sort of like a VERY simple algorithm. Just describe what you broadly need to do.