elo
elo

Reputation: 11

looping an array in javascript and doubling the numbers based on condition

I am a new in programming so bear me, i find a problem and my question is how to make that in array using javascript that each element preceded by six is multiplied by 2 even if the element multiplied is 6 the condition still remain and avoiding to apply the condition if the multiplied is 3 and make Six ?

var arr = [6, 6, 6, 6, 3, 2];

for (var i = 0; i < arr.length; i++) {
  if (arr[i] == 6) {
    arr[i + 1] *= 2
  }
}

console.log(arr)

i expected the output to be

var arr= [6,12,12,12,6,2]

Upvotes: 0

Views: 291

Answers (6)

d.b
d.b

Reputation: 32558

var arr = [6, 6, 6, 6, 3, 2];
var ar2 = [arr[0]];

for (var i = 1; i < arr.length; i++) {
  if (arr[i - 1] == 6) {
    ar2.push(arr[i] * 2)
  } else {
    ar2.push(arr[i])
  }
}

console.log(ar2)

Upvotes: 0

victor
victor

Reputation: 41

When you do your first loop, you change the second item to be 12, right? It's no longer equal to 6, so it doesn't change the 3rd item. the 3rd is still 6, so it changes the 4th item and so on and so forth. So you if you have an array filled with 6, it changes every other 6.

If you want your expected output, I'd suggest making a temp copy of your array, checking the original array, changing the temp array, then returning the temp array.

edit:

var arr=[6,6,6,6,3,2];
var temparr = arr.slice();

for( var i=0;i<arr.length;i++){
   if(arr[i]==6){
      temparr[i + 1] = temparr[i + 1] * 2;
   }
}

arr.slice() makes a separate copy of the array. Without it, any change to temparr would change arr as well.

Upvotes: 0

adiga
adiga

Reputation: 35259

You could loop the array in the reverse order

var arr = [6, 6, 6, 6, 3, 2];

for (var i = arr.length - 2; i >= 0; i--) {
  if (arr[i] == 6) {
    arr[i + 1] *= 2
  }
}

console.log(arr)

I'm starting i at arr.length - 2 because the last number needs to be ignored. Otherwise, if the last number is 6, arr[i+1] *= 2 will add an extra NaN at the end

Upvotes: 2

Bill Cheng
Bill Cheng

Reputation: 956

With what you already have... i made a little modification to as below

let arr = [6, 6, 6, 6, 3, 2];
let prev = arr[0];
for (var i = 1; i < arr.length; i++) {
  let current = arr[i]
  if (prev == 6) {
    arr[i] *= 2
  }
  prev = current
}

console.log(arr)

The idea you had was to change the next element in the array but the problem is when it comes to the next element, you had changed it to another value... there are many ways to achieve this. One of them is shown above.

  1. It remembers the previous value (the original value)
  2. If previous value is 6, multiply current value by 2
  3. Set previous value to current value
  4. repeat step 2 until it is done

Upvotes: 0

Laurentiu Turcu
Laurentiu Turcu

Reputation: 140

You should map that array. What Array.prototype.map does, it takes a function and applies that function to all the array elements, and creates a new array from what that function returns. It passes three arguments to that function (the element, the index of the element and the initial array).

let newArr = arr.map(function(x, index, array) {
    if (index == 0) return x;

    if (array[index-1] == 6) return x*2

    return x;
})

Upvotes: 0

Abdelillah Aissani
Abdelillah Aissani

Reputation: 3108

Just use map and check if the previous element is 6 then return the value multiplied by 2 else just return the value

var arr = [6, 6, 6, 6, 3, 2];

var newarr = arr.map((x,i) => arr[i-1] == 6 ? x*2 :x)

console.log(newarr)

Upvotes: 1

Related Questions