数字矩阵JavaScript [英] Matrix of numbers javascript

查看:214
本文介绍了数字矩阵JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要JS中的函数帮助,该函数以给定的整数N打印矩阵,如下所示:

I need help with a function in JS that prints a matrix by a given integer N like this:

N = 2;
Matrix: 1 2
        2 3
N = 3;
Matrix: 1 2 3
        2 3 4
        3 4 5

我需要进行2次循环,但是我不知道怎么做

I need to make it with 2 loops but I can't figure out how

function solve(args) {
  var n = args[0];  
}

PS:很抱歉将矩阵插入JS代码,但是那样我可以看到结果.

PS: Sorry for inserting the matrixes into JS code but that way I could visualise the result.

推荐答案

这是逻辑

function paintMatrix(n) {

    for (var i = 1; i <= n; i++) {
      var result = "";
      for (var j = 1; j <= n; j++) {
        result += (i + j - 1);
      }
      console.log(result);
    }
}

paintMatrix(3);

这篇关于数字矩阵JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