Do ... while ... throws 'Unexpected token '{' when line break inside

Hi everyone,

Maybe I’m very tired and have a a dummy question but I’m in trouble with a simple do … while … statement… The following one:

  var number;

  do{
    
    number = random(1, 101);
    print(number);
    
  } while (number < 80);

throws the following exception message: Uncaught SyntaxError: Unexpected token ‘{’

I’ve searched for a long time before to find a solution. But I don’t understand it. If we delete the line breaks after and before the brackets, the problem is solved…

  do{
    number = random(1, 101);
    print(number);  
  } while (number < 80);

Any idea what is the reason of this rigidity? What do I wrong when inserting line breaks inside the do statement?

Thanks a lot for your insights!

Laurent

1 Like

Did you copy and paste the code from somewhere else e.g. a web page?

There are several ways to code line breaks and carriage returns and some are not compatible with Javascript. Unfortunately they are all non-printable character sequences so you can’t see the difference

2 Likes

Thanks @quark for your quick answer and relevant insight. Yes, it could be a good explanation, but unfortunately I didn’t copy/paste this code from another source or editor. I wrote it from the scratch into a new sketch in editor.p5js.org. Following your idea, the problem should be solved with deleting the space inside the editor and inserting new ones. But this doesn’t change anything (I’ve tried it many times without success…). Could it be an editor.p5js.org bug?

've tried once again replacing those line breaks and now it works. Maybe I did unconsciously a small copy/paste. Thanks again @quark

It has happened to me in the past when copying and pasting. Sometimes you et some weird character which makes it obvious but most of the time you see nothing and end up scratching your head. :grinning:

1 Like