I have a problem with the foreach loop. Written code using the foreach loop does not work but if I use the for loops everything works as it should. I look for solution in internet few hours and nothing. I put two examples. First example is use foreach loop and second example is use for loop. Sorry but my english is not the best.
Example - foreach loop (broken):
window.onload = () => {
  test();
};
function test() {
  let testDiv = Array.from(document.getElementsByClassName("test"));
  let currentTest = testDiv[0];
  currentTest.className += " test2";
   testDiv.forEach((current) => {
    current.addEventListener('mousemove', () => {
      currentTest.className = currentTest.className.replace("test2", "");
      currentTest = this;
      currentTest.className += " test2";
    });
  }); 
}.test {
  width: 50px;
  height: 50px;
  float: left;
  cursor: pointer
}<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" href="css/style.css"/>
        <script src="js/script.js" async></script>  
    </head>
    <body>     
        <div>
    <div class="test">1</div>
    <div class="test">2</div>
    <div class="test">3</div>
    <div class="test">4</div>
    <div class="test">5</div>
  </div>   
    </body>
</html>Example - for loop (works fine):
window.onload = () => {
  test();
};
function test() {
  let testDiv = Array.from(document.getElementsByClassName("test"));
  let currentTest = testDiv[0];
  currentTest.className += " test2";
  for (var i = 0; i < testDiv.length; i++) {
   testDiv[i].onmouseover = function() {
    currentTest.className = currentTest.className.replace("test2", "");
    currentTest = this;
    currentTest.className += " test2";
   };
  }
}.test {
  width: 50px;
  height: 50px;
  float: left;
  cursor: pointer
}<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" href="css/style.css"/>
        <script src="js/script.js" async></script>  
    </head>
    <body>     
        <div>
    <div class="test">1</div>
    <div class="test">2</div>
    <div class="test">3</div>
    <div class="test">4</div>
    <div class="test">5</div>
  </div>   
    </body>
</html> 
     
    