-6

My function is taking in 2 integer parameters and returning the numbers between those two parameters. I cannot convert the numbers shown into an array.

As I'm supposed to buildArray function that takes two Numbers, and returns an Array filled with all numbers between the given number: buildArray(5, 10) should return [5, 6, 7, 8, 9, 10].

I have tried using split to convert the values into an array but I've failed many times.

My code prints each value separately whereas i need the values to be displayed in an array.

function buildArray(a, b) {
  for (var i = 0; i <= b; i++) {
    i = a;
    console.log(a);
    a = a + 1;
  }
}
console.log(buildArray(5, 10));
Shahnewaz
  • 360
  • 4
  • 12
  • Possible duplicate of [Does JavaScript have a method like "range()" to generate a range within the supplied bounds?](https://stackoverflow.com/questions/3895478/does-javascript-have-a-method-like-range-to-generate-a-range-within-the-supp) – Kevin Ji Jan 27 '19 at 17:02
  • 1
    I am confused. Is this what you wanted? `function buildArray(a, b) { var arr=[]; for (var i = a; i <= b; i++) { arr.push(i); } return arr; }` – mplungjan Jan 27 '19 at 17:05
  • 1
    @mplungjan you should put that as an answer. – Giovanni Di Toro Jan 27 '19 at 17:09
  • @GiovanniDiToro Ok. Done :) Very weird answers here today – mplungjan Jan 27 '19 at 17:10

3 Answers3

3

Here is the simplest version using your original line of thoughts

Implementing a range function can be more complex than this, but since this works, I suggest it for now

function buildArray(a, b) {
  var arr = [];
  for (var i = a; i <= b; i++) {
    arr.push(i);
  }
  return arr;
}
console.log(buildArray(5, 10));
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • 1
    You have my support, +1! This will be adequate for him – Shidersz Jan 27 '19 at 17:19
  • This answer doesn't cover the case where `b` is lower than `a`. I'm know that OP didn't mention this case specifically but it may have been useful to make him aware. @Shidersz answer covers that case. – customcommander Jan 27 '19 at 21:53
  • If you need such function, you create it or sort the result. For example String.substring cannot return characters in reverse order from the end. The suggestion function is as simple as such a built-in method – mplungjan Jan 28 '19 at 06:48
2

You can construct a new array filled with zeros of the length you need and then use map() to set the values. I have updated to a generalized version for accept other types of ranges.

function buildArray(a, b)
{
    let n = Math.abs(b - a);
    return Array(n + 1).fill(0).map((x, idx) => a + ((b >= a) ? idx : -idx));
}

console.log(JSON.stringify(buildArray(5, 10)));
console.log(JSON.stringify(buildArray(12, 3)));
console.log(JSON.stringify(buildArray(1, -2)));
console.log(JSON.stringify(buildArray(-1, -1)));
Shidersz
  • 16,846
  • 2
  • 23
  • 48
  • I think there is no `why?`, he can just fix the iterator of the loop, this is just an alternative solution. – Shidersz Jan 27 '19 at 17:12
  • Lovely solution. Not understandable for someone who just started programming and got to the for loop - and not pragmatic at all. Super complex for something incredibly simple. Run yours for 100000 items and compare to my suggestion – mplungjan Jan 27 '19 at 17:18
  • Wow. Getting more and more complex :) Filling and Math.abs and ternaries, oh my! – mplungjan Jan 27 '19 at 17:50
  • 1
    Lol, I really know what is your point @mplungjan , but maybe this can be useful for someone in the future :). – Shidersz Jan 27 '19 at 17:58
  • I don't think this solution is over-engineered at all. It's perfectly readable and covers the case were `b` is lower than `a`. – customcommander Jan 27 '19 at 21:55
  • I call it feature creep It is three times as hard to debug now - but each to his own. – mplungjan Jan 28 '19 at 06:48
1

Try the following (using Array.from):

function buildArray(min, max) {
  return Array.from({ length: max - min + 1 }, (_, i) => min + i);
}

console.log(buildArray(5, 10));
Kevin Ji
  • 10,479
  • 4
  • 40
  • 63
  • @mplungjan Using `Array.from` is more natural than a for loop. In fact, [one of the examples](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from#Sequence_generator_(range)) on the MDN page suggests that it can be used for implementing `range`. – Kevin Ji Jan 27 '19 at 17:13
  • 2
    Sure. But this is a BRAND NEW programmer. Kudos to show the shiny new features of the language but if the do not grasp a for loop, then an Array.from is an alien language. Just look at your code as if you never did any JS before – mplungjan Jan 27 '19 at 17:14
  • 1
    @mplungjan I'm not convinced that functional concepts are necessarily harder than imperative loops. e.g. you don't need to understand how iteration works to guess what `[1, 2, 3].map(ele => ele * ele)` does. This answer is essentially doing that: creating a `max - min + 1` length array, and mapping each element to `min + i` (with `i` being the index of the element). – Kevin Ji Jan 27 '19 at 17:19
  • 1
    But that is not just what you are doing. You are using arrow functions, an Array.from constructor with a cryptic OBJECT with a length member and passing it a map function too. I have been coding JS since 95 and have to think hard about what you did because I have not yet used an Array.from myself. I had to look it up at MDN – mplungjan Jan 27 '19 at 17:26