Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

272
Visualizações
try to create if to stop get numbers higher then 20 javascript

i tray to build function to show the high and lowest number but between 0 and 20

"use strict";
let scors = [50, 70, 60, 20, 15, 14, 12, 10, 9, 8, -5, 4, 16, 2, 17.5, 12.75];
function minMax(score) {
  let max = score[0];
  let min = score[0];

  for (let i = 0; i < score.length; i++) {
    if (score[i] < min && score[i] >= 0) {
      min = score[i];
    }
    //this part dose not work
    if (score[i] > max && score[i] <= 20) {
      max = score[i];
    }
  }
  console.log(max);
  console.log(min);
}

minMax(scors);
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

These are the conditions you applied, the current score must be greater than max and less than 20.

if (score[i] > max && score[i] <= 20) {
      max = score[i];
}

This is an example of 4th first iteration.

  • Iteration 1: input: 50 > max: 50
  • Iteration 2: input: 70 > max: 50
  • Iteration 3: input: 60 > max = 50
  • Iteration 4: input: 20 > max = 50

In the 4th iteration, the max is not assigned as 20 because it doesn't greater than the current max which is 50.

Here is the simple solution:

  1. Add score.sort(); for sorting the array before start looping.
  2. Define the min with the last index of the array.

'use strict';

let scors = [50, 70, 60, 20, 15, 14, 12, 10, 9, 8, -5, 4, 16, 2, 17.5, 12.75];

function minMax(score) {
  score.sort();
  let max = score[0];
  let min = score[score.length - 1];

  for (let i = 0; i < score.length; i++) {
    if (score[i] < min && score[i] >= 0) {
      min = score[i];
    }
    // This part now works!
    if (score[i] > max && score[i] <= 20) {
      max = score[i];
    }
  }
  console.log(max);
  console.log(min);
}

minMax(scors);
about 4 years ago · Juan Pablo Isaza Relatório

0

For the range of 0 - 20 you can bound the min and the max which would eliminate the need to sort the array.

Time complexity O(N).

'use strict';
    
    let scors = [50, 70, 60, 20, 15, 14, 12, 10, 9, 8, -5, 4, 16, 2, 17.5, 12.75];
    
    function minMax(score) {
      let max = -1;
      let min = 21;
    
      for (let i = 0; i < score.length; i++) {
        if (score[i] < min && score[i] >= 0) {
          min = score[i];
        }
        // This part now works!
        if (score[i] > max && score[i] <= 20) {
          max = score[i];
        }
      }
      console.log(max);
      console.log(min);
    }
    
    minMax(scors);
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda