如何在此代码中正确添加括号

Moh*_*mad 3 javascript readability semantics

这段代码修剪了空白,(fyi:它被认为非常快)

function wSpaceTrim(s){
    var start = -1,
    end = s.length;
    while (s.charCodeAt(--end) < 33 );  //here
    while (s.charCodeAt(++start) < 33 );  //here also 
    return s.slice( start, end + 1 );
}
Run Code Online (Sandbox Code Playgroud)

while循环没有括号,我如何正确地为此代码添加括号?

while(iMean){
  // like this;
}
Run Code Online (Sandbox Code Playgroud)

非常感谢!

Dir*_*mar 7

循环体是空的(实际发生的事情是循环条件中的递增/递减操作),所以只需添加{}:

while (s.charCodeAt(--end) < 33 ){}
while (s.charCodeAt(++start) < 33 ){}
Run Code Online (Sandbox Code Playgroud)

同一个while循环的更长,可能更容易阅读的版本将是:

end = end - 1;
while (s.charCodeAt(end) < 33 )
{
    end = end - 1;
}
start = start + 1;
while (s.charCodeAt(start) < 33 )
{
    start = start + 1;
}
Run Code Online (Sandbox Code Playgroud)