为了账号安全,请及时绑定邮箱和手机立即绑定

密码检查器显示解锁 (0,0,0) 但未解锁给定数字(密码)

密码检查器显示解锁 (0,0,0) 但未解锁给定数字(密码)

繁星点点滴滴 2023-11-13 10:42:58
import React ,{useState}from 'react';import './App.css';function App() {  const password =[2,2,3];  const [digits ,setDigits]=useState([0,0,0]);  const [isUnlocked,setIsUnlocked]=useState(false);  let setDigitAtIndex=(digit,idx)=>{    setDigits((currentDigits)=>      [        ...currentDigits.slice(0, idx),        digit,        ...currentDigits.slice(idx  + 1)      ]    );  };  let checkPassword=() =>{    for (let i=0;i<password.length;i++){      if(password[i]===digits[i]){        return;      }    }    setIsUnlocked(true);  }  return (    <section>    <h1>Passo</h1>      <div style={{display:'flex'}}>      <input type="number" value={digits[0]}       onChange={(event) => setDigitAtIndex(parseInt(event.target.value),0)}/>       <input type="number" value={digits[1]}           onChange={(event)=>setDigitAtIndex(parseInt(event.target.value),1)}/>      <input type="number" value={ digits[2]}            onChange={(event)=>setDigitAtIndex(parseInt(event.target.value),2)}/>      </div>      <button onClick={()=>checkPassword()}>Press Me</button>      { isUnlocked && <p>Unlocked</p>}    </section>  );}export default App;在反应中运行此应用程序后,当我单击带有 0 0 0 的按我按钮时,它显示已解锁,但在输入 2 2 3 后它不起作用,但在输入 223 后应该可以工作需要帮助吗?以及任何正在学习反应的人那些可以回答这个问题
查看完整描述

1 回答

?
慕姐8265434

TA贡献1813条经验 获得超2个赞

let setDigitAtIndex=(digit,idx)=>{

    let temp = [...digits];

    temp[idx] = digit;

    setDigits(temp);

  };

这看起来容易多了?


您的检查密码功能也不正确。


替换===为!==


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);


  }


查看完整回答
反对 回复 2023-11-13
  • 1 回答
  • 0 关注
  • 32 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信