default params / destructuring
by trentHarlem
JavaScript
function plywoodCutter(height = 4, width = height * 2) {
// 'cut' the wood to new dimensions
// return new dimensions
return [height, width]
}
//console.log(plywoodCutter(),'4,8')
//console.log(plywoodCutter(24),'24,48')
// All or No params declared like this must be entered when invoking
function f([x, ...y] = [1, 2], {
z: z
} = {
z: 3
}) {
console.log('args',x,y,z)
y = (y.flat().length > 1) ? y = y.flat(2).reduce((a, c) => a + c, 0) : y = y.reduce((a, c) => a + c, 0)
//y = +(y.flat().join('+'))
//console.log('y',y)
console.log('args----after', x, y, z)
return x + y + z
}
console.log(
f(), // 6 1+2+3,
f([2, 3], {z: 4}), // 9
f( [2] ,
) , // 9
f([2, [3, 3]], {
z: 4
}), // 9
)
function fib() {
let a = 0,
b = 1;
while (a < 200) {
console.log(a);
[a, b] = [b, a + b]
}
}
fib()