I'm very new to coding, only learning HTML & CSS about a year ago and just learning javascript about a week ago. I decided to create a cookie clicker-type project in order to try and improve my JS skills and everything works fine except for one thing. These two if statements i have below are not executing together. I want to make them both execute so that if the banana count is equal to 10,000 then it should add a single farm and also change the html elements.
The reason they are two seperate if statements is that I had to add a second condition (!addFarm1) to check if a var is false then make it true so that it only adds the farm once instead of every click after 10,000 banana counts.
How i want it to work is that once banana count reaches 10000, a single farm is added to the farm count and ALSO changes the html elements to match the 'upgrade' then changes the interval. There is much more code but i didnt know if i should post it so i just posted the important parts but please ask if you need the rest of the code to fix it.
var addFarm1 = false;
function countByTwo(num1) {
for (let i = 0; i < num1; i ) {
bananaCount = 2;
}
return bananaCount;
}
function addFarm(farmAdd) {
farmCount = farmCount farmAdd;
farmCountText.textContent = farmCount;
farmIncrementText.classList.remove('hidden');
farmIncrement = farmCount * 5;
farmIncrementText.textContent = ' ' farmIncrement;
setInterval(function() {
bananaCount = bananaCount farmIncrement;
bananaCountText.textContent = bananaCount;
}, 1000);
}
function bananaClick() {
bananaCount = 1;
if (bananaCount > 10000) { // this one to change the clicker image, text, and interval
bananaClicked.textContent = 'Golden Banana Bunch ( 15)';
banana.src = 'images/banana6.png';
countByTwo(3);
}
bananaCountText.textContent = bananaCount;
}
if (bananaCount > 10000 && !addFarm1) { // and this one to add one farm
addFarm(1);
addFarm1 = true;
}
CodePudding user response:
You could do something like:
if (bananaCount > 10000) { // this one to change the clicker image, text, and interval
bananaClicked.textContent = 'Golden Banana Bunch ( 15)';
banana.src = 'images/banana6.png';
countByTwo(3);
if(!addFarm1) {
addFarm(1);
addFarm1 = true;
}
}
CodePudding user response:
Simply put both if
statements in the function.
function bananaClick() {
bananaCount = 1;
if (bananaCount > 10000) { // this one to change the clicker image, text, and interval
bananaClicked.textContent = 'Golden Banana Bunch ( 15)';
banana.src = 'images/banana6.png';
countByTwo(3);
}
if (bananaCount > 10000 && !addFarm1) { // and this one to add one farm
addFarm(1);
addFarm1 = true;
}
bananaCountText.textContent = bananaCount;
}