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

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 后应该可以工作需要帮助吗?以及任何正在学习反应的人那些可以回答这个问题


繁星点点滴滴
浏览 38回答 1
1回答

慕姐8265434

let setDigitAtIndex=(digit,idx)=>{&nbsp; &nbsp; let temp = [...digits];&nbsp; &nbsp; temp[idx] = digit;&nbsp; &nbsp; setDigits(temp);&nbsp; };这看起来容易多了?您的检查密码功能也不正确。替换===为!==let checkPassword=() =>{&nbsp; &nbsp; for (let i=0;i<password.length;i++){&nbsp; &nbsp; &nbsp; if(password[i]!==digits[i]){&nbsp; &nbsp; &nbsp; &nbsp; setIsUnlocked(false);&nbsp; &nbsp;// if the password is wrong, it will hide the text again&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; setIsUnlocked(true);&nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5