1
let x = [3, 6, -2, -5, 7, 3]

function solution(x) {
  for (let i = 0; i < x.length; i++) {}
}

Нужно умножить первый элемент со вторым, третий с четвёртым а пятый с шестым.

4 ответа 4

3

var x = [-1, 5, -2, 5, -7, 4, 11];

for (let i = 0; i < x.length; ++i) {
    if (i < x.length-1) {
        console.log(x[i] * x[++i]);
    }
}

var x = [-1, 5, -2, 5, -7, 4, 11], num = 0, max =-Infinity;

for (let i = 0; i < x.length; ++i) {
    if (i < x.length - 1) {
        num = x[i] * x[++i];
        if(num > max) {
            max = num;
        }
    }
}
console.log(max);

0
2

При условии, что кол-во элементов массива - четное.

x = [-1, 5, -2, 5, -7, 4]

function solution(x) {
  // Нам же нужно где-то хранить предыдущий максимум
  var max = -Infinity

  // Пишем += 2, чтобы перескакивать сразу через одно число
  for (let i = 0; i < x.length; i += 2)
    if (max < x[i] * x[i + 1])
      max = x[i] * x[i + 1];

  console.log(max);
}

solution(x)

0
1

console.log(Math.max(...[3, 6, -2, -5, 7, 3].reduce((acc, curr, idx, arr) => {
  return idx % 2 != 0 ? acc.concat(arr[idx] * arr[idx - 1]) : acc;
}, [])))

-1

function solution(x){
    let max = x[0]*x[1]
    for(let i=1; i<x.length-1;i++){
        if(x[i]*x[i+1]>max){
            max=x[i]*x[i+1]
        }
    }
  return max
}

0

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.