布尔变量似乎无缘无故变为真

时间:2018-07-30 17:27:08

标签: javascript html

var Number1 = 0
var Number2 = 0
var ZeroVAR = "0"
var OneVAR = "1"
var TwoVAR = "2"
var ThreeVAR = "3"
var FourVAR = "4"
var FiveVAR = "5"
var SixVAR = "6"
var SevenVAR = "7"
var EightVAR = "8"
var NineVAR = "9"
var EquationN1 = 0
var EquationN2 = 0
var AddVAR = false
var SubtractVAR = false
var MultiplyVAR = false
var DiviseVAR = false
var Output = false

function Zero() {
  Number1 = Number1 + ZeroVAR;
}

function One() {
  Number1 = Number1 + OneVAR;
}

function Two() {
  Number1 = Number1 + TwoVAR;
}

function Three() {
  Number1 = Number1 + ThreeVAR;
}

function Four() {
  Number1 = Number1 + FourVAR;
}

function Five() {
  Number1 = Number1 + FiveVAR;
}

function Six() {
  Number1 = Number1 + SixVAR;
}

function Seven() {
  Number1 = Number1 + SevenVAR;
}

function Eight() {
  Number1 = Number1 + EightVAR;
}

function Nine() {
  Number1 = Number1 + NineVAR;
}

function Ok1() {
  SetNumber1();
}

function SetNumber1() {
  EquationN1 = Number1;
  console.log(Number1)

}

function Add() {
  AddVAR = true
}

function Subtract() {
  SubtractVAR = true
}

function Multiply() {
  MultiplyVAR = true
}

function Divise() {
  DiviseVAR = true
}

function Zero2() {
  Number2 = Number2 + ZeroVAR;
}

function One2() {
  Number2 = Number2 + OneVAR;
}

function Two2() {
  Number2 = Number2 + TwoVAR;
}

function Three2() {
  Number2 = Number2 + ThreeVAR;
}

function Four2() {
  Number2 = Number2 + FourVAR;
}

function Five2() {
  Number2 = Number2 + FiveVAR;
}

function Six2() {
  Number2 = Number2 + SixVAR;
}

function Seven2() {
  Number2 = Number2 + SevenVAR;
}

function Eight2() {
  Number2 = Number2 + EightVAR;
}

function Nine2() {
  Number2 = Number2 + NineVAR;
}

function Ok2() {
  SetNumber2();
}

function SetNumber2() {
  EquationN2 = Number2;
  console.log(Number2);
  InitiateCalculation();

}

function InitiateCalculation() {

  if (AddVAR = true) {
    Output = parseInt(EquationN1) + parseInt(EquationN2);
    console.log(Output);
  }
  if (SubtractVAR = true) {
    Output = EquationN1 - EquationN2;
    console.log(Output);
  }

  if (MultiplyVAR = true) {
    Output = EquationN1 * EquationN2;
    console.log(Output);
  }

  if (DiviseVAR = true) {
    Output = EquationN1 / EquationN2;
    console.log(Output);
  }
}
<!DOCTYPE html>
<html>

<head>
  <div id="Zero"><button onclick="Zero()">0</button></div>
  <div id="One"><button onclick="One()">1</button></div>
  <div id="Two"><button onclick="Two()">2</button></div>
  <div id="Three"><button onclick="Three()">3</button></div>
  <div id="Four"><button onclick="Four()">4</button></div>
  <div id="Five"><button onclick="Five()">5</button></div>
  <div id="Six"><button onclick="Six()">6</button></div>
  <div id="Seven"><button onclick="Seven()">7</button></div>
  <div id="Eight"><button onclick="Eight()">8</button></div>
  <div id="Nine"><button onclick="Nine()">9</button></div>
  <div id="Ok1"><button onclick="Ok1()">OK</button></div>

  <div id="Add"><button onclick="Add()">+</button></div>
  <div id="Subtract"><button onclick="Subtract()">-</button></div>
  <div id="Multiply"><button onclick="Multiply()">x</button></div>
  <div id="Divise"><button onclick="Divise()">÷</button></div>

  <div id="Zero2"><button onclick="Zero2()">0</button></div>
  <div id="One2"><button onclick="One2()">1</button></div>
  <div id="Two2"><button onclick="Two2()">2</button></div>
  <div id="Three2"><button onclick="Three2()">3</button></div>
  <div id="Four2"><button onclick="Four2()">4</button></div>
  <div id="Five2"><button onclick="Five2()">5</button></div>
  <div id="Six2"><button onclick="Six2()">6</button></div>
  <div id="Seven2"><button onclick="Seven2()">7</button></div>
  <div id="Eight2"><button onclick="Eight2()">8</button></div>
  <div id="Nine2"><button onclick="Nine2()">9</button></div>
  <div id="Ok2"><button onclick="Ok2()">OK</button></div>
</head>

</html>

我的创作是一个基本的计算器。但是,每当SetNumber2函数通过(启动InitiateCalculation()函数的函数)时,所有变量AddVARSubtractVARMultiplyVARDiviseVAR即使其中的if语句本应使早先应用的条件(将按钮按下更改为true)也设置为true,也未更改。好像它们完全没有意义,因为在代码末尾,所有值都设置为true。

2 个答案:

答案 0 :(得分:3)

您正在true语句中将每个变量设置为if。您需要使用===来检查是否相等,而不是使用=(即赋值运算符)。

if (AddVAR === true) {
    Output = parseInt(EquationN1) + parseInt(EquationN2);
    console.log(Output);
}

您需要为每个if语句执行此操作。

可以使用==而不是===,但这通常是令人讨厌的,因为它对类型不敏感。

答案 1 :(得分:0)

在每个if语句中,您都使用赋值运算符而不是相等运算符

if (AddVAR = true) {更改为if (AddVAR == true) {if (AddVAR === true) {

,对于其他if条语句