Вычисление средней длины трех слов в JavaScript

Чтобы рассчитать среднюю длину трех слов в JavaScript, вы можете использовать следующие методы:

Метод 1: использование функции

function calculateAverageLength(word1, word2, word3) {
  var totalLength = word1.length + word2.length + word3.length;
  return totalLength / 3;
}
var word1 = "example";
var word2 = "words";
var word3 = "length";
var averageLength = calculateAverageLength(word1, word2, word3);
console.log("Average length of the three words: " + averageLength);

Метод 2. Использование массива

var words = ["example", "words", "length"];
var totalLength = words.reduce(function (acc, word) {
  return acc + word.length;
}, 0);
var averageLength = totalLength / words.length;
console.log("Average length of the three words: " + averageLength);

Метод 3: использование стрелочной функции ES6 и оператора расширения

const calculateAverageLength = (...words) => {
  const totalLength = words.reduce((acc, word) => acc + word.length, 0);
  return totalLength / words.length;
};
const word1 = "example";
const word2 = "words";
const word3 = "length";
const averageLength = calculateAverageLength(word1, word2, word3);
console.log("Average length of the three words: " + averageLength);