Having issue with Ruby while loop

We have written code as -

def looping(variable1)
    j = 0
    num = variable1
    while j < num  do
        j +=1;
        num1 = j
        return num1
    end
end

Input (stdin)
*** 7**

Your Output (stdout)
*** 7**

Expected Output
*** [0, 1, 2, 3, 4, 5, 6]**

Please let us know where is the issue

Your code as posted does not return what you have shown. As posted, the code returns 1.

As your “Expected output” looks like an array, you need to set up an array to fill and then return it at the end.
You have also made a mistake by putting the return inside the while loop. You also do not need so many variables, e.g. num and num1.

Start with something like this:

def looping(variable1)
  result = []  # this creates an array to store the final result in

  # -- your loop to fill the result array

  return result # this returns the array of values
end

Now complete “your loop” to fill the result array with the values you want.

It looks like you need to put the numbers less than variable1 into the result array, so the while loop should be:

j = 0                   # start value for loop is 0
while j < variable1 do  # repeat loop until j == variable1
  # put current value of j into result
  j += 1                # make sure we increment j each loop
end

and the instruction to add a value to the right side of an array is push

So putting it all together gives:

def looping(variable1)
  result = []  

  j = 0
  while j < variable1 do
    result.push(j) 
    j += 1
  end

  return result 
end
```

thanks a lot for quick help

we have 2 more issue with Ruby Array funtion…

  1. Need to find odd number between 2 numbers
  2. Fnd number between 2 numbers in array divisible by 3 and 5
def find_odd(first, last)
    #Write your code here and return the result
    result=[]
    last.step first, -2 do |x|
        result.push "#{x}"
    end
    return result
end
def divisiblity_test(variable1, variable2)
    #Write your code here
    result = []
    for range(variable1, variable2)
   
    return result
end

In same content need hep in these 2 code also