/////////////////////////////////////////////////////////////// //Utilities: //range and arange like from python function range(n){ return [...Array(n).keys()]; } //example: range(20).map(i => i*i) [0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361] function arange(start, stop, step){ var a=[start], b=start; while(b<stop){b+=step;a.push(b)} return a; }; //example use: arange(0,100,10) [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100] //add line numbers before displayed code function addLineNums() { var lines = document.getElementById("mycode").innerText.split(/\r\n|\r|\n/).length; var str =""; for(var i = 1; i < lines; i++){ str += i + "\\a ";} document.styleSheets[0].addRule('xmp:before', 'content: "' + str + '"'); } /////////////////////////////////////////////////////////////// //HTML script editor - print functions //to get something like ipython notebook working for javascript <pre id="output"/> <script> function print(result){ document.getElementById('output').innerHTML += JSON.stringify(result) + '\n'; } function clear(){ document.getElementById('output').innerHTML = ''; } function pairwise(list) { if (list.length < 2) { return []; } var first = list[0], rest = list.slice(1), pairs = rest.map(x => [first, x] ); return pairs.concat(pairwise(rest)); } function pairwiseForLoop(list) { var pairs = new Array((list.length * (list.length - 1)) / 2), pos = 0; for (var i = 0; i < list.length; i++) { for (var j = i + 1; j < list.length; j++) { pairs[pos++] = [list[i], list[j]]; } } return pairs; } var result = pairwise(['a', 'b', 'c', 'd', 'e']); console.log(result); print(result) var result1 = pairwiseForLoop(['a', 'b', 'c', 'd', 'e']); console.log(result1); print(result1); //clear(); print("hello"); for(var i = 0; i < 10; i++){ print(i); } </script>