A.Kao
A.Kao

Reputation: 31

JavaScript Arrays Counting

everyone, I had met some problems in my codes, I want to make them count downwards.However, dunno how why it comes like below:

My Codes:

var countx = [];

function range(start, end, step) {
  if (step === undefined) {
    step = 1;
    for (var i = start; i <= end; i += step)
      countx.push(i);
    return countx;
  } else {
    for (var y = start; y >= end; y += step)
      countx.push(y);
    return countx;
  }
}

console.log(JSON.stringify(range(1, 10))); // -> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

console.log(JSON.stringify(range(5, 2, -1))); // -> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 5, 4, 3, 2]

Can anyone tell me where I made it wrong?

Upvotes: 1

Views: 50

Answers (3)

Nina Scholz
Nina Scholz

Reputation: 386540

You could use a single loop and a check for the condition.

function range(start, end, inc) {
    var i, result = [];
    inc = inc || Math.abs(end - start) / (end - start) || 1;
    for (i = start; inc > 0 ? i <= end : i >= end; i += inc) {
        result.push(i);
    }
    return result;
}

console.log(JSON.stringify(range(1, 10)));
console.log(JSON.stringify(range(5, 2)));
console.log(JSON.stringify(range(7, 7)));

Upvotes: 0

Weedoze
Weedoze

Reputation: 13943

You should declare your array inside your function or it will keep its data even after calling the function.

Before 1st range() call : countx = []

After 1st range()call : countx = [1,2,3,4,5,6,7,8,9,10]

Before 2nd range() call : countx = [1,2,3,4,5,6,7,8,9,10] //Here is the problem

function range(start, end, step) {
  let countx = [];
  if (step === undefined) {
    step = 1;
    for (var i = start; i <= end; i += step)
      countx.push(i);
    return countx;
  } else {
    for (var y = start; y >= end; y += step)
      countx.push(y);
    return countx;
  }
}

console.log(JSON.stringify(range(1, 10)));

console.log(JSON.stringify(range(5, 2, -1)));

Upvotes: 1

kevguy
kevguy

Reputation: 4438

It's because of your countx variable.

When you first execute range, countx is [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] like you expected. But when you execute it one more time, you keep pushing new values into countx, thus you have [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 5, 4, 3, 2].

In order to fix it, put countx into the function so the variable is scoped to the function and will be initialized everytime when range is executed.

function range(start, end, step) {
  let countx = [];
  if (step === undefined) {
    step = 1;
    for (var i = start; i <= end; i += step)
      countx.push(i);
    return countx;
  } else {
    for (var y = start; y >= end; y += step)
      countx.push(y);
    return countx;
  }
}

console.log(JSON.stringify(range(1, 10))); // -> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

console.log(JSON.stringify(range(5, 2, -1))); // -> [5, 4, 3, 2]

console.log(JSON.stringify(range(1, 10).reverse()))

You can also use Array.prototype.reverse (link) to reverse the array of the array, as illustrated in the snippet.

Upvotes: 1

Related Questions