[JavaScript] Troubles with 'While' Loops


#1

I have been working to practice with JavaScript and ran into an error when I tried to insert a second while loop. I seem to receive an error saying that the loop is potentially infinite even though all the math seems to work correctly. Any ideas why that error is being given?

console.clear(0)
let statement = 'C:\\'
let dir = ['C:\\']
while (VarA = '', VarA.length === 0, VarA = prompt(statement)) { 
  statement = statement + VarA + '\\' 
  if (statement[statement.length -1] === '+') {
    
  }
  else {
    if (statement[statement.length -1] === '-') {
      
    }
    else{
      if (VarA.includes('cd\\')) {
        let VarB = ''
        while (CounterA = 2+VarA.indexOf('cd\\'), CounterA === VarA.length, CounterA++) {
          VarB = VarB + VarA[CounterA]
        }
        if (VarB[VarB.length+1] === '\\' || VarB.length !== 0) {
          statement = 'C:\\' + VarB
        }
        else {
          statement = 'C:\\' + VarB + '\\'
        }
      }
      else {
        
      }
    }
  }
}

#2

With that much code in the condition it’s not surprising if it’s difficult to reason about.
You can stuff a whole program in there, doesn’t mean you should. Do the opposite, put the minimal amount required in your condition.

There’s also a large amount of paths execution can take, which also makes the code difficult to reason about. If you can phrase what you’re doing as sequential steps instead of nesting, that’s far preferable. Less control structures would also help. If you can describe an action with a name, then you can break that out into a function - making both pieces easier to reason about individually.

The code could also be closer to English, or at least have comments explaining intentions where that isn’t possible.
I don’t see what’s going on unless I read everything that has already happened (which is quite the mental exercise)


#3

Sorry about it being so bulky. The real trouble I have is within four-ish lines. Notebook that ‘VarA’ has already been defined as a string.

let VarB = '' \\Defines 'VarB' as nothing. 
        while (CounterA = 2+VarA.indexOf('cd\\'), CounterA === VarA.length, CounterA++) { \\The error seems to appear where 'CounterA' is defined. This loop is started after the code ensures 'VarA' contains 'cd\\.' '2+VarA.indexOf('cd\\')' is worth the last character of 'cd\\' where it is found within 'Var.' Every time the loop is cycled: 'CounterA' by one. The loop stops when 'CounterA' reaches the length of 'VarA.' 
          VarB = VarB + VarA[CounterA] \\ When each cycle is ran, VarB is extended now including the next character of 'VarA.' 
        } \\The goal of the loop is to rewrite 'VarA' excluding the string 'cd\\.' 

#4

Didn’t mean that you need to comment/explain it for me.
I mean that if you can’t look at a small piece of code and say “yeah, that is correct” then it should probably be written some other way.
You wouldn’t have this problem if your code was kept simple, avoiding deep nesting, avoiding lots of control structures (made more complicated yet when nested within each other), and avoiding having so much logic all in the same place. I wouldn’t entertain the idea of fixing that, my answer will therefore be about how it should be written differently.
The first and most obvious offender being the amount of code in your condition. I expect something like this:

while (notDone) {
}

Not:

while (fire the nuclear missiles, cook the fish, solve these three sudoku puzzles, feed the dog, still purple?) {
}

If you mean to remove a substring, I’m sure there’s some string substitution function/method that already does this, it would also be an excellent unit of code to break out into its own function if you defined it yourself - for the purpose of not having so many details all in one place it’s impossible to figure out what is going on

I might also point out that you’re populating the while-statement’s condition as if it were a for-statement. It’s not. But the resemblance is so strong that I’m fairly sure you expect a while-statement to behave that way. (And it’s still far too much code even if it was)


#6

Hmm… I ended up using the substring element after all however I am still wondering why I have the error at all. Anyways, thank you for the advice. I think I see what you mean.


#7

I haven’t gotten as far as considering the error. I don’t know what it is, what is emitting it, or how to reproduce it. And since you use the wording “may”, it probably isn’t an error at all, but a warning.

while-statements are very simple. You made them complicated by putting lots of code in them. And I’m pretty sure you think they behave like for-statements. So look up how they do behave, and then read your code. It’s also rather trivial to test for it.

It’s easy to write code that is difficult to reason about.
That’s to say that you need to reach the point where you’re writing sufficiently simple code that you’re able to reason about, and then you keep going, continuing to write simpler code while increasing your ability to reason about it. That’s when you can take on more difficult problems.

The problem you are solving/describing isn’t difficult, but you’ve written difficult code for it. The problem is entirely within your abilities so long as you keep code complexity to a minimum.