Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

118
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda