Why are my else if statements not working

Why are these not working the code works on the first if statement then stops working with no error messages in the console.

let normalTreeChopped = 0
let patch1 = document.getElementById("patch-image-1")
let patch1Src = patch1.getAttribute('src')

let patch2 = document.getElementById("patch-image-2")
let patch2Src = patch2.getAttribute('src')

let patch3 = document.getElementById("patch-image-3")
let patch3Src = patch3.getAttribute('src')

let nonePath = 'assets/img/woodcutting/trees/none.png'

function startTreeGrow() {
	if (woodLevel >= 1) {
		let normalTreeChance = Math.floor(Math.random() * 45)
		if (normalTreeChance == 33) {
			if (patch1Src == nonePath) {
				document.getElementById("treeName1").innerHTML = "Tree"
				patch1.src = 'assets/img/woodcutting/trees/tree/tree1.png'
			} else if (patch2Src == nonePath) {
				document.getElementById("treeName2").innerHTML = "Tree"
				patch2.src = 'assets/img/woodcutting/trees/tree/tree1.png'
			} else if (patch3Src == nonePath) {
				document.getElementById("treeName3").innerHTML = "Tree"
				patch3.src = 'assets/img/woodcutting/trees/tree/tree1.png'
			} else {

setInterval(startTreeGrow, 1000)

hi there, is this an fCC challenge? Can you post a link to it?

No it’s not. Its just something I’m stuck on.

maybe you can try adding some log statements?

for eg. if this if is not executing then I would add a console.log right above it
console.log("normalTreeChance = " + normalTreeChance);

Eventually you will find a log that is unexpected and can work from there.

yeah, the else ifs are broken

can you log patch1Src patch2Src etc?

patch1 says tree1 as src because The program was running for an while if it was not it would’ve been none.png

Here is my HTML if you’re curious

            <div id="woodcut" class="tabcontents">
                <h1 style="text-align: center;" class="titleSkill">Woodcutting</h1>
                <h3 id="levelWoodcut" style="text-align: center;">Woodcutting Level: 1</h3>
                <div class="grass">
                        <span class="path-margin desc-patch"><p id="treeName1" class="treeNAME">None</p><p id="timeleft1tree">(00:00:00)</p></span>
                        <img class="patchO-grass" id="patch-image-1" src="assets/img/woodcutting/trees/none.png">
                        <span class="path-margin desc-patch"><p id="treeName2" class="treeNAME">None</p><p id="timeleft1tree">(00:00:00)</p></span>
                       <img class="patchO-grass" id="patch-image-2" src="assets/img/woodcutting/trees/none.png">
                        <span class="path-margin desc-patch"><p id="treeName1" class="treeNAME">None</p><p id="timeleft1tree">(00:00:00)</p></span>
                       <img class="patchO-grass" id="patch-image-3" src="assets/img/woodcutting/trees/none.png">

(sorry of the formatting) don’t mind the time left

1 Like

what if you do this

console.log(patch1Src == nonePath);
console.log(patch1Src === nonePath);
1 Like

so true means that the if statement did fire.

Can you add a console.log inside this if? We know it is running, but maybe you are just not seeing the results?

the if staement works fine its just the else if statements that don’t work
Here is a visual

The nones are supposed to be trees if the else if did not work

Sorry, i keep misreading you.
Can you do the above for patch2Src and patch3Src then?

true and true

can you log them all at the same time?
including patch1Src first, then patch2Src etc.
I want to see what they are simultaneously

Is this what you want?

That’s weird because the tree started growing in the first patch it’s supposed to be assets/img/woodcutting/trees/tree/tree1.png right now it displays assets/img/woodcutting/trees/tree/tree1.png but the console says it’s not.

yes so you can see that they are all true
this means the first if will fire which is why no else will fire after that

Red the top I juts updated it. lemme try changing patch1.src to patch1Src when I change the image to a tree

so it works now but the images are nto showing up Ill fix that onmyown in the meantime I found some more naming errors like treeName3 was not even there

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.