Home > Enterprise >  How do I Replace the elements in the Array and assign it zero in javascript?
How do I Replace the elements in the Array and assign it zero in javascript?

Time:08-04

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]

CodePudding user response:

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);

CodePudding user response:

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?

CodePudding user response:

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)

CodePudding user response:

To replace an element in an array:

Use the indexOf() method to get the index of the element. Use bracket notation to change the value of the element at the specific index. The value of the array element will get updated in place.

const arr = ['a', 'b', 'c'];

const index = arr.indexOf('a'); // it will give you 0

if (index !== -1) {
  arr[index] = 'z';
}

console.log(arr); // it will give you ['z', 'b', 'c']

  • Related