gpt4 book ai didi

javascript - 测试用例没有通过,而 console.logs 是正确的

转载 作者:行者123 更新时间:2023-11-30 06:14:38 25 4
gpt4 key购买 nike

问题如下

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

最佳答案

您正从 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));

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

关于javascript - 测试用例没有通过,而 console.logs 是正确的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56898932/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com