Javascript 密码检查器显示(0,0,0)的解锁,但不显示给定号码(密码)的解锁 import React,{useState}来自“React”; 导入“/App.css”; 函数App(){ 常量密码=[2,2,3]; const[digits,setDigits]=useState([0,0,0]); const[isUnlocked,setIsUnlocked]=useState(false); 设setDigitAtIndex=(数字,idx)=>{ 设置数字((当前数字)=> [ …currentDigits.slice(0,idx), 数字 …currentDigits.slice(idx+1) ] ); }; 让我们检查密码=()=>{ for(设i=0;i setDigitIndex(parseInt(event.target.value),1)}/> setDigitIndex(parseInt(event.target.value),2)}/> checkPassword()}>按我 {isUnlocked&&
Unlocked} ); } 导出默认应用程序; 在react中运行此应用程序后,当我用0 0单击press me按钮时,它显示为unlocked,但在键入2 2 3后它不工作,但在键入223后它应该工作需要帮助?任何正在学习react的人都可以回答此问题Javascript 密码检查器显示(0,0,0)的解锁,但不显示给定号码(密码)的解锁 import React,{useState}来自“React”; 导入“/App.css”; 函数App(){ 常量密码=[2,2,3]; const[digits,setDigits]=useState([0,0,0]); const[isUnlocked,setIsUnlocked]=useState(false); 设setDigitAtIndex=(数字,idx)=>{ 设置数字((当前数字)=> [ …currentDigits.slice(0,idx), 数字 …currentDigits.slice(idx+1) ] ); }; 让我们检查密码=()=>{ for(设i=0;i setDigitIndex(parseInt(event.target.value),1)}/> setDigitIndex(parseInt(event.target.value),2)}/> checkPassword()}>按我 {isUnlocked&&,javascript,html,reactjs,Javascript,Html,Reactjs,Unlocked} ); } 导出默认应用程序; 在react中运行此应用程序后,当我用0 0单击press me按钮时,它显示为unlocked,但在键入2 2 3后它不工作,但在键入223后它应该工作需要帮助?任何正在学习react的人都可以回答此问题 let setDigitAtIndex=(digit,idx)=>{ let temp = [...digits]; temp[idx] = digit; setDigits(temp); }; 这看起来
let setDigitAtIndex=(digit,idx)=>{
let temp = [...digits];
temp[idx] = digit;
setDigits(temp);
};
这看起来容易多了
此外,您的checkpassword功能不正确
将==
替换为==代码>
let checkPassword=() =>{
for (let i=0;i<password.length;i++){
if(password[i]!==digits[i]){
setIsUnlocked(false); // if the password is wrong, it will hide the text again
return;
}
}
setIsUnlocked(true);
}
让checkPassword=()=>{
对于(设i=0;iif(密码[i]==digits[i]){return;}
如果匹配,则退出..批准为答案