Let $f : I \to \mathbb R$ be continuous. For any compact interval $J \subseteq f(I), \exists$ a compact interval $K \subseteq I$ with $f(K)=J.$ 
Let $f \colon I \to \mathbb R$ be continuous where $I$ is an interval. For any compact interval $J \subseteq f(I)$ there exists a compact interval $K \subseteq I$ such that $f(K)=J.$

My attempt:
Let $J=[f(x),f(y)]$ wehre $x,y \in I.$ Without loss of generality $x<y.$ Let $p=f(x), q=f(y).$
Let $$A=f^{-1}\{p\}\cap [x,y]$$ Then $A$ is closed and bounded, hence compact. Therefore, $r=\sup A \in A.$ Thus, $f(r)=f(x).$
Similarly, let $$B=f^{-1}\{q\}\cap [x,y]$$ Then $s=\inf B \in B$. Thus, $f(s)=f(y).$
Now I want to show that $f([r,s])=J.$ I understand intuitively and geometrically that if there is a point $w \in [r,s]$ such that $f(w)<f(x)$ Then we'll get a point in $(w,s)$ such that $f(w)=f(p).$ This will contradict the definition of $r.$ However, I'm not able to make this precise.

Is there a rigorous argument to show $f([r,s])=J?$

 A: We want to prove $f([r,s])=J$. Due to the intermediate value theorem, we have $$f([r,s])\supseteq [f(r),f(s)]=[f(x),f(y)]=J.$$
For the reverse inclusion, suppose $a\in[r,s]$ so that $f(a)\in f([r,s])$.
Suppose by contradiction that $f(a)\not\in J$.
Then we have two cases: $f(a)<f(x)$ or $f(a)>f(y)$. Without loss of generality, suppose we have $f(a)<f(x)$. Then $f(a) < f(x) < f(s)$, so by the intermediate value theorem, there is $b\in(a,s)$ such that $f(b)=f(x)$. Hence $b\in(a,s)\subseteq [r,s]\subseteq [x,y]$ contradicts the definition of $r$.
A: WLOG $p\leq q.$ (Otherwise  study the function $g(t)=-f(t)$...). We have $$f([r,s])\supset [f(r),f(s)]=[p,q]$$ by  the IVP, because $f$ is continuous. 
If $t\in (r,s)$ and $f(t)<p\leq q=f(s)$ then by the IVP there exists $t'\in (t,s)$ with $f(t')=p,$ contrary to the def'n of $r.$ 
If $t\in (r,s)$ and $f(t)>q\geq p=f(r)$ then by  the IVP there exists $t''\in (r,t)$ with $f(t'')=q$ contrary to the def'n of $s.$ 
So $f([r,s])\subset [p,q].$
A: 
Now I want to show that $f([r,s])=J.$ I understand intuitively and geometrically that if there is a point $w \in [r,s]$ such that $f(w)<f(x)$ Then we'll get a point in $(w,s)$ such that $f(w)=f(p).$
  This will contradict the definition of $r.$ However, I'm not able to make this precise.

If $f(y)=f(x)$ then $J=\{f(x)\}$ and we may put $K=\{x\}$. Otherwise the continuous image $f((w,s))$ of a connected set $w,s]$ is connected, $f(w)<f(x)$ and $f(s)=f(y)>f(x)$. Therefore $f((w,s))$ contains a point $f(x)$. Thus there exists a point $z\in [w,s]$ such that $f(z)=x$, but $z>w>r$ a contradiction. The remaining proof looks OK.
