Skip to content

Instantly share code, notes, and snippets.

@scr2em
Last active August 9, 2023 12:30
Show Gist options
  • Save scr2em/a5994c7b7c872eb6c6c65a9457d917cb to your computer and use it in GitHub Desktop.
Save scr2em/a5994c7b7c872eb6c6c65a9457d917cb to your computer and use it in GitHub Desktop.
JS Lab 3 - ASAT 2023
function outer() {
var a = 10;
function inner() {
console.log(a);
}
return inner;
}
const x = outer();
x(); // ?
// ---------------------------------------
function multiplyBy(x) {
return function (y) {
return x * y;
};
}
const double = multiplyBy(2);
console.log(double(5)); // ?
// ---------------------------------------
function outer() {
var count = 0;
return function() {
count++;
return count;
};
}
const increment = outer();
console.log(increment() + increment()); // ?
// ---------------------------------------
function buildMessage(message) {
return function (name) {
return `${message}, ${name}!`;
};
}
const greet = buildMessage("Hello");
console.log(greet("Alice")); // ?
// ---------------------------------------
var funcs = [];
for (var i = 0; i < 3; i++) {
funcs.push(function() {
console.log(i);
});
}
funcs[2](); // ?
// ---------------------------------------
function outer() {
var a = 5;
function inner() {
a++;
console.log(a);
}
return inner;
}
const incrementA = outer();
incrementA(); // ?
incrementA(); // ?
// ---------------------------------------
function countdown(n) {
return function () {
console.log(n);
n--;
};
}
const counter = countdown(3);
counter(); // ?
counter(); // ?
// ---------------------------------------
var number = 10;
function outer() {
var number = 5;
return function() {
console.log(number);
};
}
const display = outer();
display(); // ?
// ---------------------------------------
function makeCounter(start, step) {
var count = start;
return function () {
var current = count;
count += step;
return current;
};
}
const counter = makeCounter(1, 2);
console.log(counter() + counter()); // ?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment