简体   繁体   English

我需要我的数组返回并数组返回另一个变量,同时还包括它以前的数组成员,不知道如何去做

[英]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.

相关问题 我将如何通过与另一个MySQL数组进行比较来更改它? - How would i go about changing a MySQL array by comparing it to another? 我需要访问一个数组里面的一个数组里面的一个项目如何 go 关于它(反应) - I need to access an item inside an array inside an array how to go about it (react) 我需要访问数组内的一个项目,如何了解它的 go (react &amp; javascript) - I need to access an item inside an array inside an array how to go about it (react & javascript) 如何将一组对象组合到另一个对象数组中,同时还组合了公共键 - How can i combine an array of objects into another array of objects while also combining the common keys 在映射数组时将异步函数的结果包含在我的“返回”中 - Including the results of an async function in my 'return' while mapping an array 在 JavaScript 中,我将如何通过数组为 go 创建一个变量,不仅要记住最大值,还要记住该值的 position - In JavaScript how would I create a variable to go through an array and remember not only the highest value but also the position of that value 将圆形结构转换为JSON-将元素数组复制到另一个数组并返回到原始数组时出错 - Converting circular structure to JSON - error while copying array of elements to another array and return back to original array 我应该如何 go 关于使用 for 循环将 arrays 连接到另一个数组并生成结果? - How should I go about using a for loop to randomize arrays concat into another array and generate the outcome? 我对对象数组使用了 map 方法,但是这个数组中的一个项目也是对象数组所以我需要获取它的属性 - I used map method for array of objects items but one item inside this array is also array of object So I need to take its properties 当我的配置文件中有一个普通数组时,我应该如何 go 关于使用 jQuery 扩展? - How should I go about using jQuery extend when my config file has a normal array in it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM