猿问

8.14.4:求和网格代码HS

问题是这样的:(这是 JavaScript)


写一个函数叫


函数 sumGrid(grid) 以网格作为参数,并用该位置的行索引和列索引之和填充网格中的每个位置。


例如,这段代码:


var grid = new Grid(2, 3);

println(sumGrid(grid));

应该输出:


0   1   2

1   2   3

这是我到目前为止所得到的......


function start(){

   var grid = new Grid(2, 3);

   println(sumGrid(grid));


}

function sumGrid(grid){

   for(var row = 0; row < grid.numRows(); row++){

      for(var col = 0; col < grid.numCols(); col++){

        var elem = (row);

        row += 1;

        print(elem + " ");

    }

        println("");

    col = 0

    for(var row = 0; row < grid.numRows(); row++){

        col += 1;

        var elem2 = (col);

        print(elem2 + " ");

        

      }

   }

}

当我运行代码时,它显示:


 0   1   2

 1   2  undefined 

知道我应该做什么吗?


慕无忌1623718
浏览 114回答 4
4回答

一只甜甜圈

您看到的原因undefined是因为您正在print调用该sumGrid函数,该函数不会返回任何内容,因此undefined会打印出“为什么”。不知道为什么你有 3 个for循环,但你只需要 2 个。首先循环rows,然后循环cols,然后简单打印row + col应该可以工作。const Grid = function (x, y) {&nbsp; this.x = x&nbsp; this.y = y&nbsp;&nbsp;&nbsp; this.numRows = () => {&nbsp; &nbsp; return this.x&nbsp; }&nbsp;&nbsp;&nbsp; this.numCols = () => {&nbsp; &nbsp; return this.y&nbsp; }&nbsp; &nbsp;&nbsp;}const sumGrid = (grid) => {&nbsp; &nbsp;for(let row = 0; row < grid.numRows(); row++){&nbsp; &nbsp; &nbsp; document.write("<p> ")&nbsp; &nbsp; &nbsp; for(let col = 0; col < grid.numCols(); col++){&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; document.write(row + col)&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; document.write(" </p>")&nbsp; &nbsp; }}const grid = new Grid(4, 5)sumGrid(grid)

蝴蝶不菲

我相信在我查阅了Grid 文档并使用源代码进行练习后,这应该可以工作。我也在REPL上有它function start(){  var grid = new Grid(2, 3);  println(sumGrid(grid));}function sumGrid(grid){  grid.grid.forEach(a=>a.fill(1)) //if some strange error happens, change this line to: grid.forEach(a=>a.fill(1))  var ans=[]  var x=grid.toList()  x.forEach(a=>ans.push(a[0]+a[1]))  return ans}start()

繁华开满天机

这对我来说有点小问题,但我得到了这个更简单的答案。&nbsp; &nbsp; function start(){&nbsp; &nbsp; &nbsp; &nbsp; var grid = new Grid(2,3);&nbsp; &nbsp; &nbsp; &nbsp; println(sumGrid(grid));&nbsp; &nbsp; }&nbsp; &nbsp; function sumGrid(grid){&nbsp; &nbsp; &nbsp; &nbsp; for(var row = 0; row < grid.numRows(); row++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(var col = 0; col < grid.numCols(); col++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var elem = col + row;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; grid.set(row, col, elem);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return grid;&nbsp; &nbsp; }

MM们

我让它工作了!这就是我为获得正确输出所做的事情:function start(){&nbsp; &nbsp; var grid = new Grid(2, 3);&nbsp; &nbsp; println(sumGrid(grid));}function sumGrid(grid){&nbsp; &nbsp; for(var row = 0; row < grid.numRows(); row++){&nbsp; &nbsp; &nbsp; &nbsp; for(var col = 0; col < grid.numCols(); col++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var elem = (row);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; row += 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(elem + " ");&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; }&nbsp; &nbsp; println("");&nbsp; &nbsp; col = 0&nbsp; &nbsp; for(var row = 0; row < grid.numRows(); row++){&nbsp; &nbsp; &nbsp; &nbsp; col += 1;&nbsp; &nbsp; &nbsp; &nbsp; var elem2 = (col);&nbsp; &nbsp; &nbsp; &nbsp; print(elem2 + " ");&nbsp; &nbsp; &nbsp; &nbsp; elem2 += 1&nbsp; &nbsp; }&nbsp; &nbsp; return elem2&nbsp; &nbsp;}}这可能不是解决问题的最佳方法,但它可以完成工作。
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答