Bird
Raised Fist0
Google Sheetsspreadsheet~20 mins

Custom functions in Google Sheets - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Custom Functions Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
📊 Formula Result
intermediate
1:30remaining
Output of a simple custom function
You created this custom function in Google Sheets:
function DOUBLE(input) { return input * 2; }

What will be the output in cell A1 if you enter =DOUBLE(5)?
Google Sheets
function DOUBLE(input) { return input * 2; }
A5
B10
CError: Function not found
D25
Attempts:
2 left
💡 Hint
Think about what doubling 5 means.
Function Choice
intermediate
2:00remaining
Choosing the correct custom function for text manipulation
You want a custom function that takes a text string and returns it in uppercase.
Which of these custom functions will work correctly in Google Sheets?
Afunction TOUPPER(text) { return text.toUpper(); }
Bfunction TOUPPER(text) { return text.uppercase(); }
Cfunction TOUPPER(text) { return UPPER(text); }
Dfunction TOUPPER(text) { return text.toUpperCase(); }
Attempts:
2 left
💡 Hint
JavaScript strings have a method to convert to uppercase.
📊 Formula Result
advanced
2:30remaining
Output of a custom function with array input
Given this custom function:
function SUMPLUSONE(range) {
  let sum = 0;
  for (let i = 0; i < range.length; i++) {
    sum += range[i][0];
  }
  return sum + 1;
}

What is the output of =SUMPLUSONE(A1:A3) if cells A1=2, A2=3, A3=4?
Google Sheets
function SUMPLUSONE(range) {
  let sum = 0;
  for (let i = 0; i < range.length; i++) {
    sum += range[i][0];
  }
  return sum + 1;
}
A10
B9
CError: Cannot read property '0' of undefined
D7
Attempts:
2 left
💡 Hint
Sum the values 2 + 3 + 4, then add 1.
🎯 Scenario
advanced
2:00remaining
Debugging a custom function that returns #NAME!
You wrote this custom function:
function CONCATENATE_TWO(a, b) {
  return a + b;
}

When you use =CONCATENATE_TWO("Hello", "World") in your sheet, it shows #NAME!.
What is the most likely reason?
Google Sheets
function CONCATENATE_TWO(a, b) {
  return a + b;
}
ACustom functions must be declared with 'function myFunction()' only.
BThe script is missing the <code>function</code> keyword.
CThe script file is not saved.
DThe function name must be lowercase.
Attempts:
2 left
💡 Hint
Think about what happens if the script is not properly saved.
data_analysis
expert
3:00remaining
Analyzing output of a custom function with nested arrays
Consider this custom function:
function FLATTEN_AND_SUM(input) {
  let flat = input.flat();
  return flat.reduce((a, b) => a + b, 0);
}

If you enter =FLATTEN_AND_SUM({1,2;3,4}) in a cell, what is the output?
Google Sheets
function FLATTEN_AND_SUM(input) {
  let flat = input.flat();
  return flat.reduce((a, b) => a + b, 0);
}
A10
BError: input.flat is not a function
C7
DNaN
Attempts:
2 left
💡 Hint
The input {1,2;3,4} is [[1,2],[3,4]], flat() gives [1,2,3,4], sum is 10.

Practice

(1/5)
1. What is a custom function in Google Sheets?
easy
A. A chart type you add to your sheet
B. A built-in formula like SUM or AVERAGE
C. A filter to sort data automatically
D. A formula you create using JavaScript in Google Apps Script

Solution

  1. Step 1: Understand what custom functions are

    Custom functions are formulas you write yourself using JavaScript in Google Apps Script.
  2. Step 2: Differentiate from built-in features

    Built-in formulas like SUM are pre-made, while custom functions are user-created.
  3. Final Answer:

    A formula you create using JavaScript in Google Apps Script -> Option D
  4. Quick Check:

    Custom function = user-made JavaScript formula [OK]
Hint: Custom functions are your own formulas written in JavaScript [OK]
Common Mistakes:
  • Confusing custom functions with built-in formulas
  • Thinking custom functions are chart types
  • Believing custom functions are filters
2. Which of the following is the correct way to start defining a custom function in Google Apps Script?
easy
A. function MYFUNCTION() { return 'Hello'; }
B. def MYFUNCTION() { return 'Hello'; }
C. func MYFUNCTION() { return 'Hello'; }
D. function: MYFUNCTION() { return 'Hello'; }

