Skip to content

Instantly share code, notes, and snippets.

@mindoftea
Created June 21, 2014 15:03
Show Gist options
  • Save mindoftea/ed4f96de58101a71d737 to your computer and use it in GitHub Desktop.
Save mindoftea/ed4f96de58101a71d737 to your computer and use it in GitHub Desktop.
A node.js program that helps users sort a list based on subjective criteria.
"use strict";
var Prompt=require('prompt');
Prompt.start();
var LOW={};
var HIGH={};
var sortedList=[LOW,HIGH];
var unSortedList=[
"Journey to Babel",
"Amok Time",
"The City on the Edge of Forever",
"The Enterprise Incident",
"The Tholian Web",
"The Doomsday Machine",
"The Menagerie",
"What Are Little Girls Made Of?",
"Balance of Terror",
"Space Seed",
"Mirror, Mirror",
"The Immunity Syndrome",
"Arena",
"The Trouble With Tribbles",
"The Devil in the Dark",
];
var printList=function()
{
console.log("\n"+sortedList.join("\n")+"\n");
};
var check=function(optionA,optionB,callBack)
{
var promptUser;
if(optionB===LOW || optionA==HIGH)
{
callBack(false);
return undefined;
}
else if(optionA===LOW || optionB==HIGH)
{
callBack(true);
return undefined;
}
console.log("Which is better?");
console.log("\tA: "+optionA);
console.log("\tB: "+optionB);
promptUser=function()
{
Prompt.get(
["A / B"],
function(err,result)
{
console.log();
if(result["A / B"]==="a")
{
callBack(true);
}
else if(result["A / B"]==="b")
{
callBack(false);
}
else if(result["A / B"]==="l")
{
printList();
promptUser();
}
else
{
promptUser();
}
}
);
};
promptUser();
};
var place=function(toBePlaced,zoneStart,zoneEnd,callBack)
{
var pivotPoint,pivotValue;
if(zoneEnd-zoneStart===1)
{
sortedList.splice(zoneStart+1,0,toBePlaced);
callBack();
return undefined;
}
pivotPoint=zoneStart+Math.round((zoneEnd-zoneStart-1)/2);
pivotValue=sortedList[pivotPoint];
check(
toBePlaced,pivotValue,
function(t)
{
if(t)
{
place(
toBePlaced,
zoneStart,
pivotPoint,
callBack
);
}
else
{
place(
toBePlaced,
pivotPoint,
zoneEnd,
callBack
);
}
}
);
};
var sort=function()
{
var sortNext,n;
console.log();
sortedList.splice(1,0,unSortedList[unSortedList.length-1]);
n=unSortedList.length-1;
sortNext=function()
{
if(n===0)
{
printList();
return undefined;
}
console.log("Progress: "+Math.round(10000*(unSortedList.length-n-1)/unSortedList.length)/100+"%");
n--;
place(
unSortedList[n],
0,
sortedList.length,
sortNext
);
};
sortNext();
};
sort();
// Repeatedly asks the user questions of the form:
// "Which is better?"
// "A: Mirror, Mirror"
// "B: A Private Little War"
// The program then uses this data to construct a ranked list based on the user's preferences.
// This program will work correctly on any list of strings.
// The options included here are episodes from Star Trek, the original series.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment