Javascript for nav highlight not working in IE

I have a great little script for highlighting links on a one page site that I found here on Stack Overflow. It works perfectly in Chrome and Edge but not in IE. Here is a snippet. Any suggestions?

<a href="#one">ONE</a>
<a href="#two">TWO</a>
<a href="#three">THREE</a>
<a href="#four">FOUR</a>
<a href="#five">FIVE</a>
<section id="one">
<section id="two">
<section id="three">
<section id="four">
<section id="five">

const links = document.querySelectorAll('nav a');
const sections = document.querySelectorAll('section');

function changeLinkState() {
  let index = sections.length;

  while(--index && window.scrollY + 1 < sections[index].offsetTop) {}

  links.forEach((link) => link.classList.remove('active'));

window.addEventListener('scroll', changeLinkState);

104 thoughts on “Javascript for nav highlight not working in IE”