Solution

  1. Step 1: Recall JavaScript function syntax

    In Google Apps Script, which uses JavaScript, functions start with the keyword 'function' followed by the name and parentheses.
  2. Step 2: Check each option's syntax

    function MYFUNCTION() { return 'Hello'; } uses correct JavaScript syntax. Options B and C use Python or other language syntax. function: MYFUNCTION() { return 'Hello'; } has incorrect punctuation.
  3. Final Answer:

    function MYFUNCTION() { return 'Hello'; } -> Option A
  4. Quick Check:

    JavaScript function syntax = function MYFUNCTION() { return 'Hello'; } [OK]
Hint: JavaScript functions start with 'function' keyword [OK]
Common Mistakes:
  • Using Python or other language syntax
  • Adding colons after function name
  • Missing parentheses after function name
3. Given this custom function code in Google Apps Script:
function DOUBLE(input) {
  return input * 2;
}

What will be the result of =DOUBLE(5) in a sheet cell?
medium
A. 5
B. DOUBLE(5)
C. 10
D. Error

Solution

  1. Step 1: Understand the function logic

    The function takes an input and returns input multiplied by 2.
  2. Step 2: Calculate the output for input 5

    5 multiplied by 2 equals 10.
  3. Final Answer:

    10 -> Option C
  4. Quick Check:

    5 * 2 = 10 [OK]
Hint: Multiply input by 2 to get output [OK]
Common Mistakes:
  • Expecting the function name as output
  • Thinking it returns input unchanged
  • Assuming syntax error without checking code
4. You wrote this custom function:
function ADDTEN(value) {
  return value + 10
}

But when you use =ADDTEN(5) in the sheet, it shows an error. What is the likely problem?
medium
A. Function name must be lowercase
B. Missing closing brace '}'
C. Function lacks parentheses
D. Missing semicolon after return statement

Solution

  1. Step 1: Check the function syntax carefully

    The function code is missing the closing brace '}' at the end.
  2. Step 2: Understand impact of missing brace

    Without the closing brace, the script is incomplete and causes an error when called.
  3. Final Answer:

    Missing closing brace '}' -> Option B
  4. Quick Check:

    All functions need matching braces [OK]
Hint: Always close function with a brace '}' [OK]
Common Mistakes:
  • Thinking semicolon is mandatory in Apps Script
  • Assuming function name case matters
  • Overlooking missing braces
5. You want a custom function that takes a range of numbers and returns the sum of only the positive numbers. Which of these custom functions will work correctly?
hard
A. function SUMPOS(range) { let total = 0; for (let i = 0; i < range.length; i++) { if (range[i][0] > 0) total += range[i][0]; } return total; }
B. function SUMPOS(range) { return range.reduce((a,b) => a + b); }
C. function SUMPOS(range) { let total = 0; for (let i in range) { if (range[i] < 0) total += range[i]; } return total; }
D. function SUMPOS(range) { return Math.sum(range); }

Solution

  1. Step 1: Analyze each function's logic

    function SUMPOS(range) { let total = 0; for (let i = 0; i < range.length; i++) { if (range[i][0] > 0) total += range[i][0]; } return total; } loops through the range and adds only positive numbers. function SUMPOS(range) { return range.reduce((a,b) => a + b); } sums all numbers without filtering. function SUMPOS(range) { let total = 0; for (let i in range) { if (range[i] < 0) total += range[i]; } return total; } adds negative numbers only. function SUMPOS(range) { return Math.sum(range); } uses a non-existent Math.sum function.
  2. Step 2: Identify correct filtering and summing

    Only function SUMPOS(range) { let total = 0; for (let i = 0; i < range.length; i++) { if (range[i][0] > 0) total += range[i][0]; } return total; } correctly filters positive numbers and sums them.
  3. Final Answer:

    The function that sums only positive numbers -> Option A
  4. Quick Check:

    Filter positive numbers, then sum [OK]
Hint: Loop and add only if number > 0 [OK]
Common Mistakes:
  • Summing all numbers without filtering
  • Adding negative numbers by mistake
  • Using non-existent functions like Math.sum