TestCones没有通过,而console.log正确

时间:2019-07-05 07:59:38

标签: 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 -
     *
    * *
   * * *
  * * * *
 * * * * *
* * * * * *

*/

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;

这是提供的测试用例

const chai = require('chai');
const expect = chai.expect;
const pyramid = require('../solutions/q1_pyramid_of_stars.js');

describe('Testing - pyramid_of_stars', () => {
    it('module return type test case', (done) => {
        expect(typeof pyramid).to.deep.equal('function');
        done();
    });

    it('positive test case for odd count of height', (done) => {
        expect(pyramid(5)).equal(
            '     *  \n    * *  \n   * * *  \n  * * * *  \n * * * * *  \n');
        done();
    });

    it('positive test case for even count of height', (done) => {
        expect(pyramid(6)).equal(
            '      *  \n     * *  \n    * * *  \n   * * * *  \n  * * * * *  \n * * * * * *  \n');
        done();
    });

    it('negative test case', (done) => {
        expect(pyramid('invalid value')).to.deep.equal('');
        done();
    });
});

但是我遗漏了一些东西,因此所有的测试用例都失败了,正如我认为的那样,我没有传递结果输出​​,您能帮我遗漏什么吗?

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上的值设置为局部变量的代码时,它将获得所需的输出。

1 个答案:

答案 0 :(得分:0)

您是从 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;         
}

您的for循环不是函数,因此,当您返回时,它将从buildPyramid返回。

您的测试需要模拟console.log或构建字符串。这是建立字符串的两个选项:

(旧方法)

function buildPyramid(n) {
  // create a variable outside of the for loop
  let pyramid = '';
  for (var i = 1; i <= n; i++) {
    var output = ' '.repeat(n - i) + '* '.repeat(i) + ' '.repeat(n - i);
    console.log(output);
    //update outer variable
    pyramid += `\n ${output}`;
  }
  // return it after the for loop is finished
  return pyramid;
}

console.log(buildPyramid(6))

(现代方式)

const pyramidLevel = (level, totalLevels) => ' '.repeat(totalLevels - level) + '* '.repeat(level) + ' '.repeat(totalLevels - level)

// reduce an array of size n
const pyramid = n => Array.from(Array(n))
  .reduce((pyramid, value, level) => {
    // for each element in the initial array
    // append the new pyramid level to your previous state
    pyramid += `\n ${pyramidLevel(level, n+1)}`;
    return pyramid;
    // the last argument of reduce is your 'initial' state
  }, '')

console.log(pyramid(6));

这两种方法都可行,但是出于各种原因,我更喜欢现代方法,主要是因为您不需要变量在您关心的主要范围之外。