ЗАДАЧА. ОБЩИЕ ЭЛЕМЕНТЫ forEach

Рейтинг: -3Ответов: 1Опубликовано: 25.01.2023

function getCommonElements(firstArray, secondArray) {
   const commonElements = [];

   firstArray.forEach(element => {
     if (secondArray.includes(element)) {
        commonElements.push(element);
     }
   });
   return commonElements;
}

console.log(getCommonElements([1, 2, 3], [2, 4]));
console.log(getCommonElements([1, 2, 3], [2, 1, 17, 19]));
console.log(getCommonElements([24, 12, 27, 3], [12, 8, 3, 36, 27]));
console.log(getCommonElements([10, 20, 30, 40], [4, 30, 17, 10, 40]) );
console.log(getCommonElements([1, 2, 3], [10, 20, 30]));

Функция getCommonElements(firstArray, secondArray) принимает два массива произвольной длины в параметры firstArray и secondArray, и возвращает новый массив их общих элементов, то есть тех которые есть в обоих массивах.

Выполни рефакторинг функции так, чтобы вместо цикла for она использовала метод forEach.

ТЕСТЫ:

Объявлена функция getCommonElements(firstArray, secondArray)

Для перебора параметра (массива) использован метод forEach

Вызов getCommonElements([1, 2, 3], [2, 4]) возвращает [2]

Вызов getCommonElements([1, 2, 3], [2, 1, 17, 19]) возвращает [1, 2]

Вызов getCommonElements([24, 12, 27, 3], [12, 8, 3, 36, 27]) возвращает [12, 27, 3]

Вызов getCommonElements([10, 20, 30, 40], [4, 30, 17, 10, 40]) возвращает [10, 30, 40]

Вызов getCommonElements([1, 2, 3], [10, 20, 30]) возвращает []

Вызов функции со случайными, но валидными аргументами, возвращает правильное значение

function getCommonElements(firstArray, secondArray) {
  const commonElements = [];
  // Change code below this line

  for (let i = 0; i < firstArray.length; i += 1) {
    if (secondArray.includes(firstArray[i])) {
      commonElements.push(firstArray[i]);
    }
  }

  return commonElements;
  // Change code above this line
}

Ответы

▲ 1Принят
function getCommonElements(firstArray, secondArray) {
    const commonElements = [];

    firstArray.forEach(element => {
        if (secondArray.includes(element)) {
            commonElements.push(element);
        }
    });

    return commonElements;
}