如何用break控制循环让console.log(sum)的值是不一样的呢

var a = [1, 2, 3, 4, 5, 6, 7, 8];

sum = [];

array = parseInt(10 * Math.random());


for (var j = 0; j < 3; j++) {

    for (var i = 0; i < a.length; i++) {


       

        sum[i] = a[i];



        if (i == array) {

            break

        };


    }


    console.log(sum);

}


 

https://img.mukewang.com/5c14a2a40001874103600147.jpghttps://img4.mukewang.com/5c14a2ad0001f7cf02300305.jpg

一只甜甜圈
浏览 486回答 1
1回答

POPMUISE

break 是可以去掉的…let arr = [1, 2, 3, 4, 5, 6, 7, 8]let result = []for (let i = 0; i < arr.length - 1; i++) {&nbsp; // 每次循环重置 result&nbsp; result = []&nbsp; for (let j = 0; j < arr.length; j++) {&nbsp; &nbsp; result.push(arr[j])&nbsp; &nbsp; if (j >= i) break&nbsp; }&nbsp; console.log(result)}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript