Search Challenge

by aholdorf

HTML

<div id="instructionsDiv">
Generate a random list of numbers from 1 to the given max value (inclusive).
<br/>
You can navigate through the list sequentially or by navigating to a specific index.
<br/>
Sorting the list will reset you to the first item in the list.
</div>
<div id="contentDiv">
<div id="controlDiv" class="left">
<input type="button" id="sortButton" value="Sort!" class="right">
<br/><br/>
<div id="listDetailsDiv">
Max value: <input type="text" id="maxField" value="1000" class="right">
<br/><br/>
Count: <input type="text" id="countField" value="10" class="right">
<br/><br/>
<input type="button" id="generateButton" value="Generate New List" class="right">
<br class="clear"/>
</div>
</div>
<div id="copiableDiv" class="hidden">
<textarea id="textPane" rows="10" cols="80" wrap="off"></textarea>
</div>
<div id="sequentialDiv">
<input type="button" id="leftButton" value="Previous Item" class="left invisible">
<div id="currentItem" class="left"></div>
<input type="button" id="rightButton" value="Next Item" class="left">
<div id="indexDiv" class="left">
<span id="indexSpan"></span>
<br/><br/>
Go to item #:<br/>
<input type="text" id="indexField"><br/>
<input type="button" id="indexButton" value="Go!">
</div>
</div>
</div>

CSS

#currentItem{
  width: 100px;
  height: 100px;
  margin: 50px;
  font-size: 500%;
}
input{
  margin: 5px;
}
#contentDiv{
  margin: 10px;
}
#indexDiv{
  padding: 5px;
  border: 1px solid black;
}
#listDetailsDiv{
  border: 1px solid black;
  padding: 5px;
}
.left{
  float: left;
}
.right{
  float: right;
}
.clear{
  clear: both;
}
.hidden{
  display: none;
}
.invisible{
  visibility: hidden;
}
.hint{
  font-size: 75%;
  font-style: italic;
}
#inputDiv, #outputDiv{
  width: 40%;
  padding: 10px;
}
#buttonDiv{
  width: 10%;
  text-align: center;
  padding-top: 30px;
}
#runButton{
  padding: 5px;
}
#inputArea, #outputArea{
  width: 100%;
  height: 200px;
}

/* The switch - the box around the slider */
.switch {
  position: relative;
  display: inline-block;
  width: 40px;
  height: 15px;
}

/* Hide default HTML checkbox */
.switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

/* The slider */
.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #ccc;
  -webkit-transition: .4s;
  transition: .4s;
}

.slider:before {
  position: absolute;
  content: "";
  height: 11px;
  width: 11px;
  left: 2px;
  bottom: 2px;
  background-color: white;
  -webkit-transition: .4s;
  transition: .4s;
}

input:checked + .slider {
  background-color: #099;
}

input:focus + .slider {
  box-shadow: 0 0 1px #099;
}

input:checked + .slider:before {
  -webkit-transform: translateX(26px);
  -ms-transform: translateX(26px);
  transform: translateX(26px);
}

/* Rounded sliders */
.slider.round {
  border-radius: 34px;
}

.slider.round:before {
  border-radius: 50%;
}

JavaScript

var list = [];
var count = 10;
var cols = 20;
var max = 1000;
var currentIndex = 0;
var sequentialSearch = true;

$("#sortButton").click(function(){
	sortList();
  if(sequentialSearch){
    currentIndex = 0;
  	updateSequentialDisplay();
  }else{
  	displayCopiable();
  }
});

function sortList(){
	list.sort(function(a, b){return a - b});
}

function generateList() {
	list = [];
  for (var i = 0; i < count; i++) {
    list.push(Math.floor(Math.random() * max) + 1);
  }
  if(sequentialSearch){
    updateSequentialDisplay();
  }else{
  	displayCopiable();
  }
}

function displayCopiable() {
	$("#sequentialDiv").addClass("hidden");
  $("#copiableDiv").removeClass("hidden");
	var output = "";
  for(var i=0;i<list.length;i++){
  	output += to3Digit(list[(i%cols)*cols+Math.floor(i/cols)]) + "\t" + (cols<=10?"  ":"");
    if((i+1) % cols == 0){
    	output += "\n";
    }
  }
  $("#textPane").val(output);
}

function to3Digit(num){
	while((num+"").length < 3){
  	num += " ";
  }
  return num;
}

$(document).ready(function() {
  generateList();
});

$("#rightButton").click(function(){
	currentIndex++;
  if(currentIndex >= count){
  	currentIndex = count-1;
  }
  updateSequentialDisplay();
});

$("#leftButton").click(function(){
	currentIndex--;
  if(currentIndex < 0){
  	currentIndex = 0;
  }
  updateSequentialDisplay();
});

function numDigits(num){
	return (num+"").length;
}

function updateSequentialDisplay(){
	$("#currentItem").html(list[currentIndex]);
  $("#indexSpan").html("Currently viewing item " + (currentIndex+1) + " out of " + count);
  if(currentIndex == 0){
  	$("#leftButton").addClass("invisible");
  }else{
  	$("#leftButton").removeClass("invisible");
  }
  if(currentIndex == count-1){
  	$("#rightButton").addClass("invisible");
  }else{
  	$("#rightButton").removeClass("invisible");
  }
}

$("#generateButton").click(function(){
	count = $("#countField").val();
  max = $("#maxField").val();
  currentIndex = 0;
 ...