Quick Index
Index
Overview


This section contains the menu of methods for type "MyArray" with descriptions and examples.

Constructors


Static factory constructor
Example:
let a = MyArray.from([10, 20, 30, 40])
Return a new array with elem count of "sz"
Elements are all initialized to null
Example:
let a = MyArray.fromSize(10)


Helpers


Return my size (length)
In other words the number of elements
Example:
let array = MyArray.from([10, 20, 30, 40])
log('size: ' + array.size())
//size: 4
Returns a "nice string" (one-line) about this object
Example:
let array = MyArray.from([10, 20, 30, 40])
log(array.toString())
//elements: [10, 20, 30, 40]
I return my array element at the index passed in
Indexes are zero-based 0, 1, 2, ...
Example:
let array = MyArray.from([10, 20, 30, 40])
log('get(0): ' + array.get(0))
//get(0): 10
log('get(2): ' + array.get(2))
//get(0): 30
Sets (replaces) the element at "index"
with "newElem"
Example:
let array = MyArray.from([10, 20, 30, 40])
array.set(1, 101)
log(array.toString()))
//elements: [10, 101, 30, 40]
array.set(3, 999)
log(array.toString()))
//elements: [10, 101, 30, 999]
Returns true if this array equals arg "anArray".
Arg "anArray" is a "MyArray"
Example:
let a1 = MyArray.from([10, 20, 30])
let a2 = MyArray.from([10, 20, 30])
let a3 = MyArray.from([99, 20, 30])
log('Compare a1 and a2: ' + a1.equals(a2))
//Compare a1 and a2: true
log('Compare a1 and a3: ' + a1.equals(a3))
//Compare a1 and a3: false


Public


Return index of first elem in our elements that matches
arg "anElem". If no match then return -1.
Example:
var elements, a
elements = [10, 20, 30, 40]
a = MyArray.from(elements);
log(a)
log('indexOf(30): ' + a.indexOf(30));
log('indexOf(99): ' + a.indexOf(99));
// elements: 10,20,30,40
// indexOf(30): 2
// indexOf(99): -1
Return a new MyArray object
that contains our elements reversed.
Example:
var elements, a
elements = [10, 20, 30]
a = MyArray.from(elements);
log(a)
log('reversed(): ' + a.reversed());
// elements: 10,20,30
// reversed(): elements: 30,20,10
Return a new MyArray object where elements are "mapped"
(converted) to new elements using arg "fct".
Example:
var elements, a, fct
elements = [10, 20, 30]
a = MyArray.from(elements);
fct = (elem) => 3 * elem
log(a)
log('map(fct): ' + a.map(fct))
// elements: 10,20,30
// map(fct): elements: 30,60,90
Return our elements joined
into a string where each element is
separated by arg "separator"
Example:
var elements, a
elements = [10, 20, 30]
a = MyArray.from(elements)
log(a)
log('join("|"): ' + a.join("|"))
// elements: 10,20,30
// join("|"): 10|20|30
Return our elements joined
into a string where each element is
separated by arg "separator"
Example:
var elements, a
elements = [10, 20, 30, 40]
a = MyArray.from(elements)
log(a)
log('includes(30): ' + a.includes(30))
log('includes(35): ' + a.includes(35))
// elements: 10,20,30,40
// includes(30): true
// includes(35): false
Write an algorithm named "findClosestToEnd" to find
the matching element that is closest to either end.
If we do not find match, return -1
Return the matching index.
Example:
var elements, a, compareFct
elements = [5, 10, 0, 10, 8, 3];
a = MyArray.from(elements);
log(a);
log('----')
compareFct = elem => elem.equals(10)
log('findClosestToEnd(compareFct for 10) (execting 1)')
log(a.findClosestToEnd(compareFct))
log('----')
compareFct = elem => elem.equals(8)
log('findClosestToEnd(compareFct for 8) (execting 4)')
log(a.findClosestToEnd(compareFct ))
// elements: 5,10,0,10,8,3
// ----
// findClosestToEnd(10) (execting 1)
// 1
// ----
// findClosestToEnd(10) (execting 4)
// 4
Return index of last elem in our elements that matches
arg "anElem". If no match then return -1.
Example:
var elements, a
elements = [30, 10, 20, 30, 40]
a = MyArray.from(elements);
log(a)
log('lastIndexOf(30): ' + a.lastIndexOf(30));
log('lastIndexOf(99): ' + a.lastIndexOf(99));
// elements: 30,10,20,30,40
// lastIndexOf(30): 3
// lastIndexOf(99): -1
Write an algorithm named "findIndex" to find
the *first* element that satisfies searchFct.
Arg "searchFct" returns true if we have
found an element satisfying the search.
Usage: searchFct(anElem)
Returns the matching index.
If no match found, return -1.
Algorithm
Example:
var a, searchFct
a = new MyArray([10, 20, 30, 40, 50, 60, 70])
searchFct = anElem => anElem > 50
log(a.findIndex(searchFct))
// outputs 5
a = new MyArray([10, 50, 10, 40, 50, 60, 10])
searchFct = anElem => anElem == 50
log('----')
log(a.findIndex(searchFct))
// outputs 1
Write an algorithm named "findLastIndex" to find
the *last* matching element and return its index.
Arg "searchFct" returns true if we have
found an element satisfying the search.
Usage: searchFct(anElem)
Returns the matching index.
If no match found, return -1.
Algorithm
Example:
var a, searchFct
a = new MyArray([10, 20, 30, 40, 50, 60, 70])
searchFct = anElem => anElem < 50
log(a.findLastIndex(searchFct))
// outputs 3
a = new MyArray([10, 50, 10, 40, 50, 60, 10])
searchFct = anElem => anElem == 50
log('----')
log(a.findLastIndex(searchFct))
// outputs 4
occurrences(matchFct)
Return a count (number) of the number of occurrences where
matchFct returns true over our elements.
Expected Output (Running Algorithm)
var a, count, matchFct
a = new MyArray([10, 50, 10, 40, 50, 60, 10])
count = 0
//match if between 20 and 50 (inclusive)
matchFct = anElem => anElem >= 20 && anElem <= 50
println(a)
println('occurrences between 20 and 50 (inclusive)')
println('occurrences(matchFct): ' + a.occurrences(matchFct));
filter(fct)
Return a new MyArray object where elements are "filtered"
(kept). By "selectFct" we mean that if the function
returns true, for a given element, then that elem
is kept as part of the result.
Note: MyArray do [not] support adding elements, so
this algo may take some special thought.
Expected Output (Running Algorithm)
var a, selectFct;
a = new MyArray([10, 50, 10, 40, 50, 60, 10]);
//filter (keep) all elements less or equal to 20
selectFct = anElem => anElem <= 20;
println(a)
println(a.filter(selectFct))
//select all elements greater than 100
//should result in empty array
selectFct = anElem => anElem > 100
println('----')
println(a)
println(a.filter(select))
reject(fct)
Return a new MyArray object where elements are "filtered out"
(rejected). By "rejectFct" we mean that if the function
returns true, for a given element, then that elem
is rejected as part of the result (otherwise it's kept)
Note: MyArray do [not] support adding elements, so
this algo may take some special thought.
Expected Output (Running Algorithm)
var a, rejectFct;
a = new MyArray([10, 50, 10, 40, 50, 60, 10]);
//reject all elements less or equal to 20 (keep others)
rejectFct = anElem => anElem <= 20;
println(a)
println(a.filter(rejectFct))
//reject all elements greater than 100 (keep others)
//should result in keeping all
rejectFct = anElem => anElem > 100
println('----')
println(a)
println(a.filter(rejectFct))