How to check the value of a two-dimensional array in a for statement

Asked 1 years ago, Updated 1 years ago, 31 views

I want to use the for statement to get the correct or incorrect values of the two-dimensional array, but it doesn't reflect well.I'm a beginner, so I'm doing it while searching online, but I don't know.Please let me know.

"Also, if you enter a value that is not in the two-dimensional array, the alert says ""ID and password do not match"", but it appears three times in a row, which is troubling me."If anyone knows the solution, please take care of it.

function logincheck(){
  constid = document.getElementById('id').value;
  const pw = document.getElementById('pas').value;

  // User Provisional Number
  const username = [
    ['ss', '2222'],
    ['df', '4444'],
    ['we', '5555'],
  ];

  // ID PW not entered
  if(id==="&pw===""){
    alert("ID and password are required");
  
  // ID not entered
  } else if(id===""){
    alert("ID is required");
  
  // When PW is not entered
  } else if(pw===""){
    alert("Password is required");
  } else {
    // ID PW match check
    for(leti=0;i<usernum.length;i++){
      let item=usernum[i];
      if(item[0]===id&&item[1]===pw){
        window.location.href='html Top Screen.html';
      } else {
        alert("ID and password do not match");
      }
    }
  }
}

javascript

2022-09-30 20:16

1 Answers

I think it's more of an algorithm problem than a problem with how to write a program.
The title says
How to check the array correctly
"Actually, I think it will be ""how to verify the existence of an array."""

The IDPW match program currently written is as follows:

//ID PW match check
for(leti=0;i<usernum.length;i++){
  let item=usernum[i];
  if(item[0]===id&&item[1]===pw){
    window.location.href='html Top Screen.html';
  } else {
    alert("ID and password do not match");
  }
}

Describe this program
·Redirect if any matches the array
·Alert if there is anything that does not match the array
It is an algorithm called .
This is true or false, but what I want to do is not true or false, but
It should be in the array.

The algorithm that should be done is
·Redirect if any matches the array
·Alerts
if none of the arrays match It will be.
This algorithm allows you to check if it exists in an array.

There are several ways to meet the above requirements, but
If you write simply, you can turn the for statement and divide it into matches. If it doesn't fit in the if block, you can issue an alert, but I think it's simple.

//ID PW match check
for(leti=0;i<usernum.length;i++){
  let item=usernum[i];
  if(item[0]===id&&item[1]===pw){
    window.location.href='html Top Screen.html';
  }
}
alert("ID and password do not match"); // At this point, location.href is not running, so it can be considered that it did not exist in the array.

By the way,
Login PWs are not implemented on the client side of javascript because generally authentication mechanisms should be done on the server side, and I think they are often implemented using RDBs such as MySQL instead of spinning the array.
It may be for practice, but it may not be a common practical problem.
It's a good practice for algorithms and programming, but I think it's better to understand that this method is not common.


2022-09-30 20:16

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.