在博客中看到了一篇文章挺有意思的,文章地址是:http://www.cnblogs.com/flsummer/p/5025286.html
是关于计算连续三个数的值,以以下数组为例,1-2-3,1-2,6,1-1-4算连续的,1-1-2,1-2-2则不算,借鉴于他的写法,我作了一些小小的改变,我把数组的每一项设置成不同项数,
于是乎有了以下的数组;写这个纯属是为了练习,项目中估计也用不到这种乱七八槽的数组,
对于js,还有很多的路要走,希望看过的大神能给以指导
var oArray = [
[1, 2,3],
[1, 2, 3],
[4, 5, 6, 3],
[6, 8, 0],
[7, 8, 9],
[11, 12, 13, 16],
[11, 12, 13, 16],
[11, 12, 13, 16],
[11, 12]
];
var sum = 0;
var arr = [];
//横向
for (var i = 0; i < oArray.length; i++) {
for (var j = 0; j < oArray[i].length - 2; j++) {
if(oArray[i][j + 2]){
sum += oArray[i][j] + oArray[i][j + 1] + oArray[i][j + 2];
arr.push(sum);
sum = 0;
}
}
}
//纵向
for (var i = 0; i < oArray.length - 2; i++) {
for (var m = 0; m < oArray[i].length; m++) {
if (oArray[i + 2][m]) {
sum += oArray[i][m] + oArray[i + 1][m] + oArray[i + 2][m];
arr.push(sum);
sum = 0;
}
}
}
//纵横,想起了秦时明月
for (var i = 0; i < oArray.length - 2; i++) {
for (var n = 0; n < oArray[i].length - 2; n++) {
if (oArray[i + 2][n + 2]) {
sum += oArray[i][n] + oArray[i + 1][n + 1] + oArray[i + 2][n + 2];
arr.push(sum);
sum = 0;
}
}
}
console.log(arr);
console.log(Math.max.apply(this,arr));
console.log(Math.min.apply(this,arr));