<s>[INST] write an explicit formula for $f(x)$ when $x$ is in $[\frac{1}{n+1},\frac{1}{n}]$ 

this is from spivak calculus ,i tried to find an explicit formula when $x$ is in $[\frac{1}{n+1},\frac{1}{n}]$
my attempt:
in $[\frac{1}{n+1},\frac{1}{n}]$ the function $f$ is a linear function so we can write it as $f(x)=\alpha x+ \beta$
-finding $\alpha$
we can see that the function equal $0$ when $x$ exists in the midle of $\frac{1}{n}$ and $\frac{1}{n+1}$,because  we can imagine the function graph in $[\frac{1}{n+1},\frac{1}{n}]$ as a diameter of  a rectangle with length $2$ and width $\frac{1}{n}-\frac{1}{n+1}$,so here the  both diameters of our rectangle have to intersect in the middle ($\frac{\frac{1}{n}-\frac{1}{n+1}}{2}=\frac{1}{2n(n+1)}$) so that's means each of the diameters have to pass from the middle of the rectangle which is in our case the middle point in $[\frac{1}{n+1},\frac{1}{n}]$ in the horizontal axis  ,and that's means $f(x)=0$,when $x=\frac{1}{n+1}+\frac{1}{2n(n+1)}=\frac{2n+1}{2n(n+1)}$(for understand my idea , you can draw a rectangle in the graph from $\frac{1}{3}$ to $\frac{1}{2}$)
so $f(\frac{2n+1}{2n(n+1)})=\alpha \frac{2n+1}{2n(n+1)}+ \beta=0$
$\Rightarrow$$\alpha=-\beta .\frac{2n(n+1)}{2n+1}$
so $f(x)=-\beta .\frac{2n(n+1)}{2n+1}x+ \beta$
-finding $\beta $
we know that $f(x)=-\beta .\frac{2n(n+1)}{2n+1}x+ \beta$ and $ f(\frac{1}{n})=(-1)^{n+1}$
so $f(\frac{1}{n})=-\beta .\frac{2(n+1)}{2n+1} +\beta =(-1)^{n+1}$
$\Rightarrow $$\beta(\frac{1+2n -2n -2}{1+2n})=(-1)^{n+1}$$\Rightarrow $$\beta=(-1)^{n+2}.(1+2n)$
so $f_n(x)=-(-1)^{n+2}.2n(n+1)x+(-1)^{n+2}.(1+2n)$ when $x$ is in $[\frac{1}{n+1},\frac{1}{n}]$
finally we can add this :
$f_n(x) =
  \begin{cases}
    1 &  |x| \geq 1 \\
    -(-1)^{n}.2n(n+1)x+(-1)^{n}.(1+2n) & x \in [\frac{1}{n+1},\frac{1}{n}] \\
   &
  \end{cases}$
-so does my attempt  is right ?.
-can you find a better formula for $f$?.
 [/INST] Your attempt is right except $f$ instead of $f_{\color{red}n}$ and $x>0$(just say the function is even). I will make a few remarks in addition to the remarks made in comments :
Let $a,b\in\Bbb R$: the middle of $[a,b]$ is $\frac12(a+b)$
The middle of $[\frac{1}{n+1},\frac1n]$ is $\frac{\frac{1}{n}+\frac{1}{n+1}}{2}=\frac{2n+1}{2n(n+1)}$(you could have gone faster that way)
Anyway, for $a\neq b, \color{red}{\text{the}}$ equation $y=\alpha x+\beta$ ("linear function") of the line going through $(a,A)$ and $(b,B)$ is $y-A=(x-a)\frac{B-A}{b-a}$
Here, with $(a,A)=(\frac{1}{n+1},(-1)^{n})$ and $(b,B)=(\frac{1}{n},(-1)^{n-1})$
$$\forall n>0, \forall x\in [\frac{1}{n+1},\frac1n], f(x)$$$$=(-1)^n+(x-\frac{1}{n+1})\frac{(-1)^{n-1}-(-1)^n}{\frac{1}{n}-\frac{1}{n+1}}$$$$=(-1)^n+2(-1)^{n-1}n(n+1)(x-\frac{1}{n+1})$$
$$=2(-1)^{n-1}n(n+1)x-2(-1)^{n-1}n+(-1)^n$$
$$=2(-1)^{n-1}n(n+1)x+(-1)^n(1+2n)$$
As Spivak said, "good exercise".
</s>