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

109
Visualizações
Using continue Statement in JavaScript

I'm learning JavaScript and I'm experimenting with the continue statement, and from my understanding, it's suppose to "skip" an iteration and than continue. I wrote a piece of code from a Udemy course I'm doing. The for loop is suppose to fill the percentages2 array, and it does work.

However, I'm trying to have the array not be filled up with values less than 2. If it works I should be getting back 2 elements in my percentages2 array instead of 4.

Anyone know why the continue statement isn't working?

const populations2 = [332, 144, 1441, 67];
const percentages2 = [];

const percentageOfWorld4 = function (population) {
  return (population / 7900) * 100;
};

for (let p = 0; p < populations2.length; p++) {
  if (percentages2[p] < 2) continue;

  percentages2.push(percentageOfWorld4(populations2[p]));
}

console.log(percentages2);

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Hyy,

  • Your are looping an array populations2 right
  • first mistake inside the loop your are not checking the return result from function percentageOfWorld4 it should be (percentageOfWorld4(populations2[p]) < 2)
    • And you should pass the pth value of populations2 then use continue
  • later you can push the other element to a different array percentages2.push(populations2[p]);
const populations2 = [332, 144, 1441, 67];
const percentages2 = [];

const percentageOfWorld4 = function(population) {
    return (population / 7900 ) * 100;
};

for (let p = 0; p < populations2.length; p++) {

    if(percentageOfWorld4(populations2[p]) < 2) continue;

    percentages2.push(populations2[p]);

}; console.log(percentages2); // [4.2025316455696204, 18.240506329113924]

It is a good practice to store the return value in a variable rather then calling the function twice

const populations2 = [332, 144, 1441, 67];
const percentages2 = [];

const percentageOfWorld4 = function(population) {
    return (population / 7900 ) * 100;
};

for (let p = 0; p < populations2.length; p++) {
    let result = percentageOfWorld4(populations2[p]);

    if(result < 2) continue;

    percentages2.push(result);

}; console.log(percentages2); // [4.2025316455696204, 18.240506329113924]

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