Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

158
Visualizações
troubles with js logic gates

i was trying to learn javascript in order to program websites i already knew html and css and thought about making a simple logic gate, but once i had built it i couldnt get it to work. this is the html doc with the script below.

<!DOCTYPE html>
<html>
<body>
<p id="logic">xor: 0 or: 0 and: 0</p>
<button onclick="thisScript()">script</button>
<script>
let A = 1
let B = 1
function thisScript() {
if ((A == 1 || B == 1)&& !(A == 1 && B == 1)) {
let xor = 1
} else {
let xor = 0
}
if (A == 1 || B == 1) {
let or = 1
} else {
let or = 0
};
if (A == 1 && B == 1) {
let and = 1
} else {
let and = 0
};
document.getElementById("logic").innerHTML = `xor: ${xor} or: ${or} and: ${and}`
};
</script>
</body>
</html>

i tried moving the dom inside each of the if/else statements and it still didnt work it still said that the variable xor was undefined

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

This is just a variable scope problem, your variables were scoped to the if statements which meant that they weren't accesible to the last line of code in your function. The below snippet makes the variables global, but scoping them to the function also works (second snippet). Checkout this webpage for more info on JS scopes.

let A = 1
let B = 1
let xor, and, or;

function thisScript() {
  if ((A == 1 || B == 1) && !(A == 1 && B == 1)) {
    xor = 1
  } else {
    xor = 0
  }
  if (A == 1 || B == 1) {
    or = 1
  } else {
    or = 0
  };
  if (A == 1 && B == 1) {
    and = 1
  } else {
    and = 0
  };
  document.getElementById("logic").innerHTML = `xor: ${xor} or: ${or} and: ${and}`
};
<!DOCTYPE html>
<html>

<body>
  <p id="logic">xor: 0 or: 0 and: 0</p>
  <button onclick="thisScript()">script</button>

</body>

</html>

let A = 1
let B = 1


function thisScript() {
  let xor, and, or;
  if ((A == 1 || B == 1) && !(A == 1 && B == 1)) {
    xor = 1
  } else {
    xor = 0
  }
  if (A == 1 || B == 1) {
    or = 1
  } else {
    or = 0
  };
  if (A == 1 && B == 1) {
    and = 1
  } else {
    and = 0
  };
  document.getElementById("logic").innerHTML = `xor: ${xor} or: ${or} and: ${and}`
};
<!DOCTYPE html>
<html>

<body>
  <p id="logic">xor: 0 or: 0 and: 0</p>
  <button onclick="thisScript()">script</button>

</body>

</html>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda