Challenge - 5 Problems
Function Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
β Predict Output
intermediateOutput of repeated code without functions
What is the output of this JavaScript code that repeats similar logic without using functions?
Javascript
console.log(5 + 3); console.log(10 + 2); console.log(7 + 8);
Attempts:
2 left
π‘ Hint
Look at each console.log output separately.
β Incorrect
Each console.log prints the sum of two numbers. They print 8, then 12, then 15 on separate lines.
π§ Conceptual
intermediateWhy use functions instead of repeating code?
Why do programmers use functions instead of writing the same code multiple times?
Attempts:
2 left
π‘ Hint
Think about saving time and effort.
β Incorrect
Functions help avoid repeating code, making programs shorter and easier to maintain.
β Predict Output
advancedOutput of function usage for repeated addition
What is the output of this code that uses a function to add two numbers?
Javascript
function add(a, b) { return a + b; } console.log(add(5, 3)); console.log(add(10, 2)); console.log(add(7, 8));
Attempts:
2 left
π‘ Hint
The function returns the sum of two numbers.
β Incorrect
The function add returns the sum of its two arguments, so the console logs print 8, 12, and 15.
π§ Conceptual
advancedMain benefit of functions in large programs
What is the main benefit of using functions in large programs?
Attempts:
2 left
π‘ Hint
Think about how to handle complexity.
β Incorrect
Functions break big programs into smaller parts, making them easier to understand and fix.
β Predict Output
expertOutput of function with side effect and return
What is the output of this code that uses a function to print and return a value?
Javascript
function greet(name) { console.log('Hello, ' + name + '!'); return name.length; } const length = greet('Alice'); console.log(length);
Attempts:
2 left
π‘ Hint
The function prints a greeting and returns the length of the name.
β Incorrect
The function first prints 'Hello, Alice!' then returns 5, which is printed next.
