Javascript TestCases未通过,而console.log';答案是正确的

Javascript TestCases未通过,而console.log';答案是正确的,javascript,Javascript,问题就在下面 /* Write a program to build a `Pyramid of stars` of given height */ const buildPyramid = () => { // Write your code here }; /* For example, INPUT - buildPyramid(6) OUTPUT - * * * * * * * * * * * * * * * * * * * * * */

问题就在下面

/* Write a program to build a `Pyramid of stars` of given height */

const buildPyramid = () => {
    // Write your code here
};

/* For example,
INPUT - buildPyramid(6)
OUTPUT -
     *
    * *
   * * *
  * * * *
 * * * * *
* * * * * *

*/

module.exports = buildPyramid;
我添加了代码以获得所需的模式,如下所示

const buildPyramid = (n) => {
        var output;
    if(Number.isInteger(n)){
        for(var i=1; i<= n; i++){      
            var output =  ' '.repeat(n-i)+'* '.repeat(i)+' '.repeat(n-i);  
            console.log(output);
            return output;         
       }

    }else{
        console.log('Not a number');
        return '';
    }   
};

module.exports = buildPyramid;
但是我遗漏了一些东西,因此,所有的测试用例都失败了,因为我认为,我没有通过结果输出,你能帮我遗漏了什么吗

Testing - pyramid_of_stars
    √ module return type test case
    *
    1) positive test case for odd count of height
     *
    2) positive test case for even count of height
Not a number
    √ negative test case


  2 passing (22ms)
  2 failing

  1) Testing - pyramid_of_stars
       positive test case for odd count of height:

      AssertionError: expected ....
      + expected - actual

      -    *     
      +     *  
      +    * *  
      +   * * *  

      at Context.it (test\q1_pyramid_of_stars.spec.js:12:22)

  2) Testing - pyramid_of_stars
       positive test case for even count of height:

      AssertionError: expected ' ...
      + expected - actual

      -     *      
      +      *  
      +     * * 

      at Context.it (test\q1_pyramid_of_stars.spec.js:18:22)

当我运行将n上的值设置为局部变量的代码时,它得到了所需的输出

您正在从for循环返回,这不是必需的

正如您可以从输出中看到的:

 + expected - actual

      -    *     
      +     *  
      +    * *  
      +   * * * 
只有第一行是您实际返回的内容

这是因为您的for循环:

for(var i=1; i<= n; i++){      
    var output =  ' '.repeat(n-i)+'* '.repeat(i)+' '.repeat(n-i);  
    console.log(output);
    return output;         
}

用于(var i=1;i你从来没有使用过
n
?我不相信这个函数能工作。你也不会从函数中返回任何东西。你忘记在函数中传递参数,你的n是本地声明的,所以它总是在elsejust更新代码,其中包含return,更新后的输出仍然不会使用输入。一旦你这样,您也不会生成换行的输出。
for(var i=1; i<= n; i++){      
    var output =  ' '.repeat(n-i)+'* '.repeat(i)+' '.repeat(n-i);  
    console.log(output);
    return output;         
}