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

99
Visualizações
Looping through an array and checking multiple conditions

I have an array of files, and want to display only one based on the detected screen width. I'm having trouble trying to loop through and check against both the current item in the array and the next one.

Here are some requirements:

  • If viewportWidth is greater than or equal to the largest video width, then choose the largest video.
  • If viewportWidth is less than or equal to the smallest video width, then choose the smallest video
  • Otherwise, loop through the array and if viewportWidth is smaller than the current item but larger than the next one, then choose that video.

Here's my current code but I can't seem to get the looping/conditions part right:

const viewportWidth = 1800;
const videos = [{
    url: 'video1.mp4',
    width: 1920
  },
  {
    url: 'video2.mp4',
    width: 1280
  },
  {
    url: 'video3.mp4',
    width: 720
  },
  {
    url: 'video4.mp4',
    width: 560
  },
  {
    url: 'video5.mp4',
    width: 420
  },
];

function getVideoUrl(viewportWidth) {
  if (viewportWidth > videos[0].width) {
    return videos[0].url;
  }

  if (viewportWidth < videos[videos.length - 1].width) {
    return videos[videos.length - 1].url;
  } else {
    let i = 0;
    while (viewportWidth < videos[i].width && viewportWidth > videos[i + 1].width) {
      i++
    }
    return videos[i].url;
  }
}

const videoUrl = getVideoUrl(viewportWidth);

console.log(videoUrl);

Based on the above:

viewWidth = 1200 // should output video2.mp4
viewWidth = 2000 // should output video1.mp4
vewWidth = 300 // should output video5.mp4
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Your approach is way too complicated, IMHO :-)

Just loop through the videos in reverse order, and as soon as you find one with a width greater(-equal) than your viewport width, return that one.

If you didn't find one, then simply return the first one, after the loop.

function getVideoUrl(viewportWidth) {
  for (let i = videos.length-1; i >= 0 ; --i) {
    if (videos[i].width >= viewportWidth) {
      return videos[i];
    }
  }
  return videos[0];
}
about 4 years ago · Juan Pablo Isaza Relatório

0

The while inside the else will always be executed and always return the second video in this example.

Try to keep it stupid simple instead

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