Problem in Proof of $L^2$ approximation of a periodic function Question
Below there is the proof with two questions. Can someone explain these statements?

Suppose $f:[-\pi,\pi] \rightarrow \mathbb{R}$ Reimann integrable function and $\epsilon >0$. Then there exists a continuous function $g:[-\pi,\pi] \rightarrow \mathbb{R}$ with
$ \|g\|_{\infty}\leq\|f\|_{\infty}$
and $g(-\pi)=g(\pi)$ s.t. $\|f-g\|_2<\epsilon$.

Proof
We fix a $\delta>0$. We can find a partition $P=\{-\pi=x_0<x_1< \dots <x_N=\pi \}$ of $[-\pi,\pi]$ s.t. $U(f,P)-L(f,P)<\delta$.
Let $f'$ be the function:
$$ f'(x)=\sup_{x_{j-1}\leq y \leq x_j}f(y), \quad x \in [x_{j-1},x_j), \quad 1 \leq j \leq N$$.
By the definition of $f'$ we have $|f'| \leq \|f\|_{\infty}$. Also
$$\int_{-\pi}^{\pi}|f'(x)-f(x)|dx = \int_{-\pi}^{\pi}(f'(x)-f(x))dx<\delta $$
Because
$$\int_{-\pi}^{\pi}(f'(x)-f(x))dx = U(f.P)-\int_{-\pi}^{\pi}f(x)dx \leq U(f,P)-L(f,P)<\delta \quad \text{(Why)???} $$
Now we can "transform" the function $f'$ so we can have a continuous function $g$ with $g(-\pi)=g(\pi)$ that approximates $f$ with the $L^2$-norm.

Be patient.

For a small $h>0$ we set:
$$ g(x)=f'(x), \quad \text{if} \quad|x-x_j| \geq h \quad  \text{for} \quad j=0, \dots,N$$
Now in the $h$-neighborhood of $x_j$ for $j=1, \dots ,N-1$ we define g to be the linear function that satisfies:
$$g(x_j \pm h)=f'(x_j \pm h)$$
Near $x_0=-\pi$ we define g linear function as:
$$\begin{cases} g(-\pi+h)=f'(-\pi+h) \\
g(-\pi)=0 \end{cases}$$
And near $x_N=\pi$ we define g linear function as:
$$\begin{cases} g(\pi-h)=f'(\pi-h) \\
g(\pi)=0 \end{cases}$$
Then $g(-\pi)=g(\pi)$, so we can "expand" $g$ to be a continuous $2\pi$-periodic function in $\mathbb{R}$.

Also $|g| \leq \|f\|_{\infty}$

Remark: the function $g$ is different from $f'$ only in the $N+1$ intervals of $2h$-length or around the points $x_0, \dots ,x_N$. So,
$$\int_{-\pi}^{\pi}|f'(x)-g(x)|dx \leq 2N \cdot 2h$$
And for $h$ small enough, we take:
$$\int_{-\pi}^{\pi}|f'(x)-g(x)|dx \leq \delta \quad \text{(Why)???} $$
 A: The function $f'$ is constructed so that it is the supremum of $f$ on each interval of the partition. Thus
$$
\int_{-\pi}^\pi f'=U(f,P). 
$$
And since
$$
L(f,P)\leq \int_{-\pi}^\pi f,
$$
you get
$$
\int_{-\pi}^\pi f'-\int_{-\pi}^\pi f=U(f,P)-\int_{-\pi}^\pi f\leq U(f,P)-L(f,P). 
$$
As for your second question, $g$ was constructed to be equal to $f'$ with the exception of the intervals $(x_j-j,x_j+h)$. Then
\begin{align}
\int_{-\pi}^\pi |f'-g|&=\sum_{j=1}^N\int_{x_j-h}^{x_j+h}|f'-g|
\leq\sum_{j=1}^N\int_{x_j-h}^{x_j+h}|f'|+|g|\\[0.3cm]
&\leq\sum_{j=1}^N\int_{x_j-h}^{x_j+h}2\max\{|f'(x_j\pm h)|\}
\leq\sum_{j=1}^N\int_{x_j-h}^{x_j+h}2\|f\|_\infty\\[0.3cm]
&=\sum_{j=1}^N4h\,\|f\|_\infty
=4Nh\,\|f\|_\infty\\[0.3cm]
\end{align}
Finally, take any $h$ with
$$
0<h<\frac\delta{4N\|f\|_\infty}.
$$
A: Let $$g(x)={x_i-x\over x_i-x_{i-1}}f(x_{i-1})+{x-x_{i-1}\over x_i-x_{i-1}}f(x_{i}) \quad x_{i-1}\le x\le x_i
$$
Denote
$$m_i=\inf_{x_{i-1}\le x\le x_{i-1}}f(x),\quad M_i=\sup_{x_{i-1}\le x\le x_{i-1}}f(x)$$ and
$$C=\sup_{-\pi\le x\le \pi}f(x)-\inf_{-\pi\le x\le \pi}f(x)$$
Then $g$ is continuous on $[-\pi,\pi]$ and $m_i\le g(x)\le M_i$ for $x_{i-1}\le x\le x_i,$ due to linearity of $g$ on each interval $[x_{i-1},x_i].$ Moreover $g(x_i)=f(x_i)$
$$|g(x)-f(x)|\le M_i-m_i,\quad x_{i-1}\le x\le x_i$$
The last inequality follows from
$$m_i\le f(x)\le M_i,\quad m_i\le g(x)\le M_i,\quad x_{i-1}\le x\le x_i$$
Hence
$$\|f-g\|_2^2=\sum_{i=1}^n\int\limits_{x_{i-1}}^{x_i}|f(x)-g(x)|^2dx\le C\sum_{i=1}^n\int\limits_{x_{i-1}}^{x_i}|f(x)-g(x)|dx\\
\le  C\sum_{i=1}^n(M_i-m_i)[x_i-x_{i-1}]=C [U(f,P)-L(f,P)]$$
