function firstFunction() {
// parent function
const x = 100; // parent variable / cant be used inside the child function
function secondFunction() {
const y = 60; // child variable / cannot be called in the parent function
return y + x; // return calculation to the child function
}
return secondFunction(); // evoke the second function by returning it to the parent function
}
console.log(firstFunction()); // call the parent function
const isRequired = () => { throw new Error('param is required'); };
const hello = (name = isRequired()) => { console.log(`hello ${name}`) };
// These will throw errors
hello();
hello(undefined);
// These will not
hello(null);
hello('David');
The idea here is that it uses default parameters, like how the b parameter here has a default if you don’t send it anything:
function multiply(a, b = 1) {
return a * b;
}
#In computer programming, an iterator is an object that enables a programmer to traverse a container, particularly lists.
# define function:
1.def unfold(fn, seed):
2.def fn_generator(val):
3.while True:
4.val = fn(val[1])
5.-5 if val == False: break
6.yield val[0]
7.return [i for i in fn_generator([None, seed])]
EXAMPLES
f = lambda n: False if n > 50 else [-n, n + 10]
unfold(f, 10) # [-10, -20, -30, -40,
function firstFunction() {
// parent function
const x = 100; // parent variable / cant be used inside the child function
function secondFunction() {
const y = 60; // child variable / cannot be called in the parent function
return y + x; // return calculation to the child function
}
return secondFunction(); // evoke the second function by returning it to the parent function
}
console.log(firstFunction()); // call the parent function
Comments