Last active
October 10, 2015 08:47
-
-
Save f3lan/3664326 to your computer and use it in GitHub Desktop.
Kundenkarte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var ENTRYLIMIT = 148; | |
Function.prototype.method = function(name,func){ | |
this.prototype[name] = func; | |
return this; | |
}; | |
Array.method('find', function(value){ | |
for(var i = 0; i < this.length; i++) { | |
if(this[i] >= value){ | |
return i; | |
} | |
} | |
return this.length; | |
}); | |
Array.method('split', function(value){ | |
i = this.find(value); | |
return [[this.slice(0,i)], [this.slice(i,this.length)]] | |
}); | |
Array.method('desc', function(){ | |
return this.sort(function(a,b){return b-a}); | |
}); | |
Array.method('asc', function(){ | |
return this.sort(function(a,b){return a-b}); | |
}); | |
Array.method('sum', function(){ | |
return this.reduceRight(function(a,b){return a+b}); | |
}); | |
var removeImposibleValues = function(array){ | |
this.array = array.desc(); | |
var first = array[0]; | |
for(var i = array.length; i > 1; i--){ | |
if((first + array[i]) < ENTRYLIMIT){ | |
array.splice(i,1); | |
} | |
} | |
return array; | |
} | |
var magicArrayCalculator = function(array){ | |
var result = []; | |
this.array = removeImposibleValues(array); | |
var a = array.slice(0,(array.length/2)); | |
var b = array.slice((array.length/2),array.length); | |
b = b.asc(); | |
for(var i = 0; i < a.length; i++){ | |
var addition = a[i] + b[i]; | |
if(addition >= ENTRYLIMIT){ | |
result.push(a[i] + b[i]); | |
} | |
} | |
return result; | |
} | |
var Sale = function(articles) { | |
var sum = articles.sum(); | |
var positions = articles.split(ENTRYLIMIT); | |
var card = new Card; | |
var entries = magicArrayCalculator(positions[0][0]); | |
card.write_entries(entries); | |
card.write_entries(positions[1][0]); | |
var nice = sum - card.get_sum(); | |
card.add_to_last_entry(nice); | |
return { | |
get_card : function(){ | |
return card; | |
} | |
} | |
}; | |
var Card = function(){ | |
var positions = new Array(); | |
return { | |
write_entries : function(entries){ | |
for(var i = 0; i < entries.length; i++){ | |
positions.push(entries[i]); | |
} | |
}, | |
add_to_last_entry : function(value){ | |
var temp = positions[positions.length - 1] | |
positions[positions.length - 1] = value + temp; | |
}, | |
get_positions : function(){ | |
return positions.desc(); | |
}, | |
get_sum : function(){ | |
return positions.sum(); | |
}, | |
to_string : function(){ | |
var string = ""; | |
positions = positions.desc(); | |
for(var i = 0; i < positions.length; i++){ | |
string += "amount" + (i + 1) + "=" + positions[i] + "&"; | |
} | |
return string.substring(0, string.length - 1); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment