0
const log = document.getElementById("log");

document.addEventListener("keydown", logKey);

function logKey(e) {
  if (e.key === "Shift" && "Control" && "CapsLock" && "Alt") {
    log.textContent += "";
  } else {
    log.textContent += ` ${e.key}`;
  }

  console.log(e);
}
#para {
  background: #f4f4f4;
  color: #333;
  border: 1px solid blue;
  height: 200px;
  width: auto;
}

#log {
  background: #333;
  color: #fff;
}
</pre>
<pre>

<!DOCTYPE html>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="./assets/styles/log.css" />
    <title>Getters and Setters</title>
    <script src="./assets/scripts/getter.js" defer></script>
  </head>
  <body>
    <div id="para">
      <p>
        Lorem ipsum dolor sit amet consectetur adipisicing elit. Expedita
        tempora distinctio quaerat vel iusto suscipit itaque dolor ipsa ducimus
        commodi eos maxime nemo temporibus, molestiae maiores. Similique
        reiciendis temporibus aut, optio tempora modi adipisci voluptatibus
        aspernatur explicabo, omnis atque nesciunt.
      </p>
    </div>
    <div id="log"></div>
  </body>
</html>

</pre>
const log = document.getElementById("log"); document.addEventListener("keydown", logKey); function logKey(e) { if (e.key === "Shift" && "Control" && "CapsLock" && "Alt") { log.textContent += ""; } else { log.textContent += ` ${e.key}`; } console.log(e); }
  • Try Using The Following: `e.key === "Shift" && e.key === "Control" && e.key === "CapsLock" && e.key === "Alt"` instead in the if statement – mrtechtroid Apr 02 '22 at 11:06
  • *"Am I using && operator incorrectly?"* Yes, in two ways (I think). Your current code is equivalent to `if (e.key === ("Shift" && "Control" && "CapsLock" && "Alt")) {` which means that `"Shift" && "Control" && "CapsLock" && "Alt"` is evaluated, and results either in `false` or `"Alt"` (it'll be `"Alt"` in that example), and then that value is compared with `e.key`. Separately, `&&` is "and" but you mean `||` ("or"). So `if (e.key === "Shift" || e.key === "Control" || e.key === "CapsLock" || e.key ==="Alt")` (or you might use a `switch`, or an `includes` check on an array). – T.J. Crowder Apr 02 '22 at 11:08
  • @mrtechtroid It's impossible for `e.key` to be `===` both `"Shift"` and `"Control"` (for instance) at the same time. – T.J. Crowder Apr 02 '22 at 11:08
  • Thank you so much. This logic worked with OR || => e.key === "Shift" || e.key === "Control" || e.key === "CapsLock" || e.key === "Alt" – Yashawant Sawant Apr 02 '22 at 11:10

0 Answers0