-1

我用 JavaScript 为我的游戏创意制作了一个登录系统,但显然我的 ide 说它太复杂了,我是否需要将一个功能分成更多部分?它会减少计算机处理时间吗?我只是不知道这是否重要。

无论如何,这是代码:

class Log {
  constructor() {
    this.list = {
      username: ["admin", "helper"],
      password: ["admin", "h24"]
    };
    this.user = document.getElementById('username');
    this.psw = document.getElementById('password');
    this.posUser = null;
    this.posPsw = null;
    this.t = true;
  }

  login() {
    if (this.user.value != '' && this.user.value != null) {
      if (!this.list.username.includes(this.user.value.toLowerCase())) {
        errors.innerHTML = 'This user does not exist.';
      } else {
        for (let i = 0; i < this.list.username.length; i++) { //user[pos]
          let j = this.user.value.toLowerCase();
          if (j === this.list.username[i]) {
            this.posUser = i;
          }
        }
        for (let k = 0; k < this.list.password.length; k++) { //psw[pos]
          let l = this.psw.value;
          if (l === this.list.password[k]) {
            this.posPsw = k;
          }
        }
        if (this.posUser === this.posPsw) {
          //access
          console.log('access');
        } else { // user[pos] != psw[pos] then show error
          errors.innerHTML = 'Incorrect password.';
        }
      }
    }
  }
}
let errors = document.querySelector('.error');
let invite = new Log();

document.querySelector('.btnLog').addEventListener('click', function() {
  invite.login();
});
* {
  margin: 5px;
}
<div class="form">
  <div class="inline">
    <label>user</label><input type="text" id="username" autocomplete="off" />
  </div>
  <div class="inline">
    <label>psw</label><input type="password" id="password" autocomplete="off" />
    <div class="eye"></div>
  </div>
  <div class="flex-start">
    <button class="btn btnLog">login</button>
  </div>
  <div class="inline none -error">
    <div class="err_img"></div>
    <div class="error"></div>
  </div>
</div>

4

1 回答 1

1

如果您的 IDE 使用 Sonar 来计算认知复杂度,我建议您将代码分解为多个方法调用

阅读这篇博文以了解更多信息https://blog.sonarsource.com/cognitive-complexity-because-testability-understandability

于 2021-08-16T13:05:26.510 回答