Quick Index
Let's tackle this one iteratively.

Problem


Write a function that returns the last element in the input array.

Function input: array
Function output: last element in array

Assign the function to a variable named "getLastFct".

Human Language Algorithm


Try #1


Return last element

Try #2


Let variable "lastIndex" = the last index in the array
Let variable "element" = get the element at index "lastIndex"
Return variable "element"

Try #3


//We know that indexes are zero-based (i.e., the first array index is 0)
//I.e., the array [10, 20, 30, 40] has indexes 0, 1, 2 and 3
Let variable "len" = the length of the array
let variable "lastIndex" = "len" minus 1
Let variable "element" = get the element at index "lastIndex"
Return variable "element"

Pseudocode Algorithm


We'll use the human language algorithm (#3) above to write the pseudocode algorithm

Try #1


//Note, we've left "YOUR_CODE_HERE" for you to finish
//See examples on how to get an element at an index
var len, lastIndex, element
len = array.size()
lastIndex = len - 1
element = YOUR_CODE_HERE
return element

Try #2


Let's put in form of function
We know from the examples (see "Function-Basics" page) that multi-line functions look like this:

//Returns max of three numbers
let maxFct = (a, b, c) => {
	let max = (a >= b) ? a : b;
	return (max >= c) ? max : c;
}

Or in general (with return statement):

//CODE-COMMENT
let FUNCTION-NAME = (FUNCTION-PARAMETERS) => {
	FUNCTION-BODY
	return RETURN-VALUE
}

Now let's insert our problem specifics:

//Return the last element in the input array
let getLastFct = (array) => {
	var len, lastIndex, element
	len = array.size()
	lastIndex = len - 1
	element = YOUR_CODE_HERE
	return element
}

Try #3


//We're given a hand calculation we can try

array = [10, 20, 30, 40]
getLastFct(array) outputs 40

//Let's just tweak that a bit for pseudocode

var array, result
array = [10, 20, 30, 40]
result = getLastFct(array)
println(result)

Combine (copy-paste) into full solution:

//Return the last element in the input array
let getLastFct = (array) => {
	var len, lastIndex, element
	len = array.size()
	lastIndex = len - 1
	element = YOUR_CODE_HERE
	return element
}

//Try It
var array, result
array = [10, 20, 30, 40]
result = getLastFct(array)
println(result)