问题描述
我必须遵循以下数组:
var collections=[
{value:20,next:"",prev: "",lastUpdated: false,isLoaked: false,pointer: false},{value:20,pointer: false}
];
使用以下迭代,我可以计算next
和prev
:
for(i = 0; i < collections.length; i++) {
collections[i].next = (i+1) % collections.length;
collections[i].prev = i > 0 ? i-1 : collections.length-1;
}
以下是结果:
var collections=[
{value:20,next:"1",prev: "3",next:"2",prev: "0",next:"3",prev: "1",next:"4",prev: "2",next:"0",pointer: false}
];
基于新集合,如果指针是increased
或decreased
示例:第三项的值已增加到25
var collections=[
{value:20,{value:25,pointer: true},pointer: false}
];
预期结果:
0: {value: 19,next: 1,prev: 4,lastUpdated: true,pointer: false}
1: {value: 19,next: 2,prev: 0,pointer: false}
2: {value: 25,next: 3,prev: 1,pointer: true}
3: {value: 18,next: 4,prev: 2,pointer: false}
4: {value: 19,next: 0,prev: 3,pointer: false}
示例2:如果同一指针的值减小为2。
var collections=[
{value:19,{value:19,{value:23,{value:18,pointer: false}
];
预期以下结果,并遵循prev
:
0: {value: 20,pointer: false}
1: {value: 20,pointer: false}
2: {value: 23,pointer: false}
var collections=[
{value:20,pointer: false}
];
var maxValue = 100;
var totalValue = 0;
var toBedistributedNext = 0;
var toBedistributedPrev = 0;
for(i = 0; i < collections.length; i++) {
collections[i].next = (i+1) % collections.length;
collections[i].prev = i > 0 ? i-1 : collections.length-1;
}
collections[2].value = 25;
collections[2].pointer = true;
collections.forEach(function (collection) {
totalValue += parseInt(collection.value);
});
if (totalValue > maxValue) {
toBedistributedNext = totalValue - maxValue;
} else {
toBedistributedPrev = maxValue - totalValue;
}
var currentPointer = collections.find(item => item.pointer === true);
for (let i = 0; i < toBedistributedNext; i++) {
var nextIndex = currentPointer.next;
if (!collections[i%collections.length].pointer) {
collections[i%collections.length].value--;
collections[i%collections.length].lastUpdated = true;
console.log(collections[i%collections.length])
//currentPointer = collections[i%collections.length].next;
}
}
0: {value: 19,…}
1: {value: 19,…}
2: {value: 25,…}
3: {value: 19,…}
4: {value: 19,…}
最后一个没有计算,请任何人帮助。 总数必须为100 。
我有Jsfiddle:https://jsfiddle.net/tanvir_alam_shawn/zm21pLen/144/
解决方法
您非常接近,但是您的代码中有一些奇怪的事情。
首先,您为当前指针仔细分配:
var currentPointer = collections.find(item => item.pointer === true);
然后在循环中分配下一个指针
for (let i = 0; i < toBeDistributedNext; i++) {
var nextIndex = currentPointer.next;
但是在下一行中,您忽略了所有好的工作:
if (!collections[i%collections.length].pointer)
collections[i%collections.length]
并不指向您当前的指针,请注意在循环开始时i等于0。应该使用{{1}来代替用i
访问指针。 }您已经知道的价值。
此外,您通过示例提高了样本集数组中的五个项的值5的范围,从而对自己毫无帮助。无论如何,看着你的循环,看看问题所在:
nextIndex
将for (let i = 0; i < toBeDistributedNext; i++) {
var nextIndex = currentPointer.next;
if (!collections[i%collections.length].pointer) {
collections[i%collections.length].value--;
collections[i%collections.length].lastUpdated = true;
console.log(collections[i%collections.length])
//currentPointer = collections[i%collections.length].next;
}
}
设置为true怎么办?好吧,值没有改变,但是无论如何我都会增加,因此实际上只进行了4次更改。因此,要修复您的代码,您想使用已经计算出的实际索引,并在当前收集项的指针设置为true时执行某些操作(例如,增量pointer
)。您还希望更新currenntPointer的值,以便正确计算下一个索引。所以循环看起来像这样:
toBeDistributedNext
以下是代码正常工作的代码段:
var currentPointer = collections.find(item => item.pointer === true);
for (let i = 0; i < toBeDistributedNext; i++) {
var nextIndex = currentPointer.next;
if (!collections[nextIndex].pointer) {
collections[nextIndex].value--;
collections[nextIndex].lastUpdated = true;
} else {
toBeDistributedNext++;
}
currentPointer = collections[nextIndex];
}
,
您可以使用一个函数来更新值并检查增量分布。
function update(array,index,value) {
function go(index,direction) {
do {
if (!array[index].isLocked) { // check for locked items
array[index].value += increment;
if (!(delta -= increment)) return;
}
index = array[index][direction];
} while (!array[index].pointer)
}
let delta = array[index].value - value,increment = Math.sign(delta);
array[index].value = value;
array[index].pointer = true;
while (delta) {
go(array[index].prev,'prev');
if (!delta) break;
go(array[index].next,'next');
}
array[index].pointer = false;
}
var collections = [
{ value: 20,next: 1,prev: 4,lastUpdated: false,isLocked: false,pointer: false },{ value: 20,next: 2,prev: 0,next: 3,prev: 1,next: 4,prev: 2,next: 0,prev: 3,pointer: false }
];
update(collections,2,25);
console.log(...collections.map(({ value }) => value));
update(collections,23);
console.log(...collections.map(({ value }) => value));