JS101 Flashcards

1
Q

instead of:
if (x > 3) return true
You should?

A

return x > 3

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

Instead of:
variable <= 0 || variable >= 5 || Number.isNaN(Number(variable))
break;
you should?

A

Check it’s existence on an array:
If (![‘1’, ‘2’, ‘3’, ‘4’].includes(variable))

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

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?

A

Check it’s existence on an array:
If (![‘1’, ‘2’, ‘3’, ‘4’].includes(variable))

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

How does Try/Catch work?

When should you use it? (2 considerations)

A

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.

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

What’s the difference between String and .toString()

A

String
> String(null) ‘null’
> String(undefined) ‘undefined’

.toString()
> null.toString();
Error

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

What happens with:
` ${ undefined} ` ?

A

undefind is converted into a string.
This is why you don’t do String() or .toString() in template literal.

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

What is returned from:
+[]
+[1]
+[1, 2]

A

0
1
NaN

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

When should you do implicit conversion (at launch school)?

A

Never

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

When should you use ==

A

never

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

When a string and number are compared, what is coerced to what?
‘12’ == 12

A

String to number

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

When Boolean is compared to something, what is converted to what?
true == ‘apple’

A

Boolean always converted to number
Above is false;

true == ‘1’
true

true + true == ‘2’
rue

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

When an Object is compared to a primitive, what is converted to what?

A

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

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

Null == undefined?
Null === undefined?

A

True
False

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

true >= 1
> [] == 0
> 1 == true

A

true
true
true

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

> 3 == true
0 == false

A

false
true

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

> ‘[object Object]’ == {}
‘[object Object]’ == {rt: 21}
[] == ‘’
String([1, [1, 2]])

A

true
true
true
1,1,2

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

String + something?

A

Becomes a string

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

Number, boolean, null, undefined, + together?
true + true
null + 3 + undefined

A

Becomes number
2
NaN

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

Object/Array + something?

A

Becomes a string

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

Three rules for conversion/comparison at launch school
One rule for conversion in template literals

A

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).

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

What are the elements of PEDAC

A

Problem
Examples
Data Structures
Algorithm
Code

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

What is involved with PROBLEMS in PEDAC? (4 things)

A

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

What is involved with EXAMPLES in PEDAC (4 things)

A
  • understand how the input translates to output
  • identify VALID CASES
  • identify EDGE CASES
  • create the test cases and confirm outputs
24
Q

What is involved with DATA STRUCTURES in PEDAC? (3 things)

A
  • what sort of actions do you have to do (sort, collect, filter, etc.)
  • what kind of data are you primarily dealing with? (strings, arrays, numbers, objects, etc.)
  • focus on methods for these types
25
Q

What is involved with ALGORITHM in PEDAC? (2 things)

A
  • step by step process that takes you from input to output, your code will depend on your algorithm
  • handles edges cases and valid example inputs
26
Q

Instead of radius * 3.14
What should you do?

A

pi = 3.14
radius * pi

Otherwise it’s a “magic” number. Don’t do it.

27
Q

Which should you do?
const FIRST_CHARACTER_CODE = 97;
vs
const FIRST_CHARACTER_CODE = ‘a’.charCodeAt();

A

Second one. More readable and understandable.

28
Q

A function is said to have side effects if it does any of the following:
(5 things)

A

It reassigns any non-local variable. Reassigning a variable in the outer scope would be a side effect.

It mutates the value of any object referenced by a non-local variable. Mutating an array or object argument, for instance, would be a side effect.

It reads from or writes to a file, network connection, browser, or the system hardware. Side effects like this include writing to the console log and reading input from the terminal.

It raises an exception without handling it.

It calls another function that has side effects.

29
Q

A function should not have a side effect AND return a value
T or F

A

True
If you write functions that do both, you may have trouble remembering one of those – either you’ll forget about the side effect, or you’ll forget that there’s a return value that you need to examine.

30
Q

How to name a function that has a side effect vs a return?

