Skip to content

Instantly share code, notes, and snippets.

@amilajack
Created June 15, 2017 23:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amilajack/18f45ce0e0dea8291f9e159a0799ae78 to your computer and use it in GitHub Desktop.
Save amilajack/18f45ce0e0dea8291f9e159a0799ae78 to your computer and use it in GitHub Desktop.
esnextbin sketch
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>ESNextbin Sketch</title>
<!-- put additional styles and scripts here -->
</head>
<body>
<!-- put markup and other contents here -->
</body>
</html>
// write ES2015 code and import modules from npm
// and then press "Execute" to run your program
const stack = [4, 3, 13, 22, 2, 6, 8]
const tmp = []
while (stack.length > 0) {
foo(stack.pop())
}
function foo(item1) {
console.log(tmp)
if (tmp.length === 0) {
tmp.push(item1)
return
}
const item2 = tmp.pop()
if (tmp.length === 0) {
if (item1 <= item2) {
tmp.push(item2)
tmp.push(item1)
} else {
tmp.push(item1)
tmp.push(item2)
}
return;
}
const item3 = tmp.pop()
if (
item1 >= item3 &&
item1 <= item2
) {
tmp.push(item3)
tmp.push(item1)
tmp.push(item2)
} else {
foo(item1)
tmp.push(item3)
tmp.push(item2)
}
}
// console.clear()
console.log(tmp)
{
"name": "esnextbin-sketch",
"version": "0.0.0"
}
"use strict";
// write ES2015 code and import modules from npm
// and then press "Execute" to run your program
var stack = [4, 3, 13, 22, 2, 6, 8];
var tmp = [];
while (stack.length > 0) {
foo(stack.pop());
}
function foo(item1) {
console.log(tmp);
if (tmp.length === 0) {
tmp.push(item1);
return;
}
var item2 = tmp.pop();
if (tmp.length === 0) {
if (item1 <= item2) {
tmp.push(item2);
tmp.push(item1);
} else {
tmp.push(item1);
tmp.push(item2);
}
return;
}
var item3 = tmp.pop();
if (item1 >= item3 && item1 <= item2) {
tmp.push(item3);
tmp.push(item1);
tmp.push(item2);
} else {
foo(item1);
tmp.push(item3);
tmp.push(item2);
}
}
// console.clear()
console.log(tmp);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment