Build a One-Time Password Generator - Build a One-Time Password Generator

Tell us what’s happening:

it works as steps mentioned, but still show errors, 10,11,12,13 and 15, 16.

Your code so far

<!-- file: index.html -->
<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8" />
    <title>OTP Generator</title>
    <link rel="stylesheet" href="styles.css" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/18.3.1/umd/react.development.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/18.3.1/umd/react-dom.development.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/7.26.5/babel.min.js"></script>
    <script
      data-plugins="transform-modules-umd"
      type="text/babel"
      src="index.jsx"
    ></script>
</head>

<body>
    <div id="root"></div>
    <script
      data-plugins="transform-modules-umd"
      type="text/babel"
      data-presets="react"
      data-type="module"
    >
      import { OTPGenerator } from './index.jsx';
      ReactDOM.createRoot(document.getElementById('root')).render(<OTPGenerator />);
    </script>
</body>

</html>
/* file: styles.css */
.container {
  text-align: center;
  margin-top: 75px;
}

#opt-timer{
  padding: 5px;
}

#generate-opt-button{
  padding: 10px 20px;
  margin-top: 20px;
}
/* file: index.jsx */
const { useState, useEffect, useRef } = React;

export const OTPGenerator = () => {

  const [disbale, setDisbale] = useState(false);
  const [msgVisible, setMsgVisible] = useState(false);
  const [randomNumber, setRandomNumber] = useState(null);
  const initialTime = 5;
  const [seconds, setSeconds] = useState(initialTime);
  const [timeActive, setTimeActive] = useState(true);

  const genSixDigitRandomNumber = () => {
    const min = 100000;
    const max = 999999;
    const newRandomNumber = Math.floor( Math.random() * ( max - min + 1)) + min;
    setRandomNumber(newRandomNumber);
    setSeconds(5);
    setTimeActive(true);
    setMsgVisible(true);
  };

  useEffect(() => {
    let intervale = null;
    if ( timeActive && seconds > 0 ){
      intervale = setInterval (() => {
        setSeconds( (prvSeconds) => prvSeconds - 1 );
      }, 1000); //every second
      setDisbale(true);
    }else if ( seconds === 0) {
      setTimeActive(false);
      clearInterval(intervale);
      setDisbale(false);
      setMsgVisible(false);
    }
    return () => clearInterval(intervale);
  }, [timeActive, seconds]);

return  (

  <div className="container">
    <h1 id="otp-title">OTP Generator</h1>
    <h2 id="otp-display">
      { randomNumber ?
         randomNumber : "Click 'Generate OTP' to get a code" 
      }
    </h2>
    <p id="otp-timer" aria-live="assertive">
    { randomNumber && 
      ( !timeActive && seconds === 0 
        ? "OTP expired. Click the button to generate a new OTP."
        : `Expires in: ${seconds} seconds`
      )}  
    </p>
    <button id="generate-otp-button" disabled={disbale} onClick={genSixDigitRandomNumber} >Generate OTP</button>
  </div>

);

};

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/26.1 Safari/605.1.15 Ddg/26.1

Challenge Information:

Build a One-Time Password Generator - Build a One-Time Password Generator

Build-One-Time-Password-Errors

Did you update the code in the meantime? I’m getting with it just test 10 to fail.

What’s interesting is when preview is loaded, it’s not possible to use Generate OTP button for a few seconds, because it’s disabled. Is that intended?

Thank you, No I didn’t update any code, i think changed for something, and didn’t to set back zero.

but now, 13, 15 and 16 showing error.

Now? I’m still seeing only test 10 failing with this code.

New-OPT-Error1

I can’t see that , i don’t know , how to check this error, i still try to find out what i am doing wrong.

fixed it, i used useRef, otherwise timer is not setting 0. enabling disable works fine in background timer is not setting.