Ask coding questions

← Back to all posts
3
I am trying to fix my solar panels but I need help to continue (javascript)
javakid999 (2)

Hello. I am trying to work on a mars colonization game but until this is fixed I don't think that I can continue. I want to make it so that the numberOfMachines variable to be less than or equal to the number of solar panels, or else the machines will not work, because of power shortage. Here is the link to the repl.it project https://repl.it/@javakid999/Colonize-Mars

Thanks in advance, Javakid999

Answered by JustARatherRidi (185) [earned 5 cycles]
View Answer
Commentshotnewtop
2
JustARatherRidi (185)

Hey, that does make sense, but the only thing is that the machines don't work even when there are enough solar panels, for a couple of reasons:

  • First off, in init.js, line 33, you say var numberOfMachines = farms + waterPumps + habZones;, which won't work because you are creating a new local variable each time updateUI is called. So when you check for it in updateResources, its value is always 2 because you never updated the global variable. So what you really want to do is update the global variable (not create a new local one), by removing the var like so
numberOfMachines = farms + waterPumps + habZones;
  • Also, in script.js, line 54, you're checking for numberOfMachines == solarPanels when you really should be checking for numberOfMachines <= solarPanels, because it should also work when there are more solar panels than machines.

Change those two things, and you're good to go.

2
heyitsmarcus (285)

@JustARatherRidi There is only one problem with your answer, JustARatherRidi. In the last bullet point, you mention that they should be checking numberOfMachines <== solarPanels but this will result in an Unexpected token error because of the extra = sign at the end. It needs to be numberOfMachines <= solarPanels. Otherwise, it looks like a great answer!

2
JustARatherRidi (185)

@heyitsmarcus Apparently, I assumed there is a <== since there are === and !==, but now I realize that wouldn't really be of much use :P

Thanks for pointing it out!

1
heyitsmarcus (285)

@JustARatherRidi Honestly, it would be nice for a strict version of <= but perhaps that will come in a later version of ECMAScript! haha Cheers!

2
JustARatherRidi (185)

@heyitsmarcus That's what i thought at first too, but think about it.

For example, if x >== 100 is false, then i can't infer from it whether its false because of the value or the type. I think that sort of thing might actually get confusing because people might falsely assume things.

Or maybe they just can't be bothered, idk :P

1
heyitsmarcus (285)

@JustARatherRidi Ah, that is an interesting take on it and quite possibly why it was never implemented. Introducing falsey-ness to equality is just plain craziness! XD

1
javakid999 (2)

@JustARatherRidi Thank you guys so much! I am not new to js, but I am pretty terrible at it, so thank you for helping me!