0
0
Javascriptprogramming~20 mins

Why functions are needed in Javascript - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Function Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output 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);
A8 12 15
B15\n12\n8
C8\n12\n15
DError
Attempts:
2 left
πŸ’‘ Hint
Look at each console.log output separately.
🧠 Conceptual
intermediate
1:30remaining
Why use functions instead of repeating code?
Why do programmers use functions instead of writing the same code multiple times?
ATo make code shorter and easier to reuse
BTo make code run slower
CTo confuse other programmers
DTo increase the number of lines in the program
Attempts:
2 left
πŸ’‘ Hint
Think about saving time and effort.
❓ Predict Output
advanced
2:00remaining
Output 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));
AError
Bundefined\nundefined\nundefined
C15\n12\n8
D8\n12\n15
Attempts:
2 left
πŸ’‘ Hint
The function returns the sum of two numbers.
🧠 Conceptual
advanced
1:30remaining
Main benefit of functions in large programs
What is the main benefit of using functions in large programs?
AThey help organize code into smaller, manageable parts
BThey make programs run without errors automatically
CThey increase the program size to use more memory
DThey prevent the program from running too fast
Attempts:
2 left
πŸ’‘ Hint
Think about how to handle complexity.
❓ Predict Output
expert
2:30remaining
Output 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);
AError
BHello, Alice!\n5
CHello, Alice!\nundefined
D5\nHello, Alice!
Attempts:
2 left
πŸ’‘ Hint
The function prints a greeting and returns the length of the name.