编写函数locateLargest(arr),返回一个Location的对象
(对象中包含了二维数组的最大值、最大值的行下标和列下标):
class Location {
constructor() {
this.row = 0;
this.column = 0 ;
this.maxValue = arr[0][0];
};
show(){
console.log('行下标row:',this.row);
console.log('列下标row:',this.column);
console.log('最大值:',this.maxValue);
}
};
function locateLargest(arr){
//创建Location类的对象,并假设第一项为最大值
let max_locate = new Location(0,0,arr[0][0]);
for (let i = 0; i < arr.length; i++) {//遍历二位数组
for (let j = 0; j < arr[i].length; j++) {
if (max_locate.maxValue < arr[i][j]) {
max_locate.maxValue = arr[i][j];
max_locate.row = i;
max_locate.column = j;
};
};
};
return max_locate;//将对象返回给该函数
}
let arr = [[23, 34, 45, 56], [12, 15, 67, 8], [11, 22, 33, 66]];
let test = locateLargest(arr);//调用函数,返回一个Location对象
//使用对象的方法
test.show();