Theme: Logic first, syntax later.

Video for this page...

Starting Point
We are given that we are in a MyArray and the algorithm receives one arg "separator"
join(separator) {
	//Object "this" is a MyArray
	/* Return our elements joined
	into a string where each element is
	separated by arg "separator" */
}
Logic Pass 1
Initialize the result (empty string).
Combine the elements into one string.
join(separator) {
	let result = ''
	iterate over elements (nextElem)
		result += separator + nextElem
	return result
}
Logic Pass 1 - Test
let MyArray with elements ['Kofi', 'Kingston']
let separator = '|'
result = '|Kofi|Kingston'
Check: FAILS -- separator should only be in between elements
join(separator) {
	let result = ''
	iterate over elements (nextElem)
		result += separator + nextElem
	return result
}
Logic Pass 2
Initialize the result (empty string).
This time iterate over indexes
Only add separator if we're "past" first element -- i.e. skip first
join(separator) {
	let result = ''
	iterate over indexes (nextIndex)
		if (nextIndex > 0)
			result += separator
		result += nextElem
	return result
}
Logic Pass 2 - Test
let MyArray with elements ['Kofi', 'Kingston']
let separator = '|'
result = 'Kofi|Kingston'
Okay
join(separator) {
	let result = ''
	iterate over indexes (nextIndex)
		if (nextIndex > 0)
			result += separator
		result += nextElem
	return result
}
Pseudocode - Open Context Menu(s)
We now pull open MyArray's menu of available algorithms for use.

We select a couple that are super helpful for iteration.
Context:
Pseudocode - Writing
We convert our logical ideas to more of a pseudocode feel.
let result = ''
let nextIndex = 0
while (nextIndex < this.size()) {
	let nextElem = this.get(nextIndex)
	if (nextIndex > 0)
		result += separator
	result += nextElem.toString()
}
return result
Oops
And we continue testing (always) and notice we have an infinite loop.

So we add the incrementor.
let result = ''
let nextIndex = 0
while (nextIndex < this.size()) {
	let nextElem = this.get(nextIndex)
	if (nextIndex > 0)
		result += separator
	result += nextElem.toString()
	nextIndex++
}
return result