Challenge - 5 Problems
Function Parameters Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
β Predict Output
intermediate2:00remaining
Output of function with default parameters
What is the output of the following JavaScript code?
Javascript
function greet(name = "Guest", greeting = "Hello") { return `${greeting}, ${name}!`; } console.log(greet(undefined, "Hi"));
Attempts:
2 left
π‘ Hint
Remember that passing undefined triggers default parameters.
β Incorrect
When you pass undefined as an argument, the default parameter is used. Here, name is undefined, so it defaults to "Guest". The greeting is "Hi" as passed.
β Predict Output
intermediate2:00remaining
Rest parameters and arguments length
What will be logged to the console when this code runs?
Javascript
function sum(...numbers) { return numbers.reduce((a, b) => a + b, 0); } console.log(sum(1, 2, 3, 4));
Attempts:
2 left
π‘ Hint
Rest parameters collect all arguments into an array.
β Incorrect
The rest parameter 'numbers' collects all arguments into an array [1,2,3,4]. The reduce sums them to 10.
β Predict Output
advanced2:00remaining
Parameter destructuring with default values
What is the output of this code snippet?
Javascript
function display({name = "Unknown", age = 0} = {}) { console.log(`${name} is ${age} years old.`); } display({age: 25});
Attempts:
2 left
π‘ Hint
Missing properties use default values in destructuring.
β Incorrect
The object passed has age:25 but no name. So name defaults to "Unknown" and age is 25.
β Predict Output
advanced2:00remaining
Effect of parameter order with default and rest
What will this code output?
Javascript
function test(a, b = 2, ...rest) { console.log(a, b, rest.length); } test(1, undefined, 3, 4);
Attempts:
2 left
π‘ Hint
Undefined triggers default parameter; rest collects remaining arguments.
β Incorrect
a=1, b=undefined triggers default 2, rest collects [3,4] so length is 2.
π§ Conceptual
expert3:00remaining
Understanding parameter shadowing and closures
Consider the following code. What is the value of variable 'result' after execution?
Javascript
let result; function outer(x) { return function inner(x = 5) { result = x; }; } const fn = outer(10); fn();
Attempts:
2 left
π‘ Hint
Inner function parameter shadows outer parameter; default applies when no argument passed.
β Incorrect
The inner function has a parameter x with default 5. Calling fn() passes no argument, so x=5 inside inner, assigned to result.