Build a One-Time Password Generator - Steps 10 to 16

Hello,

everything works as expected but for some reason my code doesn’t pass test from 10 to 16. Any idea why?

const { useState, useEffect, useRef } = React;

export const OTPGenerator = () => {
  const [otp, setOtp] = useState(null);
  const [timeLeft, setTimeLeft] = useState(null);
  const [expired, setExpired] = useState(false);

  const intervalRef = useRef(null);
  const timeoutRef = useRef(null);

  useEffect(() => {
    return () => {
      if (intervalRef.current) {
        clearInterval(intervalRef.current);
        intervalRef.current = null;
      }
      if (timeoutRef.current) {
        clearTimeout(timeoutRef.current);
        timeoutRef.current = null;
      }
    };
  }, []);

  const generateOTP = () => {
    if (intervalRef.current) {
      clearInterval(intervalRef.current);
      intervalRef.current = null;
    }
    if (timeoutRef.current) {
      clearTimeout(timeoutRef.current);
      timeoutRef.current = null;
    }

    const newOtp = Math.floor(100000 + Math.random() * 900000);
    setOtp(newOtp);
    setExpired(false);
    setTimeLeft(5);

    intervalRef.current = setInterval(() => {
      setTimeLeft((prev) => {
        if (prev > 1) return prev - 1;

        const next = prev > 0 ? prev - 1 : 0;
        if (intervalRef.current) {
          clearInterval(intervalRef.current);
          intervalRef.current = null;
        }
  
        timeoutRef.current = setTimeout(() => {
          setExpired(true);
          timeoutRef.current = null;
        }, 1000);

        return next;
      });
    }, 1000);
  };

  let timerText = "";
  if (otp !== null && !expired) {
    timerText = timeLeft !== null ? `Expires in: ${timeLeft} seconds` : "";
  } else if (otp !== null && expired) {
    timerText = "OTP expired. Click the button to generate a new OTP.";
  }

  const isCounting = otp !== null && !expired && timeLeft !== null;

  return (
    <div className="container">
      <h1 id="otp-title">OTP Generator</h1>

      <h2 id="otp-display">
        {otp ? otp : "Click 'Generate OTP' to get a code"}
      </h2>

      <p id="otp-timer" aria-live="polite">
        {timerText}
      </p>

      <button
        id="generate-otp-button"
        onClick={generateOTP}
        disabled={isCounting}
      >
        Generate OTP
      </button>
    </div>
  );
};

Link to Lab: https://www.freecodecamp.org/learn/full-stack-developer/lab-one-time-password-generator/build-a-one-time-password-generator

Hi @davidemarca

Your solution works from my end. Please try one of the following steps to move forward.

Click on the “Restart Step” button and force a refresh of your page with CTRL + F5 then try to paste the code in again.

or - Try the step in incognito or private mode.

or - Disable any/all extensions that interface with the freeCodeCamp website (such as Dark Mode, Ad Blockers, or Spellcheckers), and set your browser zoom level to 100%. Both of these factors can cause tests to fail erroneously.

or - Ensure your browser is up-to-date or try a different browser.

I hope one of these will work for you.

Happy coding

Hi teller,

thanks for the answer. Unfortunately none of what you said seems to work on my end, I don’t know what else to do…

What different browser did you try?

FireFox Incognito Mode

And previously you were not using FireFox?

It worked with MS Edge. Do you know why FireFox failed the test?

I don’t. But that’s exactly why we recommend trying a different browser.