Question: ```
function func(str) {
        var a = "" , c;
        var b = 0;

        for(var i = 0; i < str.length; i++)
        {
            c = str.charAt(i);

            if((str[i] != ' ' && str[i] != '\n' && str[i] != '\t') || b > 0)
            {
        a += c;
        b++;
    }
return a;    
}

```

So far the function I have removes the whitespace from the left side but I cant figure out for the remove the whitespace from the right side. Anyone got any suggestions.

Answer: 
I think it's cleanest if you don't try to do it all in one loop. I'd use a loop to find the start, to find the end, and to copy the part of the string you want. You're still pretty much just iterating once, but you have each task separate from the others.

```js
function isSpace(c) {
  return c == ' ' || c == '\n' || c == '\t'
}

function slice(str, from, to) {
  var start = from >= 0 ? from : 0;
  var end = to <= str.length ? to : str.length;

  var a = "";
  while (start < end) a += str[start++];
  return a;
}

function trim(str) {
  var first = 0, last = str.length-1;

  while (isSpace(str[first])) first++; // find trimmed start
  while (isSpace(str[last])) last--;   // find trimmed end

  return slice(str, first, last+1); // create the desired substring
}

console.log(JSON.stringify(trim("  foo bar  ")));
```
Use the same logic you already have to do the end of the string, but just loop starting at the end and change how you build 'a':

```
function reverseFunc(str) {
    var a = "" , c;
    var b = 0;
    for(var i = str.length-1; i >=0; i--)
        {
            c = str.charAt(i);

            if((str[i] != ' ' && str[i] != '\n' && str[i] != '\t') || b > 0)
            {
                a = c + a;
                b++;
            }
        }
    return a;
}

```