Given a working REPL interface, write and execute a statement that will print “hello world” using console.log
Identify that strings are a list of characters defined by using double or single quotes
"hello world"[0] // 'h' (indices start at 0)
"hello world"[-1] // undefined (indices outside range of characters return undefined)
"hello world"["hello world".length - 1] // d (since we start at 0, the last character is at the index of length - 1)
Given an arithmetic expression using +, -, *, /, %, compute its value
console.log(1 + 2); // 3
console.log(14 - 6); // 8
console.log(2 * 3); // 6
console.log(6 / 3); // 2
console.log(7 % 6); // 1
Given an expression, predict if its value is NaN
let num1;
console.log(1 + 2 * 3); // 7 (multiplication is performed first)
console.log((1 + 2) * 3); // 9 (addition is first because it is grouped)
Construct the truth tables for &&, ||, ! - NOT (!)
A | !A |
---|---|
true | false |
false | true |
A | B | A && B |
---|---|---|
true | true | true |
true | false | false |
false | true | false |
false | false | false |
A | B | A || B |
---|---|---|
true | true | true |
true | false | true |
false | true | true |
false | false | false |
Given an expression consisting of >, >=, ===, <, <=, compute it’s value
console.log(1 > 4); // false
console.log(1 < 4); // true
console.log(4 >= 4); // true
console.log(4 <= 4); // true
console.log(4 === 4); // true
console.log((1 + 3) % 3 === 2 - 6 / 3 + 1) // true
Apply De Morgan’s law to a boolean expression - !(A || B) === !A && !B - !(A && B) === !A || !B
Given an expression that utilizes operator precedence, compute its value
console.log(1 + 2 * 3 + 4); // 11 (multiplication is performed first, then addition)
console.log(1 + 2 * 3 % 4); // 3 (multiplication and modulo are performed first, then addition)
Given an expression, use the grouping operator to change it’s evaluation
console.log(1 + 2 * 3); // 7 (multiplication is performed first)
console.log((1 + 2) * 3); // 9 (addition is first because it is grouped)
Given expressions using == and ===, compute their values
console.log(4 == '4'); // true (coercion takes place)
console.log(4 === '4'); // false (the number and string are compared directly)
Given a code snippet using postfix ++, postfix –, +=, -=. /=, *=, predict the value of labeled lines
let age = 5;
console.log(age); // 5
age + 5;
console.log(age); // 5 (age was not reassigned above)
age += 5;
console.log(age); // 10 (+= is shorthand for age = age + 5, so age is reassigned)
age *= 5;
console.log(age); // 50
age -= 5;
console.log(age); // 45
age /= 5;
console.log(age); // 9
age++;
console.log(age); // 10 (++ is shorthand for age = age + 1, so age is reassigned)
age--;
console.log(age); // 9
Create and assign a variable using let to a string, integer, and a boolean. Read its value and print to the console.
let variable = 'This is a string';
console.log(variable) // This is a string (The value of the variable is what is printed, not the name.)
variable = 8
console.log(variable) // 8 (The variable can be reassigned from the string to an integer.)
variable = true
console.log(variable) // true (Reassignment to a boolean works, too!)
Define a function using function declaration - We use the function
keyword to declare our intent to create a function. - We give our function a name (myNewFunction). - We capture any parameters that are passed in and assign them names (parameter1 and parameter2). - We open up a block with curly braces to house our function’s code. - We do any functionality that we want our function to perform. - We use the return
keyword to return a value from our function (otherwise we return undefined
).
function myNewFunction(parameter1, parameter2) {
console.log(parameter1);
console.log(parameter2);
return parameter1 + parameter2;
}
Define a function that calculates the average of two numbers, call it, pass in arguments, and print it’s return value
function averageOfTwo(num1, num2){
let sum = num1 + num2;
let average = sum / 2;
return average;
}
console.log(averageOfTwo(3, 7)) // 5
Identify the difference between parameters vs arguments - Parameters are in the function definition. They capture the values passed in to the function (arguments) as variable names and allow us to use those values in our function. - Arguments are in the function invocation. They are the values that are passed in to the function. - In Objective 2 above, num1
and num2
are the parameters while 3
and 7
are the arguments.