Javascript 如何比较这两个变量?

Javascript 如何比较这两个变量?,javascript,html,css,Javascript,Html,Css,我是编程新手。我想用JS编写一些记分员的代码。 如何比较两个变量:pointOne与maxScore 我想要这样的东西: 如果pointOne===maxScore->Player1获胜,游戏结束。 目前,这部分代码无法比较此变量 这是我的代码: const maxInput=document.querySelector(“#number”); maxInput.addEventListener('input',function(){ 设maxScore=number.value; maxSc

我是编程新手。我想用JS编写一些记分员的代码。 如何比较两个变量:
pointOne
maxScore

我想要这样的东西: 如果
pointOne===maxScore->Player1
获胜,游戏结束。 目前,这部分代码无法比较此变量

这是我的代码:

const maxInput=document.querySelector(“#number”);
maxInput.addEventListener('input',function(){
设maxScore=number.value;
maxScore=parseInt(此.value);
log(`Max score是${maxScore}`);
//如果(点一===3){
//console.log(“妈咪3!”);
// }
})
函数headerUpdate(){
const header=document.querySelector('h1');
header.innerText=`${pointOne}到${pointTwo}`;
}
设pointOne=0;
设点2=0;
函数addPoints(){
const buttonClickTrue=document.getElementById('btn_one');
const buttonClickTrue2=document.getElementById('btn_two');
const resetButtonTrue=document.getElementById('btn_reset');
buttonClickTrue.addEventListener('单击',函数()){
pointOne++;
log(`你的分数是${pointOne}`);
头更新();
})
buttonClickTrue2.addEventListener('单击',函数()){
点2++;
log(`secondscore是${pointTwo}`);
头更新();
})
resetButtonTrue.addEventListener('单击',函数()){
window.location.reload(true);
})
}
addPoints()
*{
框大小:边框框;
保证金:2倍;
填充:0;
背景色:#7c86ac;
}
.游戏盒{
身高:30%;
显示器:flex;
对齐项目:居中;
弯曲方向:立柱;
}
#泰尼斯{
宽度:30%;
边缘顶部:30px;
}
#btn_一{
背景颜色:绿色;
边框:3倍纯黑;
宽度:30%;
}
#btn_二{
背景颜色:橙色;
边框:3倍纯黑;
宽度:30%;
}
#btn_复位{
背景色:红色;
边框:3倍纯黑;
宽度:30%;
}
#数{
背景色:白色;
边框:3倍纯黑;
宽度:5%;
}

记分员
0比0
使用下面的按钮进行计数

玩游戏

+一人一人 +1人2人 重置
我已修改了您的代码。使用
var
全局声明
pointOne
pointTwo
变量,以便可以在代码中的任何位置访问它们。我对
maxScore
也做了同样的操作

现在我把if条件放在里面,单击事件监听器中的
playeron
按钮。这很管用

const maxInput=document.querySelector(“#number”);
设pointOne=0;
设点2=0;
设maxScore=0;
maxInput.addEventListener('input',function(){
maxScore=maxInput.value;
maxScore=parseInt(maxScore);
log(`Max score是${maxScore}`);
})
函数headerUpdate(){
const header=document.querySelector('h1');
header.innerText=`${pointOne}到${pointTwo}`;
}
函数addPoints(){
const buttonClickTrue=document.getElementById('btn_one');
const buttonClickTrue2=document.getElementById('btn_two');
const resetButtonTrue=document.getElementById('btn_reset');
buttonClickTrue.addEventListener('单击',函数()){
pointOne++;
log(`你的分数是${pointOne}`);
头更新();
如果(pointOne==maxScore){
console.log(“妈咪3!”);
}
})
buttonClickTrue2.addEventListener('单击',函数()){
点2++;
log(`secondscore是${pointTwo}`);
头更新();
})
resetButtonTrue.addEventListener('单击',函数()){
window.location.reload(true);
})
}
addPoints()
*{
框大小:边框框;
保证金:2倍;
填充:0;
背景色:#7c86ac;
}
.游戏盒{
身高:30%;
显示器:flex;
对齐项目:居中;
弯曲方向:立柱;
}
#泰尼斯{
宽度:30%;
边缘顶部:30px;
}
#btn_一{
背景颜色:绿色;
边框:3倍纯黑;
宽度:30%;
}
#btn_二{
背景颜色:橙色;
边框:3倍纯黑;
宽度:30%;
}
#btn_复位{
背景色:红色;
边框:3倍纯黑;
宽度:30%;
}
#数{
背景色:白色;
边框:3倍纯黑;
宽度:5%;
}

记分员
记分员
0比0
使用下面的按钮进行计数

玩游戏

+一人一人 +1人2人 重置
鉴于上述公认的答案,我认为这是一个糟糕的做法,尤其是因为他使用var关键字并填充全局名称空间。阅读下面的另一个Stackoverflow答案,了解为什么使用全局变量是错误的()。为什么不使用ES6的const或let呢。下面的代码使用的是事件总线体系结构,因此避免了不必要的复杂性,并将代码保留在全局命名空间之外:

//事件总线体系结构
window.addEventListener('click',Button.bind(this));
window.addEventListener('input',Max.bind(this));
功能按钮(事件){
常量p1=document.getElementById(“p1”);
constp2=document.getElementById(“p2”);
事件。路径[0]。id==“btn_one”?A(p1,p2):空;
事件。路径[0]。id==“btn_two”?B(p1,p2):空;
事件。路径[0]。id==“btn_重置”?重置():null;
}
功能A(p1、p2){
p1.innerText++;
log(`A score是${p1.innerText}`);
头部更新(p1,p2);
}
功能B(p1、p2){
p2.innerText++;
log(`B分数为${p2.innerText}`);
头部更新(p1,p2);
}
函数重置(){
window.location.reload(true);
}
最大功能(事件){
事件。路径[0]。id==“编号”?MaxValue():null;
}
函数MaxValue(){
常量编号=document.getElementById(“编号”);
设maxScore=number.value;
maxScore=parseInt(maxScore);
log(`Max score是${maxScore}`);
}
功能头更新(p1、p2){
p1.innerText=p1.innerText;
p2.innerText=p2.innerText;
常量编号=document.getElementById(“编号”);
设maxScore=number.value;
maxScore=+maxScore;
警察