我是一般的代码新手,我正在学习 javascript,几天前我做了一个“路径检查器”,只是为了看看我能用我目前的知识做什么,现在 1 周后我正在尝试改进它,更少的代码,更多可重用的函数,但我不知道为什么不能正确运行,单独的逻辑很好,但变量不起作用。
这是基本逻辑。
let moto1='';
function checkMoto1() {
if (myMotosInTheGarage === 0 && moto1 === '') {
openAlert(displaySorryAlert); //global function that works
} else if (myMotosInTheGarage === 0 && moto1 === 'out') {
moto1 = 'return'; //change status
myMotosInTheGarage++; //sum to a count
countMotos; //const of write the myMotosInTheGarage in a textContent
changeBackground(btn1, 'red'//global function that works
} else if (myMotosInTheGarage >= 0) {
if (moto1 === '') {
moto1 = 'out';
myMotosInTheGarage--;
countMotos;
changeBackground(btn1, 'green');
} else if (moto1 === 'out') {
moto1 = 'return';
myMotosInTheGarage++;
countMotos;
changeBackground(btn1, 'red');
}
}
}
我尝试将其用于全局功能。
let moto1='';
function checkMoto1() {
checkMotoGarage(moto1, btn1);
};
function checkMotoGarage(motonumber, btnnumber) {
if (myMotosInTheGarage === 0 && motonumber === '') {
openAlert(displaySorryAlert);
} else if (myMotosInTheGarage === 0 && motonumber === 'out') {
motonumber = 'return';
myMotosInTheGarage++;
countMotos;
changeBackground(btnnumber, 'red');
console.log(`the ${btnnumber} it's ${motonumber}`);
} else if (myMotosInTheGarage >= 0) {
if (motonumber === '') {
motonumber = 'out';
myMotosInTheGarage--;
countMotos;
changeBackground(btnnumber, 'green');
console.log(`the ${btnnumber} it's ${motonumber}`);
} else if (motonumber === 'out') {
motonumber = 'return';
myMotosInTheGarage++;
countMotos;
changeBackground(btnnumber, 'red');
console.log(`the ${btnnumber} it's ${motonumber}`);
}
}
};
moto 状态在全局函数中没有改变,这是为什么呢?我做错了什么?