2

I have an array with two elements [5, 50]. I want this array iterate as [5, 6, 7,......, 49, 50].

I try below code. But not working as my expectation.

function All(arr) {
  let newArry = [];
  for(let i = arr[0]; i < arr[1]; i++ ) {
    newArry[i] = newArry.push(i);
  }
  return newArry;
}

console.log(All([5, 50]));

2
  • There are only two elements in your array.... you don't have 45 elements in your array. Can you clarify what it is exactly that you want to do? Sounds like you just need a for loop. Commented Jul 21, 2019 at 6:13
  • What do you expect newArry.push(i) to return? Commented Jul 21, 2019 at 6:27

3 Answers 3

2

Do like this remove newArry[i] =

function All(arr) {
    let newArry = [];
    for(let i = arr[0]; i < arr[1]; i++ ) {
        newArry.push(i);
    }
    return newArry;
}    
console.log(All([5, 50]));

Sign up to request clarification or add additional context in comments.

Comments

0
  1. Get the two values you want to iterate between:

const [start, end] = [5, 50];

  1. Create a new array of length difference between your two points:

Array(end - start + 1) // [undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined]

  1. Since all the values will be undefined, create an iterator object using Array#keys: Array(end - start + 1).keys() // [object Array Iterator]

  2. Spread that into an array:

[...Array(end - start + 1).keys()] // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45]

  1. Map over the values to create your new array:

[...Array(end - start + 1).keys()].map(i => i + start) // [5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50]

...which gives you something like this:

const range = (n, m) => [...Array(m - n + 1).keys()].map(i => i + n);

const [start, end] = [5, 50];

console.log(range(start, end)); // [5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50]

Or, to maintain the API of the function in your question, like this:

const All = (arr) => {
    const [n, m] = arr;
    return [...Array(m - n + 1).keys()].map(i => i + n);
}

console.log(All([5, 50])); // [5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50]

Comments

-1

The Array.prototype.push method mutates the original array, so reassigning doesn't make sense. Just do:

newArray.push(i);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.