Skip to content

Instantly share code, notes, and snippets.

@ldealmei
Last active May 12, 2019 13:49
Show Gist options
  • Save ldealmei/be35ac7f74d12f5290503061e199aa5d to your computer and use it in GitHub Desktop.
Save ldealmei/be35ac7f74d12f5290503061e199aa5d to your computer and use it in GitHub Desktop.

Callbacks

Q1

function greeting(name) {
  console.log('Hello ' + name);
}
function processUserInput(callback) {
  var name = prompt('Please enter your name.');
  // user types in: 'salvatore'
  callback(name);
}
processUserInput(greeting);
  • 'salvatore'
  • 'Hello salvatore'
  • 'Hello '
  • null

Q2

function capitalize(name) {
  name = name.toUpperCase()
  console.log('Hello ' + name);
}
function processUserInput(callback) {
  var name = prompt('Please enter your name.');
  // user types in: 'salvatore'
  callback(name);
}
processUserInput(capitalize);
  • 'salvatore'
  • 'HELLO SALVATORE'
  • 'Hello Salvatore'
  • 'Hello SALVATORE'

Q3

function capitalize(name) {
  name = name.toUpperCase()
  console.log('Hello ' + name);
}
function processUserInput(callback) {
  var name = prompt('Please enter your name.');
  // user types in: 'salvatore'
  callback(name);
}
processUserInput(capitalize());
  • 'salvatore'
  • 'Hello salvatore'
  • ReferenceError
  • 'Hello SALVATORE'

Q4

function processUserInput(callback) {
  var name = prompt('Please enter your name.');
  // user types in: 'salvatore'
  callback(name);
}
processUserInput((input) => console.log(input[0]));
  • 'salvatore'
  • ReferenceError
  • 's'
  • 'i'

Q5

const callback = () => {
    console.log("Done!");
}
setTimeout(callback, 5000);
  • prints "Done!" after 5s
  • prints "Done!" after 5000s
  • prints nothing

Q6

setTimeout(() => console.log("A"), 500);
setTimeout(() => console.log("B"), 300);
setTimeout(() => console.log("C"), 400);
  • prints in this order: "A", "B", "C"
  • prints in this order: "A", "C", "B"
  • prints in this order: "B", "C", "A"

Q7

setTimeout(() => console.log("B"), 300);
setTimeout(() => console.log("A"), 500);
setTimeout(() => console.log("C"), 400);
  • prints in this order: "A", "B", "C"
  • prints in this order: "A", "C", "B"
  • prints in this order: "B", "C", "A"

Q8

const sayGoodbye = () => console.log('Goodbye');
function sayHello(name){
  console.log('Hello ' + name);
  setTimeout(sayGoodbye, 50);
  }
sayHello('Harun')
  • 'Hello Harun' then 'Goodbye'
  • 'Hello Harun'
  • 'Goodbye'
  • 'Goodbye' then 'Hello Harun'

Scope

Q9

const name = 'HackYourFuture'; 
const func1 = () => {
  const func2 = () => {
    console.log(name)
  };
  func2();
};
func1()
  • 'HackYourFuture'
  • undefined
  • ReferenceError

Q10

const func1 = () => {
  const func2 = () => {
    console.log(name)
  };
  func2()
};
func1()
const name = 'HackYourFuture'; 
  • 'HackYourFuture'
  • undefined
  • ReferenceError

Q11

const func1 = () => {
  const func2 = () => {
    console.log(name)
  };
  func2()
};
const name = 'HackYourFuture';
func1()
  • 'HackYourFuture'
  • undefined
  • ReferenceError

Q12

const name = 'HackYourFuture';
const func1 = (name) => {
  const func2 = () => {
    console.log(name)
  };
  func2()
};
func1('Lee')
  • 'HackYourFuture'
  • 'Lee'
  • ReferenceError
  • undefined

Q13

const name = 'HackYourFuture';
const func1 = (name) => {
  const func2 = () => {
    console.log(name)
  };
  func2()
};
func1()
  • 'Lee'
  • 'HackYourFuture'
  • ReferenceError
  • undefined

Discussion

Why undefined and not ReferenceError?

Q14

const name = 'HackYourFuture';
const func1 = (name='Jafar') => {
  const func2 = () => {
    console.log(name)
  };
  func2()
};
func1()
  • 'Jafar'
  • 'HackYourFuture'
  • ReferenceError
  • undefined

Q15

const name = 'HackYourFuture';
const func1 = (name='Jafar') => {
  const func2 = (name='Mady') => {
    console.log(name)
  };
  func2('Amsterdam')
};
func1('Lee')
  • 'Lee'
  • 'HackYourFuture'
  • 'Jafar'
  • 'Mady'
  • 'Amsterdam'

Q16

const func1 = () => {
  console.log(name)
  const func2 = () => {
    name = 'Mady'
  };
};
func1()
  • ReferenceError
  • undefined
  • 'Mady'

Q17

const func1 = (name) => {
  console.log(name)
  const func2 = () => {
    name = 'Mady'
  };
};
func1()
  • ReferenceError
  • undefined
  • 'Mady'

Master challenges

Q18

const sayGoodbye = (name) => console.log('Goodbye ' + name);
function sayHello(name){
  console.log('Hello ' + name);
  setTimeout(sayGoodbye, 50);
  }
sayHello('Harun')
  • 'Hello Harun' then 'Goodbye Harun'
  • 'Hello Harun'
  • 'Goodbye Harun'
  • 'Hello Harun' then 'Goodbye undefined'

Q19

function sayHello(name){
  console.log('Hello ' + name);
  setTimeout((name) => console.log('Goodbye ' + name), 50);
  }
sayHello('Harun')
  • 'Hello Harun' then 'Goodbye Harun'
  • 'Hello Harun'
  • 'Goodbye Harun'
  • 'Hello Harun' then 'Goodbye undefined'

Q20

function sayHello(name){
  console.log('Hello ' + name);
  setTimeout(() => console.log('Goodbye ' + name), 50);
  }
sayHello('Harun')
  • 'Hello Harun' then 'Goodbye Harun'
  • 'Hello Harun'
  • 'Goodbye Harun'
  • 'Hello Harun' then 'Goodbye undefined'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment