JSFiddle - React, Tailwind, and code Playground
by evgkch
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
<div id="text"></div>
<script async src="//jsfiddle.net/evgkch/617opma8/8/embed/js,html,css,result/dark/"></script>
JavaScript
Array.prototype.min = function() {
return Math.min.apply(null, this);
};
var docBreaks = [1, 10, 25, 50, 100, 250, 500, 1000, 2500, 5000, 10000];
var partnumber = "max232";
var qty = 190;
var bestPricesObj = {
"getBestPrices" : [],
"getNameBestPrices" : [],
"printBestPrices" : [],
};
var getDstrbtrs = ["DK", "Mouser", "Avnet"];
var printfn = function(a){
//or replace 'document.getElementById('text').innerHTML +=' with 'console.log'
document.getElementById('text').innerHTML += "<pre>" + a + "<br>";
//console.log(a);
};
var getPartInfo = function (index, qty, name, docBreaks, bestPricesObj) {
var docBreaksLength = docBreaks.length;
printfn("Name: " + name);
this.name = name;
var priceInit = _.range(10, 4.5, -0.5);
printfn("Price Init: " + priceInit);
this.stockPrices = _.map(priceInit, function(el) {return parseFloat((el * Math.random()).toFixed(4))}); //Цены на позицию со стока
printfn(this.stockPrices);
this.stockSum = []; //Сетка суммы на позицию
//Создает массив суммы со стока
_.each(docBreaks, function(el, index){this.stockSum.push(parseFloat((this.stockPrices[index] * el).toFixed(2)));});
//Определяет номер позиции для дальнейших расчетов
var slicePosition = _.find(docBreaks, function(el){return qty >= el;})
// ???Алгоритм нужен, если лучшая цена на позицию находится не в следующей ячейке массива, а через одну и т.д.
var minStockSum = this.stockSum.slice(slicePosition).min();
printfn("Slice position: " + slicePosition);
printfn("Before slice: " + this.stockSum);
printfn("Slice: " + this.stockSum.slice(slicePosition));
printfn("Minimum: " + minStockSum);
printfn("");
var pos = this.stockSum.indexOf(minStockSum);
this.bestQty = qty;
this.bestPrice = this.stockPrices[slicePosition];
this.bestSum = this.stockSum[slicePosition];
bestPricesObj.printBestPrices[index] = this.name + ": $" + this.bestPrice;
...