top of page
logo-2-color_2x.png

JS03 - Functions

Submit Assignment

Points: 

5

Due By:

April 6, 2026 at 4:45:00 AM

Main Question

How do functions help programmers organize code, reuse logic, and decide when to return a value versus cause an effect?





Learning Objectives

You should be able to:

  • explain why functions are useful in larger programs

  • define and call a function correctly

  • use parameters so one function can work with different inputs

  • use return to send a value back to other code

  • distinguish between a function with a side effect and a function with a return value

  • write simple JavaScript functions using declaration syntax and arrow syntax, and solve beginner function exercises similar to the chapter tasks in Eloquent JavaScript






Directions


Part A: Short Response

Answer these in complete sentences.

  1. Why are functions useful in a program?

  2. What is the difference between defining a function and calling a function?

  3. What does a parameter do?

  4. What does return do?

  5. What is the difference between a side effect and a return value?




Part B: Read the Code

For each example, write:

  • the function name

  • the parameter(s), if any

  • whether it mainly has a side effect or returns a value

  • what happens when it runs




function showTrackTitle(title) {
	console.log(title);
}


function double(number) {
  return number * 2;
}


const greet = name => "Hello, " + name;





Part C: Write the Functions

Write code that solves each task.


A. Reusable greeting


Create a function called sayHello that prints:

"Hello from the player!"


B. Track title


Create a function called showTrackTitle with one parameter, title. When called, it should print the title.



C. Volume display


Create a function called showVolume with one parameter, level. When called, it should print:


"Volume: " followed by the level.


D. Double


Create a function called double that takes one number and returns twice that number.



E. Build a label


Create a function called buildTrackLabel with parameters title and artist.


It should return a string in this format:

"Track 1 - Artist A"

F. Return or side effect?


Write:

  • one function that mainly causes a side effect

  • one function that mainly returns a value


Add a comment above each one explaining which kind it is.





Part D: Call and Use The Functions

Use your functions to complete this mini-player task.


Write code that:

  • calls showTrackTitle("Blinding Lights")

  • calls showVolume(70)

  • stores the result of double(5) in a variable called result

  • prints result

  • stores the result of buildTrackLabel("Levitating", "Dua Lipa") in a variable called label

  • prints label





Part E: Extend Your Understanding

Choose 2 of the 3 challenges below.


Challenge 1: Minimum

Write a function min(a, b) that returns the smaller value. This mirrors one of the chapter exercises.



Challenge 2: Count Characters

Write a function countChar(text, char) that counts how many times a character appears in a string. Then write countBs(text) using countChar. This is also based on a chapter exercise.



Challenge 3: Even or Odd with Recursion


Write a recursive function isEven(n):

  • 0 is even

  • 1 is odd

  • otherwise, use n - 2


Test it with:

  • isEven(50)

  • isEven(75)


Then explain what happens with isEven(-1). This comes directly from the chapter’s recursion exercise.






Submission Checklist


Turn in:

  • your short responses for Parts A and B

  • completed code for Parts C and D

  • two completed challenge problems

Locked Message

bottom of page