Congratulations on finishing your Ruby Fundamentals lessons!
Now that you have a basic grasp on how Ruby works, let's try to apply it in one of the most common basic interview problems: The FooBar Problem. This is a real interview problem that is used to weed out applicants who don't have a good grasp on basic programming, so it's really important that you have the necessary skills to solve this problem.
1
up to the number that the user inputed3
, print out Foo
5
, print out Bar
15
, print out FooBar
This is how your problem should look when it is executed:
ruby foobar.rb
How many numbers do you want to see? 3
1
2
Foo
ruby foobar.rb
How many numbers do you want to see? 39
1
2
Foo
4
Bar
Foo
7
8
Foo
Bar
11
Foo
13
14
FooBar
16
17
Foo
19
Bar
Foo
22
23
Foo
Bar
26
Foo
28
29
FooBar
31
32
Foo
34
Bar
Foo
37
38
Foo
First, go to this website. Here, you can write ruby code and run the code inside the browser.
Next, write your code in the text editor:
Next, press "run", and check your output.
HINTS!
- When you get the user's input, make sure to convert the input into an
integer
, or else it will give you an error- Beware of the order that you check if the number is divisible by another number