Second example in (L^2)[-π,π]

f[x_]=Piecewise[{{-1,-Pi<x<0},{1,0<x<Pi}}]

Plot[f[x],{x,-Pi,Pi},
Ticks->{Range[-Pi,Pi,Pi/2],Range[-1,1]}]

fdotv=Integrate[f[x] Sin[k x],{x,-Pi,Pi}]

fdotv=Simplify[fdotv,Assumptions->Element[k,Integers]]

vdotv=Integrate[ Sin[k x]^2,{x,-Pi,Pi}]

vdotv=Simplify[vdotv,Assumptions->Element[k,Integers]]

a[k_]=fdotv/vdotv

a[1]

a[2]

a[3]

Table[a[k],{k,1,9}]

Manipulate[
Plot[{f[x],Sum[a[k]Sin[k x],{k,1,n}]},{x,-Pi,Pi},
PlotRange->{-1.5,1.5},
 ImageSize->Large],{n,1,201,2}]

Manipulate[
Plot[{f[x],Sum[a[k]Sin[k x],{k,1,n}],(f[x]-Sum[a[k]Sin[k x],{k,1,n}])^2},{x,-Pi,Pi},
PlotRange->{-1.5,1.5},
 ImageSize->Large],{n,1,201,2}]