增添一个点击取消背景色的功能

问题遇到的现象和发生背景

想添加一个功能 将已变背景色的按钮被点击时能取消背景色

问题相关代码,请勿粘贴截图
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .bg1{background: aquamarine;}
        .bg2{background: red;}
    </style>
</head>
<body>
    <button class="bg1">第1个</button><button>第2个</button><button>第3个</button><button>第4个</button><button>第5个</button>
    <script>
        let btn = document.querySelectorAll('button')
        for(let i = 0;i<btn.length;i++){
            btn[i].addEventListener('click',function(){
                document.querySelector('.bg1').classList.remove('bg1')
                btn[i].classList.add('bg1')
            })
                    
        }
        
        
        
        
        
    </script>
</body>
</html>

运行结果及报错内容
我的解答思路和尝试过的方法
我想要达到的结果

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .bg1{background: aquamarine;}
        .bg2{background: red;}
    </style>
</head>
<body>
    <button class="bg1">第1个</button><button>第2个</button><button>第3个</button><button>第4个</button><button>第5个</button>
    <script>
        let btn = document.querySelectorAll('button')
        let index = 0
        for(let i = 0;i<btn.length;i++){
            btn[i].addEventListener('click',function(){
                if(index === i){
                    if(btn[i].className){
                        document.querySelector('.bg1').classList.remove('bg1')
                        console.log(1)
                    }else{
                        btn[i].classList.add('bg1')
                        console.log(2)
                    }
                }else{
                    index = i
                    if(document.querySelector('.bg1')){
                        document.querySelector('.bg1').classList.remove('bg1')
                    }
                    btn[i].classList.add('bg1')
                }
            })
        }
    </script>
</body>
</html>