Skip to content

Instantly share code, notes, and snippets.

@Drugak
Last active December 21, 2015 13:13
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 Drugak/97b06eff8b3cecc90e03 to your computer and use it in GitHub Desktop.
Save Drugak/97b06eff8b3cecc90e03 to your computer and use it in GitHub Desktop.
# Exercises
Write a function that inserts an element into a list only if the element to be insertedis larger than any of the elements currently in the list.
Larger can mean either greaterthan when working with numeric values, or further down in the alphabet, whenworking with textual values.
function List () {
this.array = [2,36,"a","d"];
this.add = add;
this.check = check;
this.print = print;
}
function print(data , description){
console.log(data , "==="+ description +"===");
}
function add (data) {
if(this.check(data)) {
this.array.push(data);
this.print(this.array,"succses");
} else {
this.print(this.array,"error");
}
}
function check(data) {
var arrayType = this.array.filter(function(number){
return typeof number == typeof data;
});
function bigger(number){
return data > number;
}
return arrayType.every(bigger);
}
var list = new List();
list.add("c");
JS-Algoritms: List
Lists are one of the most common organizing tools people use in their day-to-day lives.
We have to-do lists, grocery lists, top-ten lists, bottom-ten lists, and many other types.
Our computer programs can also use lists, particularly if we only have a few items tostore in list form.
Lists are especially useful if we don’t have to perform searches on theitems in the list or put them into some type of sorted order.
When we need to performlong searches or complex sorts, lists become less useful, especially with more complexdata structures.
function List () {
this.listSize = 0;
this.pos = 0;
this.dataStore = [];
this.clear = clear;
this.find = find;
this.toString = toString;
this.insert = insert;
this.append = append;
this.remove = remove;
this.front = front;
this.end = end;
this.prev = prev;
this.next = next;
this.length = length;
this.currPos = currPos;
this.moveTo = moveTo;
this.getElement = getElement;
this.length = length;
this.contains = contains;
}
function append (element) {
this.dataStore[this.listSize++] = element;
}
function find(element) {
for (var i = 0; i < this.dataStore.length; ++i) {
if (this.dataStore[i] == element) {
return i;
}
}
return -i
}
function remove(element) {
var foundAt = this.find(element);
if (foundAt > -1) {
this.dataStore.splice(foundAt,1);
--this.listSize;
return true;
}
return false;
}
function length() {
return this.listSize;
}
function toString() {
return this.dataStore.length;
}
function insert(element, after) {
var insertPos = this.find(after);
if(insertPos > -1) {
this.dataStore.slice(insertPos+1, 0, element);
++this.listSize;
return true;
}
return false;
}
function clear() {
delete this.dataStore;
this.dataStore = [];
this.listSize = this.pos = 0;
}
function contains(element) {
for (var i = 0; i < this.dataStore.length; i++) {
if (this.dataStore[i] == element) {
return true;
}
}
return false;
}
function front() {
this.pos = 0;
}
function end() {
this.pos = this.listSize-1;
}
function prev() {
if(this.pos > 0) {
--this.pos;
}
}
function next() {
if(this.pos < this.listSize-1) {
++this.pos;
}
}
function currPos() {
return this.pos;
}
function moveTo(position) {
this.pos = position;
}
function getElement() {
return this.dataStore[this.pos];
}
//=====================
var names = new List();
names.append('Name 1');
names.append('Name 2');
names.append('Name 3');
names.append('Name 4');
names.front();
console.log(names.getElement()); // Name 1
names.next();
names.next();
names.prev();
console.log(names.getElement()); // Name 2
for (names.front(); names.currPos() < names.length(); names.next()) {
console.log(names.getElement());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment