[英]I need my array to return and array back to another variable while also including its previous array members, no idea how to go about it
This is the test code that it's supposed to pass这是它应该通过的测试代码
function makeArray() {
const array = [];
const t = 10;
for (let i = 0; i < t; i++) {
array.push("I am a strange loop.");
}
return [array, t];
}
describe('loops', () => {
jsdom({
src: fs.readFileSync(path.resolve(__dirname, '..', 'loops.js'), 'utf-8'),
});
describe('forLoop(array)', () => {
it('adds `"I am ${i} strange loop${i === 0 ? \'\' : \'s\'}."` to an array 25 times', () => {
const [array, t] = makeArray();
const strangeArray = forLoop(array);
const testArray = strangeArray.slice(array.length);
const first = "I am 1 strange loop.";
const rest = "I am 24 strange loops.";
expect(strangeArray[11]).to.equal(first);
expect(strangeArray[34]).to.equal(rest);
expect(strangeArray.length).to.equal(t + 25);
});
});
});
this is my code to return the function to strangeArray
what I am thinking is that 35 is the total number of members in the array and as the test pass requires me to have 'expect(strangeArray[11]).to.equal(first)' 11th value to be equal to my function return as "I am 1 strange loop."这是我将函数返回到
strangeArray
代码,我在想的是 35 是数组中成员的总数,因为测试通过要求我有 'expect(strangeArray[11]).to.equal(first) ' 等于我的函数的第 11 个值返回为“我是 1 个奇怪的循环”。
function forLoop(array) {
for (let i = 0; i < 35; i++) {
if (array[i] === "I am a strange loop.") {
return;
}
else {
array.push("I am ${i} strange loops.");
}
}
return [array,i];
}
Not sure what you mean exactly but I guess you just want the test to pass?不确定你的意思,但我猜你只是想让测试通过? The problem is that the first loop has 'loop' as singular and your indexes don't work either since they would start at 11. That's why your code doesn't work.
问题是第一个循环将 'loop' 设为单数,并且您的索引也不起作用,因为它们从 11 开始。这就是您的代码不起作用的原因。 You can just push to the original array.
您可以推送到原始数组。
function forLoop(array){
for(let i = 0; i < 25; i++){
array.push(`I am ${i} strange loop${i > 1 ? '' : 's'}.`)
}
return array
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.