-1

I found the below script in the answers to another question. It generates an array of random unique numbers from 0 to 4 and works fine in most browsers, but gives a syntax error in Internet Explorer.

const n1 = myQuestions.length;
const n2 = 5;

let pool = [...Array(n1).keys()];

var result = [];

while (result.length < n2) {
   let index = Math.floor(Math.random() * pool.length);
   result = result.concat(pool.splice(index, 1));       
}

This appears to be the line giving the error:

    let pool = [...Array(n1).keys()];

I'm not sure specifically why this line is causing an error but my guess is that some of the script is not supported by IE.

Is there a way I could modify this script or add a polyfill so that it works in IE?

gnorman
  • 163
  • 10
  • 1
    Internet Explorer does not support the spread syntax for destructuring objects. – Pointy Aug 04 '20 at 16:19
  • Neither does it support [`Array.prototype.keys()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/keys#Browser_compatibility) – Andreas Aug 04 '20 at 16:24

1 Answers1

1

I suggest try to modify your code like a sample below may help to work it in the IE browser.

const result = []
const values = ['a', 'b']
Array.prototype.push.apply(result, values)
console.log(result)

Reference:

Alternatives of spread syntax

There is a polyfill for Object.keys(). you can also try to refer to it.

Deepak-MSFT
  • 10,379
  • 1
  • 12
  • 19