Skip to content

Instantly share code, notes, and snippets.

@Pugio
Created June 4, 2018 01:55
Show Gist options
  • Save Pugio/ea2a5c5c0184410060a13ef0d11c437e to your computer and use it in GitHub Desktop.
Save Pugio/ea2a5c5c0184410060a13ef0d11c437e to your computer and use it in GitHub Desktop.
// Q1. What is a variable? (Describe in your own words)
// Q2.
var a = 1;
var b = 2;
var x = 3;
function test(x,y) {
x = 10;
a = y;
var b = x + a;
return b;
}
test(5,6);
// What is:
// * return value of the function?
// * the final value of a:
// * the final value of b:
// * the final value of x:
// Q3. What is a function?
// Q4. What are two different ways of creating a function in JavaScript?
// Q5. What is a closure?
// Q6. What is a callback?
// Q7.
function doit(a, cbk) {
var x = a + 10;
return cbk(x) + 3;
}
var res1 = doit(5, n => n + 2);
var p = 100;
function again(x) {
p += x;
return p + 2;
}
var res2 = doit(8, again);
var res3 = doit(2, again);
// What is the value of res1:
// * res2:
// * res3:
// * the final value of p:
// Q8. What is a Promise?
// Q9. What is a regular expression?
// * What are two different ways of creating a regular expression?
// * var searchStr = "pizza";
// * What is the code to create a regular expression that searches for: any number of "a" characters, the value of searchStr, a space, and then 1 or more "b" characters?
// Q10. var VS let trickery
a = 1;
b = 5;
function abc() {
a = 5;
let b = 10;
var a;
return a;
}
var res = abc();
// * What is the value of `res`:
// * What is the final value of a:
// * What is the final value of b:
// Q12.
a = 1
b = 5;
function xyz() {
a = 4;
for (let a = 10; a < 12; a++) {
b++;
}
return a;
}
var res2 = xyz();
// * What is the value of `res2`:
// * What is the final value of a:
// * What is the final value of b:
@MichaelNahum
Copy link

// Q1. What is a variable? (Describe in your own words)

A label pointing to a container for data.

// Q2.
var a = 1;
var b = 2;
var x = 3;

function test(x,y) {
b = 10;
a = y;

var b = 5 + 6;

return b;
}

test(5,6);

// What is:
// * return value of the function?
11
// * the final value of a:
6
// * the final value of b:
11
// * the final value of x:
5

// Q3. What is a function?
A block of code defining a series of actions to be executed within its own scope.

// Q4. What are two different ways of creating a function in JavaScript?
function foo(arg){...}

var x = function(arg){...}

// Q5. What is a closure?

A function enclosed within another function that has access to the outer function's variable declarations.

// Q6. What is a callback?
A function defined earlier passed as an argument into a subsequent function to be executed later on.

// Q7.
function doit(a, cbk) {
var x = a + 10;
return cbk(x) + 3;
}

var res1 = doit(5, n => n + 2);

var p = 100;
function again(x) {
p += x;
return p + 2;
}

var res2 = doit(8, again);
var res3 = doit(2, again);

// What is the value of res1:

20

// * res2:

123

// * res3:

117

// * the final value of p:

100

// Q8. What is a Promise?

A stand-in for the outcome of an asynchronous operation.

// Q9. What is a regular expression?

An object that matches a pattern of characters in a string.

// * What are two different ways of creating a regular expression?
let regExp = /target/;
let regExp = new RegExp('target');

// * var searchStr = "pizza";
// * What is the code to create a regular expression that searches for: any number of "a" characters, the value of searchStr, a space, and then 1 or more "b" characters?

var search = new RegExp('/a*/'searchStr'/\s/b+/')
***Totally guessing about the value of searchStr...I couldn't find any documentation on how to search for variables's values. Must be using the wrong search terms.

// Q10. var VS let trickery
a = 1;
b = 5;

function abc() {
a = 5;
let b = 10;

var a;

return a;
}

var res = abc();

// * What is the value of res:

Return value? 5

// * What is the final value of a:

  1. I think because even though a is set equal to 5 within the function's scope, the absence of a "var" or "let" means that that assignment applies globally. But I'm not 100% sure.

// * What is the final value of b:
5

// Q12.
a = 1
b = 5;
function xyz() {
a = 4;

for (let a = 10; a < 12; a++) {
b++;
}

return a;
}

var res2 = xyz();

// * What is the value of res2:
4
// * What is the final value of a:
4. "a=4" has global scope even though it is defined within a function.
// * What is the final value of b:
5. The incremented b is not returned. It is erased once the for loop completes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment