λ Reversing an array

======= Algorithm ======= <syntax js> function reverseArray(a) { var len = a.length; var halfLen = Math.floor(len/2); for (var i=0; i<halfLen; i++) { var temp = a[i]; a[i] = a[len-1-i]; a[len-1-i] = temp; } return a; }</syntax> ======= Support ======= <syntax js> function run(input) { result = reverseArray(input); }</syntax> ======= Tests ======= <syntax js> // Test by splitting to an array and then joining back to a string. function testTest() { assert(reverseArray("test".split("")).join("") === "tset"); } function testHello() { assert(reverseArray("hello".split("")).join("") === "olleh"); } function testLong() { assert(reverseArray("By editing this page you agree to license".split("")).join("") === "esnecil ot eerga uoy egap siht gnitide yB"); } function testNothing() { assert(reverseArray("".split("")).join("") === ""); } function testA() { assert(reverseArray("A".split("")).join("") === "A"); }</syntax> ======= Options ======= <syntax js> { "title":"Reversing an array", "height":"170px" }</syntax> ======= Visualisation ======= <syntax html> <html> <head> <style type="text/css"> input {width:100px;} body {font-family:monospace} #start {color:blue; text-decoration:underline} </style> <script type="text/javascript"> function update(n, x) { var element = document.getElementById("result"); var a = x.lookupInScope("a"); var i = x.lookupInScope("i"); var temp = x.lookupInScope("temp"); var result = x.lookupInScope("result"); element.innerHTML = ""; var stringArray = "?"; if (result) { stringArray = result.join(""); } else if (!a) { stringArray = document.getElementById('input').value; } else { stringArray = a.join(""); } /*if (typeof i !== "undefined") { element.innerHTML += "<span id=start>"+binary.slice(0, i+1) + "</span>" + binary.slice(i+1) +"<br>"; } else if (typeof result !== "undefined") { element.innerHTML += "<span id=start>"+binary+"</span><br>"; } else*/ { element.innerHTML += stringArray+"<br>"; } /*if (result) { element.innerHTML += "= "+result; } else if (!decimal) { element.innerHTML += "= 0"; } else { element.innerHTML += "= "+decimal; }*/ } function args() { var input = document.getElementById('input').value.split(""); return input; } function reverse() { reset(); } </script> </head> <body> <input id="input" value="Algorithms"> <button id="reverse" onclick="reverse()">Reverse</button> <div id="result"></div> </body> </html></syntax>