problem in for loop, loops keeps waiting for the final input. Given input n, but returns result for n-1