I'm new in code in general, I'm learning javascript, some days ago I make a "Path Checker", just for look what Can I do with my current knowledge, now 1 week later I'm try to improve it, less code, more reusable functions, but I don't know why Don't run correctly, the logic it's good alone, but with the variables dont' work.
This is the basic logic.
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');
}
}
}
And I try this to a global function.
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}`);
}
}
};
The moto status don't change in the global function, Why is that? What I did wrong?.
I got it, you need to return a value, not a variable, and then change the variable with the new value outside the reusable function. I badly think you Can somehow change the value inside the reusable function, that whats my question, becouse in this case, you only Can have just one value of this function every time?, how to change the value inside instead? It's possible?