Quick Index
Purpose


This example is a slight variation of our previous work. In this case we println the collection in reverse.

We'll see how the index comes in handy.

Writing Algorithm


Problem Statement
Problem Statement: Write an algorithm to println a collection of elements in reverse using the "println" method for each element.

Given: Variable 'elements' that holds a collection of objects.
Writing Algorithm in Human Language
Write the algorithm in human language (English).


Human Language:
function printInReverse(list) {
	Let 'len' equal to length of 'elements'
	Let 'i' equal to len - 1
	While 'i' is equal or greater than 0
		Let 'nextElement' equal to the element in 'elements' at index 'i'
		println 'nextElement'
		Let 'i' equal to 'i' minus 1
}
Writing Algorithm in Pseudocode
Write the algorithm in pseudocode.


Pseudocode:
function printInReverse(elements) {
	let len = elements.length
	let i = len - 1;
	while (i >= 0) {
		let nextElem = elements[i]
		println(nextElem)
		i = i - 1
	}
}

//try-it
let list = [10, 20, 30, 40];
printInReverse(list);


Pseudocode Breakdown


Pseudocode Breakdown
let len = elements.length

  • let len = elements.length -- says to let variable "len" have the length of "elements"

let i = len - 1

  • let i = len - 1 -- says to let variable "i" have value "len - 1" -- this is the end of "elements", where we want the iteration to begin

while (i >= 0) {

  • while -- says to do a while (true) loop
  • (i >= 0) -- says to compare if "i" is >= "0" (the start of "elements"), the loop continues if true, or ends if false
  • We could think of this statement generally as "while (CONDITION)"

while (i < elements.length) {
	let nextElem = elements[i]
	println(nextElement.toString())
	i = i + 1
	}
 

  • {} -- The braces define the loop block. The block of code within the braces executes for every loop
  • let nextElem = elements[i] -- says to get the element at index "i" and assign to var "nextElem"
  • println(nextElement.toString()) -- says to print the resulting string
  • i = i + 1 -- says to increment var "i"

Notes:
  • A loop variable is the same as a local variable (the local context is the "loop block").
  • If the loop block contains only one statement, the braces "{}" are optional.

Walking Through Algorithm


We're now going to walk through the algorithm using example data.

Given Data
Let us say that the provided array is a collection of student (objects) as follows:

elements = [Riya, Asha, Chin]
[Riya, Asha, Chin]
printInReverse(elements) {
Initialization
Before beginning the loop we initialize the length variable "len", and the loop variable.
     3
let len = elements.length
    2    3
let i = len - 1
Loop 1
The first loop.
Get the element at index "i" and assign to var "nextElem"
Print out nextElem
Lastly, decrement "i"
       2
while (i >= 0) {
          Chin              2
    let nextElem = elements[i]
                  Chin
    println(nextElement.toString())
    1  2
    i = i - 1
}
Loop 2
Second loop.
Same loop logic except loop var "i" is "1",
and "nextElem" (from elements) is thus "Asha"
       1
while (i >= 0) {
          Asha              1
    let nextElem = elements[i]
                  Asha
    println(nextElement.toString())
    0   1
    i = i - 1
}
Loop 3
Third loop.
Same loop logic except loop var "i" is "0",
and "nextElem" (from elements) is thus "Riya"
       0
while (i >= 0) {
          Riya              0
    let nextElem = elements[i]
                   Riya
    println(nextElement.toString())
   -1   0
    i = i - 1
}
LOOP ENDS
The condition "i >= 0" (-1 >= 0) is false, and thus the loop ends.
      -1
while (i >= 0) {