Define g(x) as a function of $x$ Let $f(x)= \begin{cases} 
      -1 & ,-2\leq x\leq 0 \\
       \\
      |x-1| & ,0<x\leq2 
   \end{cases}$
and $g(x)=\int_{-2}^{x}f(t) dt$.Define g(x) as a function of $x$.
I tried to solve it.I redefined $f(x)= \begin{cases} 
      -1 & ,-2\leq x\leq 0 \\
      1-x & ,0<x<1      \\
      x-1 & ,1\leq x\leq2
   \end{cases}$
$g(x)=\int_{-2}^{x}f(t) dt=\begin{cases} 
      \int_{-2}^{x}-1dx & ,-2\leq x\leq 0 \\
      \int_{-2}^{x}(1-x)dx & ,0<x<1      \\
      \int_{-2}^{x}(x-1)dx & ,1\leq x\leq2
   \end{cases}$
$g(x)= \begin{cases} 
      -(x+2) & ,-2\leq x\leq 0 \\
      x-\frac{x^2}{2}+4 & ,0<x<1      \\
      \frac{x^2}{2}-x-4 & ,1\leq x\leq2
   \end{cases}$
But the answer is given to be
$g(x)= \begin{cases} 
      -(x+2) & ,-2\leq x\leq 0 \\
      x-\frac{x^2}{2}-2 & ,0<x<1      \\
      \frac{x^2}{2}-x-1 & ,1\leq x\leq2
   \end{cases}$
Have i done it wrong,what should be the correct method.Please guide me. 

For $0<x<1$ we have
\begin{align*}
g(x)&=\int_{-2}^{x}f(t)dt\\
&=\int_{-2}^0f(t)dt+\int_0^xf(t)dt\\
&=\int_{-2}^0(-1)dt+\int_0^x(1-t)dt\\
&=(-1)(0+2)+x-\frac{1}{2}x^2-0\\
g(x)&=-\frac{1}{2}x^2+x-2,\qquad \text{ for }\;\;0<x<1
\end{align*}
On the other hand, for $1\le x\le 2$ it follows
\begin{align*}
g(x)&=\int_{-2}^{x}f(t)dt\\
&=\int_{-2}^0f(t)dt+\int_0^1f(t)dt+\int_1^xf(t)dt\\
&=\int_{-2}^0(-1)dt+\int_0^1(1-t)dt+\int_1^x(t-1)dt\\
&=(-1)(0+2)+1-\frac{1}{2}(1)^2-0+\frac{1}{2}x^2-x-\left[\frac{1}{2}-1\right]\\
g(x)&=\frac{1}{2}x^2-x-1,\qquad \text{ for }\;\;1\le x\le 2
\end{align*}
Thus
\begin{equation*}
\color{blue}{g(x)=
\begin{cases}
-(x+2)&-2\le x\le 0\\
-\frac{1}{2}x^2+x-2& 0<x<1\\
\frac{1}{2}x^2-x-1& 1\le x\le 2
\end{cases}
}
\end{equation*}
