100% found this document useful (1 vote)
109 views8 pages

CS114 Using Selection/Iteration Constructs and Functions

The document discusses selection/iteration constructs and functions in JavaScript. It defines selection constructs as controlling program flow based on conditions, and iteration constructs as repeating steps. It explains that functions allow grouping of statements to perform a task, are defined with the function keyword followed by name and parameters, and are called elsewhere in a program using the function name followed by arguments in parentheses. It provides examples of a function to calculate the area of a square and calling that function. It then presents 4 exercises for readers to write JavaScript functions to calculate totals/averages, tax amounts, and display capital cities using if/else and switch-case constructs.

Uploaded by

api-26450504
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
109 views8 pages

CS114 Using Selection/Iteration Constructs and Functions

The document discusses selection/iteration constructs and functions in JavaScript. It defines selection constructs as controlling program flow based on conditions, and iteration constructs as repeating steps. It explains that functions allow grouping of statements to perform a task, are defined with the function keyword followed by name and parameters, and are called elsewhere in a program using the function name followed by arguments in parentheses. It provides examples of a function to calculate the area of a square and calling that function. It then presents 4 exercises for readers to write JavaScript functions to calculate totals/averages, tax amounts, and display capital cities using if/else and switch-case constructs.

Uploaded by

api-26450504
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 8

CS114 Using Selection/Iteration Constructs and Functions

1.0 Selection Constructs

6-1
Using Selection/Iteration Constructs and Functions CS114

6-2
CS114 Using Selection/Iteration Constructs and Functions

2.0 Iteration Constructs

6-3
Using Selection/Iteration Constructs and Functions CS114

3.0 Functions in JavaScript

A collection of program statements maybe grouped into a function to perform a


particular task.

A function begins with the keyword function followed by the name assigned to the
function and a pair of parentheses. Usually some information need to be passed to the
function as parameters.

4.0 Example

function area_of_square(length)
{
var area=length*length;
return area;
}

5.0 Calling functions

A function does not execute automatically.


To execute a function, you must call it from elsewhere in the program.
To call the function, you must create a statement that includes the function name,
followed by a parenthesis containing any variables or values to be assigned to the
function’s arguments.

Using the previous example, the function call would be area_of_square(10) .

6-4
CS114 Using Selection/Iteration Constructs and Functions

6.0 Exercise 1

Write a function in JavaScript that calculates the total and the average of three integer
values. The program should display the total and the average in an alert window.

Solution

6-5
Using Selection/Iteration Constructs and Functions CS114

7.0 Exercise 2

Write a simple program that calculates the tax to be paid by the customer. The program
should read in the total purchase value and add in the tax rate of 3.0%. The tax
amount and the total purchase value with tax should be displayed.

Solution

6-6
CS114 Using Selection/Iteration Constructs and Functions

8.0 Exercise 3

Write a JavaScript function that displays the capital cities in South East Asia when the
user keys in the country. The program should use an if else construct.

Solution

6-7
Using Selection/Iteration Constructs and Functions CS114

9.0 Exercise 4

Write a JavaScript function that displays the capital cities in South East Asia when the
user keys in the country. The program should use a switch-case construct

Solution

6-8

You might also like