A

Can do “display” to show user
“print” to show in console.
“compute” suggests a return
“get” suggests asking. maybe “ask”
“read”
“write”

31
Q

Why is this:
while (true) {
console.log(‘Continue? (y/n)’);
let answer = readline.question();
if (answer.toLowerCase() === ‘n’) break;
}

Better than this:

let answer = ‘’;
while (answer.toLowerCase() !== ‘n’) {
console.log(‘Continue? (y/n)’);
answer = readline.question();
}

A

Because if answer isn’t defined in the second instance, the program won’t run.

32
Q

Write a function that takes a number as an argument. If the argument is a positive number, return the negative of that number.

A

function negative(number) {
return Math.abs(number) * -1;
}

33
Q

An equation that gives a random number in between 2 values?
How to make it so that if the user puts the numbers in the wrong order it still works?

A

function randomNumberBetween(minNum, maxNum) {
if (minNum >= maxNum) {
[minNum, maxNum] = [maxNum, minNum];
}

return Math.floor(Math.random() * (maxNum + 1 - minNum)) + minNum;
}

34
Q

You’re repeating certain mathematical things, like ‘string’.length / 2. What should you do?

A

Make it a variable.
centerIndex = ‘string’.length / 2

35
Q

What happens with:
let myVar = [1];

function myFunc(myVar) {
myVar[0] = 2;
}

myFunc();
console.log(myVar);

A

let myVar = [1];

function myFunc(myVar) {
myVar[0] = 2;
}

myFunc(); // TypeError: Cannot set properties of undefined (setting ‘0’)
console.log(myVar);

36
Q

What happens with:
let myVar = 1;

function myFunc(myVar) {
myVar = 2;
}

myFunc(myVar);
console.log(myVar);

A

let myVar = 1;

function myFunc(myVar) {
myVar = 2;
}

myFunc(myVar);
console.log(myVar); // 1

37
Q

let myVar = [1];

function myFunc(myVar) {
myVar = [2];
}

myFunc();
console.log(myVar);

A

let myVar = [1];

function myFunc(myVar) {
myVar = [2];
}

myFunc();
console.log(myVar); // [1]

38
Q

let myVar = [1];

function myFunc(myVar) {
myVar[0] = 2;
}

myFunc(myVar);
console.log(myVar);

A

let myVar = [1];

function myFunc(myVar) {
myVar[0] = 2;
}

myFunc(myVar);
console.log(myVar); // [2]

39
Q

What happens if an block’s variable or parameter has the same name as one outside?

A

The inner one shadows the outer one.

40
Q

Are Blocks and Function bodies the same thing basically?

A

Yes but no. Function bodies are not blocks
There are subtle differences that we will see later. But often function similarly.

41
Q

What will this log to console?
let num = 5;

function myFunc() {
console.log(num);
let num = 10;
}

myFunc();
console.log(num);

A

Error!
The program sees the num used before it’s called in the function body.

42
Q

What is logged?

function bar(array) {
return array;
}

let array = [1, 2, 3];
let newArray = bar(array);
console.log(array === newArray);

array.push(4);
console.log(array);
console.log(newArray);

A

function bar(array) {
return array;
}

let array = [1, 2, 3];
let newArray = bar(array);
console.log(array === newArray); // true (they are same object)

array.push(4);
console.log(array); // [ 1, 2, 3, 4 ]
console.log(newArray); // [ 1, 2, 3, 4 ]

43
Q

What is logged?

function changeMyWords(words) {
console.log(words);
words = [‘Hi’, ‘Goodbye’];
}

let myWords = [‘Hello’, ‘Goodbye’];
changeMyWords(myWords);
console.log(myWords);

A

[‘Hello’, ‘Goodbye’]
[‘Hello’, ‘Goodbye’]

44
Q

What is logged?

let myVar = [1];

function myFunc() {
myVar = [2];
}

myFunc();
console.log(myVar);

A

let myVar = [1];

