Tell us what’s happening:
I can’t pass 13, 14, 16- the message doesn’t update, the button isn’t disabled, and the countdown doesn’t display an expired message at the end
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 */
/* file: index.jsx */
const { useState, useEffect, useRef } = React;
export const OTPGenerator = () => {
const [otpHeader, setOTPHeader] = useState("Click 'Generate OTP' to get a code");
const [countdown, setCountdown] = useState(null);
const [isButtonDisabled, setIsButtonDisabled] = useState(false);
const [showTimeout, setShowTimeout] = useState(false);
const timerRef = useRef();
function getNewOTP() {
if (timerRef.current) {
clearInterval(timerRef.current)
}
let digits = []
const numbers = "1234567890"
for (let i = 0; i < 6; i++) {
const randomIndex = Math.floor(Math.random() * numbers.length);
digits.push(numbers[randomIndex])
}
let newOTP = digits.join("")
setOTPHeader(newOTP);
setCountdown("5");
setIsButtonDisabled(prevDisabled => !prevDisabled);
setShowMessage(false);
timerRef.current = setInterval(()=>{
setCountdown(prevCountdown => {
if (prevCountdown === 1) {
clearInterval(timerRef.current);
setIsButtonDisabled(prevDisabled => !prevDisabled)
setShowMessage(true)
return null;
}
return prevCountdown - 1;
});
}, 1000);
}
useEffect(() => {
return () => {
if (timerRef.current) {
clearInterval(timerRef);
}
}
}, [])
const timer = countdown !== null ? `Expires in: ${countdown} seconds` : "";
return <div className="container">
<h1 id="otp-title">OTP Generator</h1>
<h2 id="otp-display">{otpHeader}</h2>
<p id="otp-timer" aria-live="assertive">{timer}</p>
<button id="generate-otp-button" onClick={getNewOTP}>Generate OTP</button>
</div>;
};
Your browser information:
User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36
Challenge Information:
Build a One-Time Password Generator - Build a One-Time Password Generator