【问题标题】:Why do i get extra undefined in last row of my output?为什么我在输出的最后一行得到额外的未定义?
【发布时间】:2021-03-27 06:08:24
【问题描述】:

这是我的代码:

function triangle1(n , shape){
    let ajib =[];
    for (let row = 1; row <= n; row++) {
        let b ="";
        let a="";
        
        for (let space = 1; space <= n - row; space++) {
            b= b + " ";
        }
    
    
        for (let star = 1; star <= row; star++) {
            a = a + shape + " "; ///" " will make an Equilateral triangle 
        }
       
        let c = b + a;
        
        console.log(c);

        ajib.push(c);
        //return c ;

    
    }
    
    console.log(ajib)
   

}
console.log(triangle1(5,"a"))


这是我的输出:

    a 
   a a 
  a a a 
 a a a a 
a a a a a 
[ '    a ', '   a a ', '  a a a ', ' a a a a ', 'a a a a a ' ]
undefined

首先:我不知道这个undefined是从哪里来的!

我怎样才能摆脱它?

第二:当我使用 return c; 而不是 console.log(c) 输出每一行时,我得到一个错误的结果,例如:

    a



为什么会发生这种情况,我该如何解决?

【问题讨论】:

  • 索引通常从零开始。您是否在 for 语句中从 1 开始超出了数组长度?
  • undefined 是来自triangle1() 的返回,这确实没有定义。您可以通过调用triangle1(5,"a") 而不是console.log(triangle1(5,"a"))摆脱它。
  • 我认为未定义是预期的。你是在像 repl 这样的网站上开发吗?奇怪的结果似乎是因为你之前记录了这封信,然后你提前返回,导致 console.log(ajib) 永远不会被调用。
  • 我建议使用debugger 语句并调试代码 - 记录undefined 的位置应该很明显
  • @Lain 你提出了一个好方法,但现在当我在函数中使用return c 而不是console.log(c) 时,三角形不会按原样显示!

标签: javascript


【解决方案1】:

参见下面的sn-p中的cmets和解释。

function triangle1(n, shape){
    //REM: You might wanna add an empty entry, so the console.log always creates a new line - to align the pyramid properly.
    let ajib = [''];

    //REM: If you enter more than one letter as shape, you need to multiply whitespaces by its length
    let whitespace = ' '.repeat(shape.length);

    for (let row = 1; row <= n; row++){
        let b = '';
        let a = '';
     
        for (let space = 1; space <= n - row; space++) {
            b= b + whitespace;
        };
    
        for (let star = 1; star <= row; star++) {
            a = a + shape + whitespace; ///" " will make an Equilateral triangle 
        };
       
    //REM: You do not really need c, since you can just push the result to ajib
        //let c = b + a;

        ajib.push(b + a)

        //REM: Here is the wrong point to return c, since it leaves the function and only returns c from this iteration - always the first in this case
        //return c ;
    };
    
    //REM: You should output the whole list. To reuse and log as see fit
    return ajib
};

//REM: Adding the event
document.querySelector('input[type=button]').addEventListener('click', function(){
    let tValue = triangle1(
      Number(document.querySelector('input[type=number]').value) || 1,
      document.querySelector('input[type=text]').value || 'a'
    );
    
    console.log(tValue.join('\n'))
})
<input type = 'number' value = '5'>
<input type = 'text' value = 'a'>
<input type = 'button' value = 'click me'>

【讨论】:

    【解决方案2】:

    它来自console.log(triangle1(5,"a")),因为三角形没有返回值。它在运行函数时正确打印字母,然后未定义,因为三角形的返回值未定义 第二:当您使用return c 时,您将立即结束该功能。将其添加到列表并返回列表

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-08
      相关资源
      最近更新 更多