Skip to content

Instantly share code, notes, and snippets.

View Dongjinmedia's full-sized avatar
🎯
Focusing

Dongjin Park Dongjinmedia

🎯
Focusing
View GitHub Profile
@Dongjinmedia
Dongjinmedia / answer4.js
Last active December 25, 2025 10:57
The bug is due to JavaScript's function-level scoping of var i. The i in the inner function refers to the same variable i from the outer scope, which changes with each loop iteration. By the time the returned functions are called, i equals y, so all functions return x + y instead of x + i (where i is the loop index).
function createArrayOfFunctions(y) {
var arr = [];
for (var i = 0; i < y; i++) {
// Use an IIFE to capture the current value of i
arr[i] = (function(index) {
return function(x) {
return x + index;
};
})(i);
}
def nextFibonacci(numbers):
results = []
for num in numbers:
a, b = 0, 1
# Generate Fibonacci numbers until we find one > num
while b <= num:
a, b = b, a + b
results.append(b)

Computational Complexity: O(n)

Converting arr1 to a set takes O(m) time, where m is the length of arr1.

Checking each element in arr2 takes O(n) time, where n is the length of arr2.

Set membership checking is O(1) on average.

def isSubset(arr1, arr2):
# Convert arr1 to a set for O(1) lookups
set1 = set(arr1)
# Check if every element in arr2 exists in set1
for elem in arr2:
if elem not in set1:
return False
return True