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

266
Visualizações
Javascript function that returns an array that contains multiples of 7 that are less than max

https://i.stack.imgur.com/FU2o4.png

I am clearly new at programming. I need a support. I have tried to write a function that contains multiple of 7 that are less than max argument. But after I run my code I saw the result 0 as you can find in the picture. How can I fix it?

function returnSevens(max) {
    let sevenarr=[];
    for (let i = 0; i<max; i++) {
        if (i % 7 === 0) {
            sevenarr.push[i];
        }
    }
return sevenarr;
}
console.log(returnSevens(14))
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Your code is correct, you just have a minor typo with your brackets. [] instead of ().

A small hint to optimize the performance of your code:

You can actually skip the whole if condition when increasing your i by 7 instead of 1.

function returnSevens(max) {
    let sevenarr=[];
    for (let i = 0; i<max; i += 7) {
         sevenarr.push(i);
    }
return sevenarr;
}
about 4 years ago · Juan Pablo Isaza Relatório

0

Push is a method. so you need to use () instead of []

function returnSevens(max) {
    let sevenarr=[];
    for (let i = 0; i<max; i++) {
        if (i % 7 === 0) {
            sevenarr.push(i);
        }
    }
return sevenarr;
}
console.log(returnSevens(14))
about 4 years ago · Juan Pablo Isaza Relatório

0

You already have the answer to your question, but I'd suggest to do some adjustments.

  1. Declare the array you'd like to return as const, not let. So it wont be changed by accident.
  2. Instead of running the loop with step 1 and testing each value, you can loop it with step 7 and skip testing at all. This code is much faster. It is not very important for small values, but for huge quantities and complex test operations, it might be significant.

function returnSevens(max) {
  const sevenarr = [];
  for (let i = 0; i < max; i+=7) {
    sevenarr.push(i);
  }
  return sevenarr;
}
console.log(returnSevens(14));

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