// Your code at the beginning of the file.
// Символы Юникод
"use strict;"


function Demo() {



// Autogenerated with DRAKON Editor 1.31


function alt_select_test() {
    // item 265
    alternative_select(-100, -1);
    alternative_select(100, 1);
    alternative_select(0, 0);
}

function alternative_select(value, expected) {
    // item 273
    var actual;
    // item 2740001
    if (value < 0) {
        // item 282
        actual = -1;
    } else {
        // item 2740002
        if (value === 0) {
            // item 283
            actual = 0;
        } else {
            // item 284
            actual = 1;
        }
    }
    // item 285
    if (actual === expected) {
        
    } else {
        // item 291
        throw "alt select failure!";
    }
    // item 2950001
    if (value < 0) {
        // item 303
        actual = -1;
    } else {
        // item 2950002
        if (value === 0) {
            // item 304
            actual = 0;
        } else {
            // item 2950003
            if (value > 0) {
                
            } else {
                // item 2950004
                throw "Not expected condition.";
            }
            // item 305
            actual = 1;
        }
    }
    // item 306
    if (actual === expected) {
        
    } else {
        // item 311
        throw "alt select failure!";
    }
}

function ascComparer(left, right) {
    // item 258
    return right.localeCompare(left);
}

function compare(comparer, array, leftIndex, rightIndex) {
    // item 245
    var left = array[leftIndex];
    var right = array[rightIndex];
    // item 244
    return comparer(left, right);
}

function fibonacci(n) {
    // item 185
    var result = [0];
    // item 1610001
    if (n === 0) {
        
    } else {
        // item 1610002
        if (n === 1) {
            // item 187
            result.push(1);
        } else {
            // item 188
            result.push(1);
            // item 1710001
            var i = 2;
            while (true) {
                // item 1710002
                if (i <= n) {
                    
                } else {
                    break;
                }
                // item 172
                var f2 = result[i - 2];
                var f1 = result[i - 1];
                var fib = f1 + f2;
                // item 173
                result.push(fib);
                // item 1710003
                i++;
            }
        }
    }
    // item 176
    return result;
}

function foreachDemo() {
    // item 155
    printElement("h2", "Iteration demo");
    // item 153
    var sequence = fibonacci(15);
    // item 154
    printListArrow(sequence);
    printListFor(sequence);
    printListForeach(sequence);
    // item 337
    var user = { email: "user@example.com", name: "Buster" };
    foreachWithKeys(user);
    // item 362
    twoLoopsInOneFunction();
}

function foreachWithKeys(collection) {
    // item 320
    printHeader("Using 'foreach' with keys");
    // item 334
    var list = [];
    // item 3160001
    var _ind316 = 0;
    var _col316 = collection;
    var _keys316 = Object.keys(_col316); 
    var _len316 = _keys316.length;
    while (true) {
        // item 3160002
        if (_ind316 < _len316) {
            
        } else {
            break;
        }
        // item 3160004
        var key = _keys316[_ind316]; var item = _col316[key];
        // item 317
        list.push(key + ":" + item);
        // item 3160003
        _ind316++;
    }
    // item 333
    printArray(list);
}

function main() {
    // item 310
    alt_select_test();
    // item 189
    foreachDemo();
    // item 50
    quicksortDemo();
    // item 260
    var result = 0;
    // item 261
    return result;
}

function printArray(list) {
    // item 329
    var items = list.join(", ");
    // item 330
    printLine(items);
}

function printListArrow(collection) {
    // item 131
    printHeader("Using 'if' and arrow");
    // item 336
    var list = [];
    // item 126
    var length = collection.length;
    var i = 0;
    while (true) {
        // item 127
        if (i < length) {
            
        } else {
            break;
        }
        // item 125
        var item = collection[i];
        list.push(item);
        // item 129
        i++;
    }
    // item 335
    printArray(list);
}

function printListFor(collection) {
    // item 142
    printHeader("Using 'for'");
    // item 332
    var list = [];
    // item 184
    var length = collection.length;
    // item 1390001
    var i = 0;
    while (true) {
        // item 1390002
        if (i < length) {
            
        } else {
            break;
        }
        // item 183
        var item = collection[i];
        list.push(item);
        // item 1390003
        i += 1;
    }
    // item 331
    printArray(list);
}

function printListForeach(collection) {
    // item 119
    printHeader("Using 'foreach'");
    // item 322
    var list = [];
    // item 1150001
    var _ind115 = 0;
    var _col115 = collection;
    var _len115 = _col115.length;
    while (true) {
        // item 1150002
        if (_ind115 < _len115) {
            
        } else {
            break;
        }
        // item 1150004
        var item = _col115[_ind115];
        // item 116
        list.push(item);
        // item 1150003
        _ind115++;
    }
    // item 118
    printArray(list);
}

function quicksort(comparer, collection, begin, end) {
    // item 230
    var i;
    var pivotIndex;
    var storeIndex;
    // item 203
    var length = end - begin;
    var last = end - 1;
    // item 2040001
    if ((length === 0) || (length === 1)) {
        
    } else {
        // item 2040003
        if (length === 2) {
            // item 219
            if (compare(comparer, collection, begin, last) <= 0) {
                
            } else {
                // item 220
                swap(collection, begin, last);
            }
        } else {
            // item 223
            pivotIndex = begin + Math.floor(length / 2);
            // item 231
            swap(collection, pivotIndex, last);
            storeIndex = begin;
            // item 2240001
            i = begin;
            while (true) {
                // item 2240002
                if (i < last) {
                    
                } else {
                    break;
                }
                // item 232
                if (compare(comparer, collection, i, last) < 0) {
                    // item 233
                    swap(collection, i, storeIndex);
                    storeIndex++;
                } else {
                    
                }
                // item 2240003
                i++;
            }
            // item 236
            swap(collection, storeIndex, last);
            // item 237
            quicksort(comparer, collection, begin, storeIndex);
            // item 238
            quicksort(comparer, collection, storeIndex + 1, end);
        }
    }
}

function quicksortDemo() {
    // item 62
    printElement("h2", "Quick sort demo");
    // item 58
    var unsorted = [ "the", "sooner", "we", "start", "this", "the", "better" ];
    var sorted   = [ "aa", "bb", "cc", "dd", "ee", "ff" ];
    var reverse  = [ "ff", "ee", "dd", "cc", "bb", "aa" ];
    var empty    = [];
    var flat     = [ "flat", "flat", "flat", "flat", "flat" ];
    // item 59
    var comparer = ascComparer;
    quicksort(comparer, unsorted, 0, unsorted.length);
    quicksort(comparer, sorted, 0, sorted.length);
    quicksort(comparer, reverse, 0, reverse.length);
    quicksort(comparer, empty, 0, empty.length);
    quicksort(comparer, flat, 0, flat.length);
    // item 61
    stringsAreSorted(unsorted);
    stringsAreSorted(sorted);
    stringsAreSorted(reverse);
    stringsAreSorted(empty);
    stringsAreSorted(flat);
    // item 60
    printLine(unsorted);
    printLine(sorted);
    printLine(reverse);
    printLine(empty);
    printLine(flat);
}

function stringsAreSorted(array) {
    var _sw810000_ = 0;
    var current = null;
    var i = 0;
    var j = 0;
    var length = array.length;
    var _next_item_ = 0;
    _next_item_ = 710001;
    while (true) {
        if (_next_item_ === 710001) {
            i = 0;
            _next_item_ = 710002;
    
        } else if (_next_item_ === 710002) {
            if (i < length) {
                // item 73
                current = array[i];
                _next_item_ = 740001;
            } else {
                return null;
            }
    
        } else if (_next_item_ === 740001) {
            j = i + 1;
            _next_item_ = 740002;
    
        } else if (_next_item_ === 740002) {
            if (j < length) {
                // item 76
                var after = array[j];
                _next_item_ = 810000;
            } else {
                // item 710003
                i += 1;
                _next_item_ = 710002;
            }
    
        } else if (_next_item_ === 810000) {
            _sw810000_ = current.localeCompare(after);
            _next_item_ = 810001;
    
        } else if (_next_item_ === 810001) {
            if (_sw810000_ === 1) {
                // item 90
                _next_item_ = 740003;
            } else {
                _next_item_ = 810002;
            }
    
        } else if (_next_item_ === 810002) {
            if (_sw810000_ === 0) {
                // item 89
                _next_item_ = 740003;
            } else {
                _next_item_ = 810003;
            }
    
        } else if (_next_item_ === 740003) {
            j += 1;
            _next_item_ = 740002;
    
        } else if (_next_item_ === 810003) {
            if (_sw810000_ === -1) {
                _next_item_ = 88;
            } else {
                // item 810004
                throw "Unexpected switch value: " + _sw810000_;
            }
    
        } else if (_next_item_ === 88) {
            _next_item_ = 259;
    
        } else if (_next_item_ === 259) {
            printLine("current = " + current);
            printLine("after = " + after);
            printLine("collection = " + array);
            printLine("result = " + current.localeCompare(after));
            printLine("...not sorted.");
            _next_item_ = 77;
    
        } else if (_next_item_ === 77) {
            throw "Collection is not sorted.";
    
        }
    }
}

function swap(array, leftIndex, rightIndex) {
    // item 252
    var left = array[leftIndex];
    var right = array[rightIndex];
    // item 251
    array[leftIndex] = right;
    array[rightIndex ] = left;
}

function twoLoopsInOneFunction() {
    // item 350
    var point = { x:100, y:50 };
    // item 354
    var list1 = [];
    // item 3510001
    var _ind351 = 0;
    var _col351 = point;
    var _keys351 = Object.keys(_col351); 
    var _len351 = _keys351.length;
    while (true) {
        // item 3510002
        if (_ind351 < _len351) {
            
        } else {
            break;
        }
        // item 3510004
        var key = _keys351[_ind351]; var value = _col351[key];
        // item 355
        list1.push(key + ": " + value);
        // item 3510003
        _ind351++;
    }
    // item 353
    printArray(list1);
    // item 361
    var numbers = [-40, 834, 2998];
    // item 359
    var list2 = [];
    // item 3560001
    var _ind356 = 0;
    var _col356 = numbers;
    var _keys356 = Object.keys(_col356); 
    var _len356 = _keys356.length;
    while (true) {
        // item 3560002
        if (_ind356 < _len356) {
            
        } else {
            break;
        }
        // item 3560004
        var key = _keys356[_ind356]; var value = _col356[key];
        // item 360
        list2.push(key + ": " + value);
        // item 3560003
        _ind356++;
    }
    // item 358
    printArray(list2);
}


// Your code at the end of the file.

	this.main = main
}

var obj = new Demo()


obj.main()
