逻辑门与二进制

时间:2017-03-10 16:28:18

标签: javascript html

我正在尝试使用逻辑门与真正的错误陈述,如AND / OR / XOR。我的控制台一直告诉我第35行和14号都有问题,但我没有看到任何错误。

var or;
var and;
var xor;
var binary = document.getElementById("binary").value;
var binarytwo = document.getElementById("binarytwo").value;

function truefalse(){
  if (binary == 1 && binarytwo == 1){
    document.getElementById("and").innerHTML = "1 or TRUE"
  }else{
    document.getElementById("and").innerHTML = "0 or FALSE"
  }

  if (binary == 1 || binarytwo == 1){
      document.getElementById("or").innerHTML = "1 or TRUE"
  }if else (binary == 1 || binarytwo == 0){
      document.getElementById("or").innerHTML = "1 or TRUE"
  }if else (binary == 0 || binarytwo == 1){
      document.getElementById("or").innerHTML = "1 or TRUE"
  }else{
    document.getElementById("or").innerHTML = "0 or FALSE"
  }
}
<!DOCTYPE html>
<html>
<head>
<h1>Logic Gates</h1>
<br>
</head>
<body>
<h1>Write two Binary Values</h1>
<br>
  <input id = "binary">Type a 0 or 1 here</input>
  <br>
  <input id = "binarytwo">Type a 0 or 1 here</input>
  <br>
  <button onclick = "trueFalse()">"Click Here to get the Statements"</button>
<br>
  <p id="or">OR Statement</p>
  <p id="and">AND Statement</p>
  <p id="xor">XOR Statement</p>
</body>
</html>

4 个答案:

答案 0 :(得分:2)

我认为您的ifelse错误地在这里:

if (binary == 1 || binarytwo == 1){
    document.getElementById("or").innerHTML = "1 or TRUE"
}if else (binary == 1 || binarytwo == 0){
    document.getElementById("or").innerHTML = "1 or TRUE"
}if else (binary == 0 || binarytwo == 1){
    document.getElementById("or").innerHTML = "1 or TRUE"
}else{
    document.getElementById("or").innerHTML = "0 or FALSE"
}

应为else if (..)

<button onclick = "trueFalse()">"Click Here to get the Statements"</button>

应该是..

<button onclick = "truefalse()">"Click Here to get the Statements"</button>

匹配function truefalse()

输入标记是自动关闭的,这意味着您不需要</input>

答案 1 :(得分:0)

您需要重新访问trueFalse函数内的变量。

你也有拼写错误:

  • else if而非if else
  • 函数名称的大小写不匹配

&#13;
&#13;
var or;
var and;
var xor;


function trueFalse(){
  // Look here.
  var binary = document.getElementById("binary").value;
  var binarytwo = document.getElementById("binarytwo").value;
  if (binary == 1 && binarytwo == 1){
    document.getElementById("and").innerHTML = "1 or TRUE"
  }else{
    document.getElementById("and").innerHTML = "0 or FALSE"
  }

  if (binary == 1 || binarytwo == 1){
      document.getElementById("or").innerHTML = "1 or TRUE"
  }else if (binary == 1 || binarytwo == 0){
      document.getElementById("or").innerHTML = "1 or TRUE"
  }else if (binary == 0 || binarytwo == 1){
      document.getElementById("or").innerHTML = "1 or TRUE"
  }else{
    document.getElementById("or").innerHTML = "0 or FALSE"
  }
}
&#13;
<!DOCTYPE html>
<html>
<head>
<h1>Logic Gates</h1>
<br>
</head>
<body>
<h1>Write two Binary Values</h1>
<br>
  <input id = "binary">Type a 0 or 1 here</input>
  <br>
  <input id = "binarytwo">Type a 0 or 1 here</input>
  <br>
  <button onclick = "trueFalse()">"Click Here to get the Statements"</button>
<br>
  <p id="or">OR Statement</p>
  <p id="and">AND Statement</p>
  <p id="xor">XOR Statement</p>
</body>
</html>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

if (condition){
     // ...
} else if (condition2){
     // ...
} else {
     // ...
}

没有if else这样的东西。

答案 3 :(得分:0)

function truefalse(){
  var binary = document.getElementById("binary").value;
  var binarytwo = document.getElementById("binarytwo").value;

  if (binary == 1 && binarytwo == 1){
    document.getElementById("and").innerHTML = "1 or TRUE"
  }else{
    document.getElementById("and").innerHTML = "0 or FALSE"
  }

  if (binary == 1 || binarytwo == 1){
    document.getElementById("or").innerHTML = "1 or TRUE"
  }else{
    document.getElementById("or").innerHTML = "0 or FALSE"
  }
}
<h1>Write two Binary Values</h1>
<br>
  <input id = "binary">Type a 0 or 1 here</input>
  <br>
  <input id = "binarytwo">Type a 0 or 1 here</input>
  <br>
  <button onclick = "trueFalse()">"Click Here to get the Statements"</button>
<br>
  <p id="or">OR Statement</p>
  <p id="and">AND Statement</p>
  <p id="xor">XOR Statement</p>

它是else if,而不是if else

<h1>未进入<head>。你可能意味着<title>

由于您使用else if,因此您无需使用||。如果您使用&&代替,则只有在这种情况下才需要它们。

您每次都需要重新初始化binarybinarytwo。仅访问.value可以获得访问时的内容。由于您在脚本首次运行时访问过一次,因此您只能获得其初始值,而不会获得用户稍后输入的内容。