Suggested answers for an accompanying worksheet for the book: Digital Media Primer, YueLing
Wong. Copyright (c)2015 by Pearson Education, Inc. All rights reserved.
Worksheet: Syntax Review and Code Writing Exercises for Part 2 of
Chapter 10
Objectives:
Review the syntax by writing general syntax and example code for:
o if statements
o switch statements
o function definitions
o invoking functions
o functions that return a value
o parameters and arguments
The completed worksheet can be used as a quick reference for programming labs and
studying for tests and exams.
Syntax Review:
if statement
General Syntax:
Example:
if/else statement
General Syntax:
Example:
Page 2 of 13
if/else if statement
General Syntax:
Example:
if (score > 90)
Page 3 of 13
switch statement
General Syntax:
switch ( expression )
Example:
switch (score)
Page 4 of 13
Function Definitions and Function Calls
What are functions and function definitions?
What are function calls?
Page 5 of 13
Function Definitions
General Syntax:
Example:
Invoking a Function
General Syntax:
Page 6 of 13
Function definition
for a function that returns a value
General Syntax:
Invoking a function that returns a value
General Syntax:
Page 7 of 13
Write a function definition
for a function that takes parameters
General Syntax:
Invoking a function that takes parameters
General Syntax:
Page 8 of 13
Write a function definition
for a function that returns a value and takes parameters
General Syntax:
Example:
Invoking a function that returns a value and takes parameters
General Syntax:
Page 9 of 13
Code Writing Exercises:
1. Write a function definition to calculate the sum of two variables.
Then, make a function call (i.e. invoke the function.)
2. Write a function definition to calculate the sum of two variables, and return the sum.
Then, make a function call (i.e. invoke the function.)
3. Write an if statement to do the following.
4. Write an if statement to do the following.
Page 10 of 13
5. What will the value of x be after the following statements are executed?
(a)
var n = 6;
(b)
var n = 6;
(c)
var n = 6;
(d)
var n = 6;
(e)
var n = 6;
var score = 51;
(f)
var n = 6;
var score = 51;
(g)
var n = 6;
var score = 51;
Page 11 of 13
6. Write a switchstatement that does the same as the following if-statement. Assume the
variable reaction has been declared. You don’t need to declare the variable reaction.
if (reaction == 0)
Page 12 of 13
7. Write a function definition (that takes three parameters). One parameter is used as the sales
price, one parameter as the discount, and one parameter as the tax rate. The function is to
calculate the total sales after taking discount off the sales price, and added with tax.
(You can make up a reasonable formula to calculate the total. The purpose of this question
is for you to practice writing a function with parameters; it’s not about actual accounting.)
Then, make a function call (i.e. invoke the function.)
8. Write a function definition that does the same task as the previous question but it also
returns the value of the result.
Then, make a function call (i.e. invoke the function.)
Page 13 of 13