function myFunc() {
myVar = [2];
}

myFunc();
console.log(myVar); // [2]

45
Q

What is logged?

let myVar = [1];

function myFunc(myVar) {
myVar = [2];
}

myFunc(myVar);
console.log(myVar);

A

let myVar = [1];

function myFunc(myVar) {
myVar = [2];
}

myFunc(myVar);
console.log(myVar); // [1]

46
Q

What is logged?

let color = ‘purple’;
let colors = [‘red’, ‘green’, ‘blue’];

function addColor(colors, color) {
colors.push(color);
return colors;
}

function removeColor(colors) {
color = colors.pop();
return colors;
}

let newColors = removeColor(colors);
addColor(colors, color);
console.log(newColors);

A

[‘red’, ‘green’, ‘blue’]

47
Q

What is logged?

function capitalize() {
return word[0].toUpperCase() + word.slice(1);
}

function exclaim() {
return word += ‘!!!’;
}

let word = ‘hello’;
let capitalizedWord = capitalize(word);
let exclaimedWord = exclaim(capitalizedWord);

console.log(word);
console.log(capitalizedWord);
console.log(exclaimedWord);

A

hello!!!
Hello
hello!!!

48
Q

function capitalize(word) {
return word[0].toUpperCase() + word.slice(1);
}

function exclaim(word) {
return word += ‘!!!’;
}

let word = ‘hello’;
let capitalizedWord = capitalize(word);
let exclaimedWord = exclaim(capitalizedWord);

console.log(word);
console.log(capitalizedWord);
console.log(exclaimedWord);

A

hello
Hello
Hello!!!

49
Q

Where should constant declarations go in the file?

A

global constants get defined near the top of the program file, usually just after any require calls.

50
Q

What’s wrong with this?
words.forEach(word => {
console.log(word);
words.shift();
});

A

Mutates something as you’re iterating over it:
words.forEach(word => {
console.log(word); // logs: scooby, on, two (in that order)
words.shift();
});

51
Q

How do you make this look more intentional:
let someVariable;

if (someVariable = getAValueFromSomewhere()) {
console.log(severable);
}

A

if ((someVariable = getAValueFromSomewhere())) {
console.log(someVariable);
}

52
Q

If you have a callback function and don’t want to use the parameter, what should you do?

A

Use an underscore:
names.forEach(_ => {
console.log(‘Got a name!’)
});

53
Q

What can you do instead of (and why?):

function isInvalidChoice(choice) {
return (!Object.keys(getOptionsAndRules()).includes(choice) &&
!Object.keys(ABBR_OPTIONS).includes(choice)) ||
(!Object.keys(getOptionsAndRules()).includes(ABBR_OPTIONS[choice]) &&
!Object.keys(getOptionsAndRules()).includes(choice));
}

or even

function isInvalidChoice(choice) {
return (isInvalidOption(choice) &&
(isInvalidAbbr(choice) || abbrIsNotInRuleSet(choice)))
}

A

Probably to make the conditional more readable

function isInvalidChoice(chioce) {
const invalidOpt = isInvalidOption(choice);
const invalidAbbr = isInvaloidAbbr(choice);
const notInRuleSet = abbrIsNotInRuleSet(choice);

return invalidOpt && invalidAbbr || notInRuleSet;
}

54
Q

If you have functions that mutate objects, what should you do instead? why?

A

To make sure the reader doesn’t have to go into updateScoreCard to see that it’s modifying the score.
And to make it easier to make a copy of the object rather than relying on mutating it.

Instead of:
function playTournament(winLimit) {
let scoreCard = {
compWins: 0,
playerWins = 0,
}
updateScoreCard(scoreCard);
}

Do:
function playTournament() {
let scoreCard = {
compWins: 0,
playerWins = 0,
}

scoreCard = getUpdatedScoreCard(scoreCard, results);
}

55
Q

What are the primitive values. How many?

A

7
numbers
strings
boolean

undefined
null

BigInt
symbols