You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JavaScript:满足条件时跳过3D数组中的子数组,实现累加至指定值

Solution for Conditional 3D Array Accumulation

Let's fix your loop logic to meet the exact requirements you outlined. The core goals are to:

  • Skip remaining elements in a subarray if adding them would push the sum over the target
  • Terminate all loops immediately once the sum hits the target exactly

Approach

Since your parent array is sorted from largest to smallest and each subarray contains identical values, we can optimize our loop to check before adding each element whether it would exceed the target. This lets us skip unnecessary additions and subarrays early. We have two clean ways to implement this: using a flag variable or a loop label to control loop termination.

Solution Code

Option 1: Using a Flag Variable (Clean & Readable)

const array = [ [5, 5, 5, 5, 5, 5], [4, 4, 4], [3, 3, 3, 3], [1, 1, 1, 1, 1, 1, 1] ];
const target = 27;
let currentSum = 0;
let reachedTarget = false;

// Outer loop runs only if we haven't hit the target yet
for (let i = 0; i < array.length && !reachedTarget; i++) {
  const subArray = array[i];
  // Inner loop runs only if we haven't hit the target yet
  for (let j = 0; j < subArray.length && !reachedTarget; j++) {
    const num = subArray[j];
    
    // Skip this element and the rest of the subarray if adding it would exceed the target
    if (currentSum + num > target) {
      break;
    }
    
    // Add the element since it won't exceed the target
    currentSum += num;
    
    // Check if we've hit the target exactly
    if (currentSum === target) {
      reachedTarget = true;
    }
  }
}

console.log(currentSum); // Output: 27

Option 2: Using a Loop Label (Direct Exit from All Loops)

const array = [ [5, 5, 5, 5, 5, 5], [4, 4, 4], [3, 3, 3, 3], [1, 1, 1, 1, 1, 1, 1] ];
const target = 27;
let currentSum = 0;

// Label the outer loop to allow direct exit from inner loops
outerLoop: for (let i = 0; i < array.length; i++) {
  const subArray = array[i];
  for (let j = 0; j < subArray.length; j++) {
    const num = subArray[j];
    
    if (currentSum + num > target) {
      break; // Skip remaining elements in this subarray
    }
    
    currentSum += num;
    
    if (currentSum === target) {
      break outerLoop; // Terminate ALL loops immediately
    }
  }
}

console.log(currentSum); // Output: 27

How It Works (Matching Your Example)

Let's walk through the execution with your sample array and target 27:

  1. First subarray (all 5s):
    • We add 5 elements to reach 25. When we check the 6th element, 25 + 5 = 30 > 27, so we break the inner loop and skip the rest of the subarray.
  2. Second subarray (all 4s):
    • Checking the first element: 25 + 4 = 29 > 27, so we break the inner loop immediately and skip the entire subarray.
  3. Third subarray (all 3s):
    • Checking the first element: 25 + 3 = 28 > 27, so we break the inner loop and skip the entire subarray.
  4. Fourth subarray (all 1s):
    • Add the first 1 to reach 26. Then add the second 1 to hit exactly 27. We immediately terminate all loops.

This gives us the desired final sum of 27, following all your rules perfectly.

内容的提问来源于stack exchange,提问作者Rasul Ali

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 13:08:11