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

121
Visualizações
How do I Replace the elements in the Array and assign it zero in javascript?

Write a function squareWave(arr) that takes in the following array: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18], and starts replacing the numbers, one by one, with zeroes, until it reaches a multiple of 5. From that point onwards, start replacing the numbers with 1s, until you reach the next multiple of 5.

Then, from that point onwards, start replacing with 0s again, then 1s again,and so on until you reach the end of the array. My code is not working Anybody can help me?

let input = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18];

function squareWave(arr) {
   let zeros = true;
   let output = [];
   for (let i = 0; i < arr.length; i++) {
       if (arr[i] % 5) {
          arr[i] = 0;
       } else if (arr[i] !== 5) {
           arr[i] = 1;
       }
   }
   console.log(arr)
}

Output should be=[0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1]

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

0

You are not keeping track if the current entry should be a 0 or 1. Also, you are not using variables zeros and output

Instead of a boolean, you can keep a 0 or 1 in the variable zeros and flip the value when the mod of 5 equals zero.

if (arr[i] % 5 === 0) {

Then for every iteration write the value of zeros

let input = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18];

function squareWave(arr) {
  let zeros = 0;
  let output = [];

  for (let i = 0; i < arr.length; i++) {
    if (arr[i] % 5 === 0) {
      zeros = 1 - zeros
    }
    output[i] = zeros;
  }
  return output;
}

console.log(squareWave(input));

Or in short:

let input = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18];
let res = input.map(i => Math.abs(Math.floor(i / 5) % 2))
console.log(res)

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use Array's map function. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

let control = 0;
const list = input.map(value => {
  if (value % 5 === 0) {
    control = control === 0 ? 1 : 0;
  }
  return control;
});
console.log(list);
about 4 years ago · Juan Pablo Isaza Relatório

0

As this is clearly a homework question, I won't give you the answer, but suggest your next steps. You actually almost have the idea of the answer, but you didn't actually implement it.

I'm assuming your current code output looks like [0, 0, 0, 0, 1, 0, 0, 0, 0, 1, ...]

At the beginning of your code you have a boolean variable called zeros. What do you think this boolean is for, and why haven't you used it in your loop?

So your current code is outputting 1 when the value is a multiple of 5, but then it forgets immediately after. How might you fix that?

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