Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

119
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!