设计一个名为Location的类,定位二维数组中的最大值及其位置。这个类包括属性row(行下标)、column(列下标)和maxValue(最大值)。编写下面的函数,返回一个Location的对象(对

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <!-- 2、设计一个名为Location的类,定位二维数组中的最大值及其位置。这个类包括属性row(行下标)、column(列下标)和maxValue(最大值)。编写下面的函数,返回一个Location的对象(对象中包含了二维数组的最大值、最大值的行下标和列下标): -->

    <script>
        class Location{
            constructor(row,column,maxValue){
                this._row=row
                this._column=column
                this._maxValue=maxValue
            }
            get row(){
                return this._row
            }
            set row(val){
                this._row=val
            }
            get column(){
                return this._column
            }
            set column(val){
                this._column=val
            }
            get maxValue(){
                return this._maxValue
            }
            set maxValue(val){
                this._maxValue=val
            }
            displiy(){
                console.log("数组最大值"+this._maxValue)
                console.log("最大值的行下标"+this._row)
                console.log("最大值的列下标"+this._column)
            }
        }


        //函数的功能:找出数组的最大值  用locate类的对象保存最大值和下标
        //函数的参数是数组 
        //函数的返回值:location的对象

       function LocateLargest(arr){
            //1.创建Location类的对象:假设二维数组的第一个元素为最大值
            let max_location =new Location(0,0,arr[0][0])
            //2.遍历二维数组元素找出最大值
            for(let i =0;i<arr.length;i++){ //外循环是数组的行下标
                for(let j=0; j<arr[i].length;j++){  //内循环是数组的列下标
                    if(max_location.maxValue<arr[i][j]){
                        max_location.maxValue=arr[i][j]
                        max_location.row=i
                        max_location.column=j
                    }
                }
            }
            //3.返回Location类的对象
            return max_location
       }
        let arr = [[12,35,45,66],[78,95,12,65],[55,44,102,89]]
        let locat = LocateLargest(arr)
        locat.displiy()


    </script>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值