A
A
Anon33632020-04-30 20:25:21
JavaScript
Anon3363, 2020-04-30 20:25:21

How to convert this array in 13 steps?

5eab09879ab25314323390.png
here the array [2,3,3,5,5,5,4,12,12,10,15] must be converted in 13 steps to [2,3,4,5,6,7,8,9,10, 11.12]

function solution(x){
  let count = 0;
  for(let i = 0 ; i < x.length;i++){

    if(x[i] > x[i + 1]){
      x[i + 1]  = x[i] + 1
      count++
      console.log(x)
    }
    if(x[i] < x[i + 1]){
      x[i + 1] = x[i] + 1
      count++
    }
    console.log(x)
    if(x[i] >= x[i + 1]){
      x[i + 1] = x[i] + 1
      count++
    }
    console.log(count)
  }
}

console.log(solution([2,3,3,5,5,5,4,12,12,10,15]))

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Igor, 2020-04-30
@loonny

Precisely in 13 steps or a maximum of 13 steps? I understand this is some kind of assignment from the courses.
because you can do it just like this:

function solution(arr){
  return arr.sort( (a, b) => a <b )
}

console.log(solution([2,3,3,5,5,5,4,12,12,10,15]))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question