Problems with a red LED, two push buttons and a piezzo buzzer

I have to make a project for school.
The intention is that when I press a button the LED turns on.
Then after some time the buzzer should start to make a sound.
And then when I press the second button, the led and buzzer should go out and stop.
But if I push the second button before the buzzer makes noise, the LED should also turn off.

But the problem I have is that the LED won’t turn off before the buzzer makes a noise.

This is the code I have now. I have already tried several things and the problem remains.

const int buttonPin1 = A5;
const int ledPin = A4;
const int buttonPin2 = A3;
const int buzzerPin = 12;

int buttonState1 = 0;
int buttonState2 = 0;
boolean canStop = false;

void setup() {
pinMode(buzzerPin, OUTPUT);

pinMode(ledPin, OUTPUT);
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);

void loop() {
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);

if (buttonState1 == HIGH) {
canStop = true;
digitalWrite(ledPin, HIGH);
for (int i=0;i<=50;i++){
delay (50);
if (i == 50){
tone(buzzerPin, 311);
if(canStop == true){
if (buttonState2 == HIGH){
int i = 0;
digitalWrite(ledPin, LOW);

Look into using millis().
Save the time that button1 was pressed, turn on the LED.
Then every pass thru loop, check the conditions like you are doing now, and add a condition to see how much time has elapsed. If button2 is pressed, do some action. If enough time has elapsed, do some action.
That is the basis of Blink Without Delay.
You have the correct logic with the canStop flag, many people never get that part figured out.
Do the same with millis().

if I push the second button before the buzzer makes noise, the LED should also turn off.

    digitalWrite(ledPin, HIGH);
    for (int i = 0; i <= 50; i++)
      delay (50);
      if (i == 50)
        tone(buzzerPin, 311);

You are not reading the input during this for loop so the only way that it will end will be for the for loop to end

Spot the problem ?

Also, why test the value of i inside the for loop ?

As suggested, there are better ways to do the timing but you can make it work using delay()