Шестая программа: «Рекорды по прыжкам в длину»

Есть ли другие варианты, как решить данную задачу ?

var qualificationDistance = 200;
var attempts = [120, 150, 160, 201, 203, 180, 202];
var qualified = false;
var averageBest = 0;
var bestAttempts = [0, 0, 0];

// Поиск 3-х лучших значений
bestAttempts[0] = attempts[0];
for (var i = 1; i < attempts.length; i++)
{
 if (attempts[i] >= bestAttempts[0])
 {
  bestAttempts[2] = bestAttempts[1];
  bestAttempts[1] = bestAttempts[0];
  bestAttempts[0] = attempts[i];
 }else if (attempts[i] >= bestAttempts[1])
 {
  bestAttempts[2] = bestAttempts[1];
  bestAttempts[1] = attempts[i];
 }else if (attempts[i] >= bestAttempts[2])
    bestAttempts[2] = attempts[i];
}

// Вычисление средненого значения 3-х лучших
var averageBest = (bestAttempts[0] + bestAttempts[1] + bestAttempts[2]) / 3;

if (averageBest > qualificationDistance)
  qualified = true;
else
  qualified = false;

у вас есть целая тема для этого:

1 Симпатия

let qualificationDistance = 200;
let attempts = [120, 150, 160, 201, 203, 180, 202];
let qualified = false;
let averageBest = 0;

for (let i = 0; i <= attempts.length - 2; i++) {
let maxValue = attempts[i];
for (let j = i + 1; j <= attempts.length - 1; j++) {
if (attempts[j] < maxValue) {
maxValue = attempts[j];
let swap = attempts[i];
attempts[i] = maxValue;
attempts[j] = swap;
}
}
}

console.log(attempts);

averageBest = (attempts[attempts.length - 1] + attempts[attempts.length - 2] + attempts[attempts.length - 3]) / 3;

if (qualificationDistance <= averageBest) {
qualified = true;
} else {
qualified = false;
}
console.log(averageBest);
Я решил вот так задачу

let qualificationDistance = 200;

let attempts = [120, 150, 160, 201, 203, 180, 202];

let qualified = false;
let averageBest = 0;

for (let i = 0; i <= attempts.length - 2; i++) {
let maxValue = attempts[i];
for (j = i + 1; j <= attempts.length - 1; j++) {
if (attempts[j] > maxValue) {
maxValue = attempts[j]
let swap = attempts[i];
attempts[i] = maxValue;
attempts[j] = swap;
}
}
}
let sumTopJump = 0
for (i=0; i <=2; i++) {
sumTopJump += attempts[i];
averageBest = sumTopJump / 3;
}

if (averageBest > qualificationDistance) {
qualified = true;
}

let qualificationDistance = 200;
let attempts = [120, 150, 160, 201, 203, 180, 202];
let qualified = false;
let averageBest = 0;

for (i = 0; i <= attempts.length - 2; i++) {
  let min = attempts[i];

  for (j = i + 1; j <= attempts.length - 1; j++) {
    if (attempts[j] > min) {
      min = attempts[j];
      let swap = attempts[i];
      attempts[i] = min;
      attempts[j] = swap;
    }
  }
}
averageBest = (attempts[0] + attempts[1] + attempts[2]) / 3;
if (averageBest > qualificationDistance) {
  qualified = true;
  } else {
qualified = false;
}

Криво и косо, но получилось

let qualificationDistance = 200;
let attempts = [201, 199, 191, 150];
let attempts1 = []
let qualified = false;
let averageBest = 0;
let average = 0;

attempts.sort()
attempts.reverse()
for (let i = 0; i < 3; i++) {
attempts1.push(attempts[i])
}

for (let i = 0; i < attempts1.length; i++) {
average += parseFloat(attempts1[i])
}

averageBest = average / attempts1.length

if (averageBest > qualificationDistance) {
qualified = true;
} else {
qualified = false;
}

averageBest = attempts.sort((a, b) => b-a).slice(0,3).reduce((acc,elem)=>acc+elem,0)/3
qualified = averageBest > qualificationDistance? true: false

Обмен значений удобно делать с помощью деструктуризации массива:

for (let i = 0; i < attempts.length; i++) {
  let maxValue = attempts[i];
  let maxIndex = i;
  
  for (let j = i + 1; j < attempts.length; j++) {
    if (attempts[j] > maxValue) {
      maxValue = attempts[j];
      maxIndex = j;
    }
  }
  
  [attempts[i], attempts[maxIndex]] = [attempts[maxIndex], attempts[i]]; 
}

averageBest = (attempts[0] + attempts[1] + attempts[2]) / 3;

qualified = averageBest > qualificationDistance;