In: Computer Science
Activity 10: Review
Comparison Operators
Comparison operators allow you to test whether values are
equivalent or whether values are identical.
Conditional Code
Sometimes you only want to run a block of code under certain
conditions. Flow control — via if and else blocks — lets you run
code only under certain conditions.
Switch Statement
Rather than using a series of if/else if/else blocks, sometimes it
can be useful to use a switch statement instead. [Definition:
Switch statements look at the value of a variable or expression,
and run different blocks of code depending on the value.]
Loops
Loops let you run a block of code a certain number of times. Note
that in Loops even though we use the keyword var before the
variable name i, this does not “scope” the variable i to the loop
block
For Loop
A for loop is made up of four statements and has the following
structure:
The initialisation statement is executed only once, before the loop
starts. It gives you an opportunity to prepare or declare any
variables.
The conditional statement is executed before each iteration, and
its return value decides whether or not the loop is to continue. If
the conditional statement evaluates to a falsey value then the loop
stops.
The iteration statement is executed at the end of each iteration
and gives you an opportunity to change the state of important
variables. Typically, this will involve incrementing or
decrementing a counter and thus bringing the loop ever closer to
its end.
The loopBody statement is what runs on every iteration. It can
contain anything you want. You’ll typically have multiple
statements that need to be executed and so will wrap them in a
block ( {...}).
While Loop
A while loop is similar to an if statement, except that its body
will keep executing until the condition evaluates to false.
An example for a while loop:
The do-while loop
This is almost exactly the same as the while loop, except for the
fact that the loop’s body is executed at least once before the
condition is tested.
An example for a do-while loop:
Functions
Functions contain blocks of code that need to be executed
repeatedly. Functions can take zero or more arguments, and can
optionally return a value.
Functions can be created in a variety of ways:
Function Declaration
Named Function Expression
A Simple Function
A Function Returns a Value
A Function that Returns another Function
A Self-executing Anonymous Function
A common pattern in JavaScript is the self-executing anonymous
function. This pattern creates a function expression and then
immediately executes the function. This pattern is extremely useful
for cases where you want to avoid polluting the global namespace
with your code — no variables declared inside of the function are
visible outside of it.
Function as Arguments
In JavaScript, functions are “first-class citizens” — they can be
assigned to variables or passed to other
functions as arguments. Passing functions as arguments is an
extremely common idiom in jQuery.
• Passing an anonymous function as an argument
• Passing a named function as an argument
Task 1 – Write web program to calculate a class average mark. It
should allow users to enter one mark at time for 3 times. See
Lesson 10 slides 33-35 for references.
Task 2 – Write web program to calculate a class average mark. It
should allow users to enter one mark at time for as many times as
they wish to do so. It also allows user to stop by entering
-1.
TASK 1 -
<!DOCTYPE html>
<html>
<title>Web Page Design</title>
<head>
<script>
function findAvg() {
var Avgmarks = 0;//THE TOTAL VARIABLE IS INITIALIZED
TO 0
var arr = [];//THE ARRAY TO SAVE THE NUMBERS
for(var i = 0; i < 3; i++) {
var num = prompt("Enter marks");//TAKES
INPUT
arr.push(+num);//CONVERTS THE STRING INPUT TO
INT AND STORES TO ARRAY
}
for (var i=0; i < arr.length; i++) {
Avgmarks +=
arr[i];//CALCULATES THE TOTAL
var avg =
(Avgmarks/arr.length);//FINDS THE AVERAGE
}
document.write(avg);//PRINTS THE AVERAGE
}
findAvg();
</script>
</head>
<body>
</body>
</html>
-------------------------------------------------------------------------------------------
TASK 2-
<!DOCTYPE html>
<html>
<title>Web Page Design</title>
<head>
<script>
function findAvg() {
// var num = prompt("Enter marks");
var Avgmarks = 0;//THE TOTAL VARIABLE IS INITIALIZED
TO 0
var arr = [];//THE ARRAY TO SAVE THE NUMBERS
while(1) {//INFINITE LOOP TO ENTER AS MANY ENTER
NUMBERS AS MANY TIMES YOU WANT
var num = prompt("Enter marks");//TAKES THE
INPUT
if(+num == -1) break;//IF -1 ENTERED BREAK THE
LOOP
arr.push(+num);//ELSE PUSH THE NUMBER TO
ARRAY
}
for (var i=0; i < arr.length; i++) {
Avgmarks +=
arr[i];//CALCULATES THE TOTAL
var avg =
(Avgmarks/arr.length);//FINDS THE AVERAGE
}
document.write(avg);//PRINTS THE AVERAGE
}
findAvg();
</script>
</head>
<body>
</body>
</html>