Files
freeCodeCamp/curriculum/challenges/korean/02-javascript-algorithms-and-data-structures/basic-javascript/nesting-for-loops.md
2024-05-29 10:06:17 -07:00

1.8 KiB

id, title, challengeType, videoUrl, forumTopicId, dashedName
id title challengeType videoUrl forumTopicId dashedName
56533eb9ac21ba0edf2244e1 For 루프 중첩하기 1 https://scrimba.com/c/cRn6GHM 18248 nesting-for-loops

--description--

다차원 배열이 있을 때, 이전에 언급된 것과 같은 논리를 사용하여 배열과 모든 하위 배열을 순환할 수 있습니다. 여기 예시가 있습니다.

const arr = [
  [1, 2], [3, 4], [5, 6]
];

for (let i = 0; i < arr.length; i++) {
  for (let j = 0; j < arr[i].length; j++) {
    console.log(arr[i][j]);
  }
}

이는 arr의 각 하위 요소를 한 번에 하나씩 출력합니다. 내부 루프에서는 arr[i].length를 확인합니다. arr[i] 자체가 배열이기 때문입니다.

--instructions--

multiplyAll 함수를 수정해 arr의 하위 배열에 있는 모든 숫자의 곱을 반환하도록 하세요.

--hints--

multiplyAll([[1], [2], [3]])6을 반환해야 합니다.

assert(multiplyAll([[1], [2], [3]]) === 6);

multiplyAll([[1, 2], [3, 4], [5, 6, 7]])5040을 반환해야 합니다.

assert(
  multiplyAll([
    [1, 2],
    [3, 4],
    [5, 6, 7]
  ]) === 5040
);

multiplyAll([[5, 1], [0.2, 4, 0.5], [3, 9]])54를 반환해야 합니다.

assert(
  multiplyAll([
    [5, 1],
    [0.2, 4, 0.5],
    [3, 9]
  ]) === 54
);

--seed--

--seed-contents--

function multiplyAll(arr) {
  let product = 1;
  // Only change code below this line

  // Only change code above this line
  return product;
}

multiplyAll([[1, 2], [3, 4], [5, 6, 7]]);

--solutions--

function multiplyAll(arr) {
  let product = 1;
  for (let i = 0; i < arr.length; i++) {
    for (let j = 0; j < arr[i].length; j++) {
      product *= arr[i][j];
    }
  }
  return product;
}