这是在某些条件下创建数字序列的代码。一切正常,直到我添加以下代码:
for (let i = 0; i < slide_index; i++) {
for (let j = 0; j <= i; j++) {
slices[i] += arrays[j][i - j];
}
}
console.log(slices);
上面的代码会产生此错误:
未捕获的TypeError:无法读取未定义的属性“ 0” 在包装上
这是主要代码:
function arra(sum, length){
var array = [];
var float = 0;
if(length == 1){array.push(sum)} else{
for (let i = 0; i < length; i++) {
let a = 0; // the very first element of array
// Math equations
let last = (sum - (length / 2 * a)) * 2 / length
let dd = (last - a) / (length - 1)
sume = (dd * (i));
if(float_part(sume) > 0.5){ sume = Math.ceil(sume) }
float += float_part(sume)
array.push(int_part(sume));
}
}
// check to see if array elemements sum is equal to "var sum = 10"
let reducer = (accumulator, currentValue) => accumulator + currentValue;
let reducerlast = (accumulator, currentValue) => accumulator + currentValue;
let sumlast = sum - (array.reduce(reducerlast));
array[length - 1] += sumlast
let sumArray = array.reduce(reducer);
//console.log("sumArray: " + sumArray)
return array // results
function int_part(y){
return Math.trunc(y);
}
function float_part(d){
let int_part = Math.trunc(d);
return Number((d-int_part).toFixed(2));
}
}
var arrays = [];
var slices = [0, 0, 0, 0, 0];
var total_slides = 5; // total number of slides we have in the course
var addMe = [];
var total_standard_reviews = 60;
var a1 = 0;
var outputArray = [];
var min = 0;
var diff = [];
function pack(added_plans, slide_index){
let reducer = (accumulator, currentValue) => accumulator + currentValue;
let reducere = (accumulator, currentValue) => accumulator + currentValue;
let reducerx = (accumulator, currentValue) => accumulator + currentValue;
var j = slide_index - 1;
//console.log("j: " + slide_index)
diff.push(added_plans);
//console.log(diff)
if(j > 0) {
min = diff[j] - diff[j - 1];
console.log("min: " + min)
arrays = arrays.concat([arra(min, total_slides - j)]);
}
// ******************* Here is the updated code *****************
//console.log(arrays);
for (let i = 0; i < slide_index; i++) {
for (let j = 0; j < arrays.length; j++) {
slices[i] += arrays[j][i - j];
}
}
console.log(slices);
var total_reviews = (total_standard_reviews + added_plans);
var next = [];
var sum = 0;
//
var last = (total_reviews - (total_slides/2 * a1)) * 2/total_slides
var d = (last - a1) / (total_slides - 1)
for (let i = 1; i < total_slides; i++){
sum += a1 + (d * ( i - 1));
var pusher = int_part(sum);
next.push(pusher);
sum -= pusher
}
let sumer = total_reviews - (next.reduce(reducer));
next.push(sumer)
//
let sumerr = next.reduce(reducere);
//console.log(sumerr)
//console.log(next)
if(slide_index < total_slides){
var output = next[slide_index - 1]
console.log("output: " + output)
outputArray.push(output)
} else {
var outputArraysum = outputArray.reduce(reducerx);
output = total_reviews - outputArraysum ;
console.log("output2: " + output)
}
function int_part(y){
return Math.trunc(y);
}
function float_part(d){
let int_part = Math.trunc(d);
return Number((d-int_part).toFixed(2));
}
}
pack(0, 1);
setTimeout(function(){ pack(5,2); }, 6000);
setTimeout(function(){ pack(10,3); }, 9000);
setTimeout(function(){ pack(15,4); }, 12000);
setTimeout(function(){ pack(20,5); }, 14000);
请帮助我不知道为什么会这样以及如何解决...
完全困惑:(
编辑:我在这里有一个简单的错误代码:
var slide_index = 3;
var arrays = [];
var slices = [0, 0, 0, 0, 0];
//
// our arrays
var a = [1, 1, 1, 1, 1];
//var b = [2, 2, 2, 2];
//var c = [3, 3, 3];
//var d = [4, 4];
//var e = [5];
// put all above arrays into one array
arrays = arrays.concat([a]);
//arrays = arrays.concat([b]);
//arrays = arrays.concat([c]);
//arrays = arrays.concat([d]);
//arrays = arrays.concat([e]);
console.log(arrays);
for (let i = 0; i < slide_index; i++) {
for (let j = 0; j <= i; j++) {
slices[i] += arrays[j][i - j];
}
}
console.log(slices);
我不知道如何解决...当我将所有数组放入arrays
时,它可以工作!?
答案 0 :(得分:1)
错误是因为数组只是一个空数组。
发生这种情况是由于以下原因:
if(j > 0) {
min = diff[j] - diff[j - 1];
console.log("min: " + min)
arrays = arrays.concat([arra(min, total_slides - j)]);
}
如果将其删除,则将得到输出..但也包含一堆NaN。
function arra(sum, length){
var array = [];
var float = 0;
if(length == 1){array.push(sum)} else{
for (let i = 0; i < length; i++) {
let a = 0; // the very first element of array
// Math equations
let last = (sum - (length / 2 * a)) * 2 / length
let dd = (last - a) / (length - 1)
sume = (dd * (i));
if(float_part(sume) > 0.5){ sume = Math.ceil(sume) }
float += float_part(sume)
array.push(int_part(sume));
}
}
// check to see if array elemements sum is equal to "var sum = 10"
let reducer = (accumulator, currentValue) => accumulator + currentValue;
let reducerlast = (accumulator, currentValue) => accumulator + currentValue;
let sumlast = sum - (array.reduce(reducerlast));
array[length - 1] += sumlast
let sumArray = array.reduce(reducer);
//console.log("sumArray: " + sumArray)
return array // results
function int_part(y){
return Math.trunc(y);
}
function float_part(d){
let int_part = Math.trunc(d);
return Number((d-int_part).toFixed(2));
}
}
var arrays = [];
var slices = [0, 0, 0, 0, 0];
var total_slides = 5; // total number of slides we have in the course
var addMe = [];
var total_standard_reviews = 60;
var a1 = 0;
var outputArray = [];
var min = 0;
var diff = [];
function pack(added_plans, slide_index){
let reducer = (accumulator, currentValue) => accumulator + currentValue;
let reducere = (accumulator, currentValue) => accumulator + currentValue;
let reducerx = (accumulator, currentValue) => accumulator + currentValue;
var j = slide_index - 1;
//console.log("j: " + slide_index)
diff.push(added_plans);
console.log(diff)
//if(j > 0) {
min = diff[j] - diff[j - 1];
console.log("min: " + min)
arrays = arrays.concat([arra(min, total_slides - j)]);
//}
console.log(arrays)
for (let i = 0; i < slide_index; i++) {
for (let j = 0; j <= i; j++) {
slices[i] += arrays[j][i - j];
}
}
console.log(slices[slide_index - 1]);
console.log(slices);
var total_reviews = (total_standard_reviews + added_plans);
var next = [];
var sum = 0;
//
var last = (total_reviews - (total_slides/2 * a1)) * 2/total_slides
var d = (last - a1) / (total_slides - 1)
for (let i = 1; i < total_slides; i++){
sum += a1 + (d * ( i - 1));
var pusher = int_part(sum);
next.push(pusher);
sum -= pusher
}
let sumer = total_reviews - (next.reduce(reducer));
next.push(sumer)
//
let sumerr = next.reduce(reducere);
//console.log(sumerr)
//console.log(next)
if(slide_index < total_slides){
var output = next[slide_index - 1]
console.log("output: " + output)
outputArray.push(output)
} else {
var outputArraysum = outputArray.reduce(reducerx);
output = total_reviews - outputArraysum ;
console.log("output2: " + output)
}
function int_part(y){
return Math.trunc(y);
}
function float_part(d){
let int_part = Math.trunc(d);
return Number((d-int_part).toFixed(2));
}
}
pack(0, 1);
setTimeout(function(){ pack(5,2); }, 6000);
setTimeout(function(){ pack(10,3); }, 9000);
setTimeout(function(){ pack(15,4); }, 12000);
setTimeout(function(){ pack(20,5); }, 14000);
编辑
我在那里做了两个更改...
1)我更改了内部循环,使其脱离了array.length
2)我将其更改为:j var slide_index = 3;
var arrays = [];
var slices = [0, 0, 0, 0, 0];
//
// our arrays
var a = [1, 1, 1, 1, 1];
//var b = [2, 2, 2, 2];
//var c = [3, 3, 3];
//var d = [4, 4];
//var e = [5];
// put all above arrays into one array
arrays = arrays.concat([a]);
//arrays = arrays.concat([b]);
//arrays = arrays.concat([c]);
//arrays = arrays.concat([d]);
//arrays = arrays.concat([e]);
console.log(arrays);
for (let i = 0; i < slide_index; i++) {
for (let j = 0; j < arrays.length; j++) {
console.log(j);
console.log(i);
console.log(i - j);
slices[i] += arrays[j][i - j];
}
}
console.log(slices);