8/19 Practise makes perfect


#1

Hello everyone, first time posting here. I'm basically a little bit confused in this task.

This is my current code:

def greeting(name)
return 'Hello' *name
end
def by_three(n)
return true
if
n % 3 == 0
else
return false
end
end

by_three 3
greeting world

The by_three def works just fine as I have run that code and it always returns true or false. I cannot seem to get the greeting def to work regardless of which combination things I try. The error that returns is either "Undefined local variable world" or "Exepecting tINTERGER, cannot convert string to interger.

I keep getting frustrated with these tutorials, I know they are vague so that you have to work things out for yourself, but for someone with no wider knowledge of coding to draw upon it's almost impossible. Most of the stages are doable but then every 1 out of 5 I'm suddenly asked something that is impossible to work out, even with going back to previous codes to have a look. The only way through those ones is to find answers on the forums. I really try to read through the code to understand why it works and why it doesn't but it's often impossible to understand why something works or doesn't.


#2

@systemrunner92169,

def greeting(name)
 # added a new-line \n
 # =return= statement return's the result
 # with NoDisplay
 # and directly EXIT the FUNCTION
 return 'Hello' +name+"\n" 
end

def by_three(n)
  #return true
  if n % 3 == 0
    return true
  else
    return false
  end
end

#prepend the FUNCTION call with a =print= statement
#to Display the RETURNed Result.

#calling the greeting() function
#providing 1 ARGUMENT
print greeting("Emma")

#calling the greeting() function
#providing 1 ARGUMENT
print  by_three(9)

#3

So I have read through your explanation and I have amended my code to this:

def greeting(name)
return 'Hello' +name+ "\n"
end

def by_three(n)
return true
if
n % 3 == 0
else
return false
end
end

print by_three 3
print greeting("Emma")

It now returns as:
trueHelloEmma

But also wont let me continue. It asks if I have defined my Greeter method, which I have.
I really don't know what I'm doing wrong, or if there is a bug here.


#4

@systemrunner92169
Read the Instructions once more.....

Define two methods in the editor:
01. A greeter method 
      that takes a single string parameter, name, 
      and returns a string greeting that person. 
      (Make sure to use return and do not use print or puts.)
02. A by_three? method 
      that takes a single integer parameter, number,
      and returns true if that number is evenly divisible by three 
      and false if not.

As a NOTE

It's a Ruby best practice 
    to end method names that produce boolean values with a question mark.

What is the method-name to greet a person ??
Where are you placing your return true ??
How to you call / execute a method ???


#5

Hello, I have amended my code again and it now works however I would just like some clarification on something.

This is my current code:

def greeter(name)
return 'Hello' +name+ "\n"
end

def by_three?(n)
if
n % 3 == 0
return true
else
return false
end
end

greeter("Emma")
by_three? 3

It returns as:
true

And then it is accepted and I can move on to the next thing. However what I'm confused about is that it doesn't print anything for the greeter method. If I do:

print greeter("Emma")

Then it prints but the code is incorrect.

What I want to know is why does it print a result for the by_three method but not for the greeter method

I also would like to know where the \n came from. This was not introduced in any of the tutorial so far. And what is its purpose?


#6

@systemrunner92169,
I tried your code a got a print-out

def greeter(name)
 return 'Hello ' +name+ "\n" 
end

def by_three?(n)
 if n % 3 == 0
   return true
 else
   return false
 end
end

print greeter("Emma") 
print by_three?(3)

About "\n"
https://www.google.nl/?gfe_rd=cr&ei=aU3zVsXhINfFaIWqm-gO#q=ruby+%22%5Cn%22+%22%5Ct%22+%22%5Cr%22+site:stackoverflow.com


#7

I have absolutely no idea why it wasn't printing for me then =/

Thanks for the article, and your help :slight_smile:


#8

@systemrunner92169,

Maybe it was a case of....

++++ reset of session *****************
under certain circumstances you can shoot your Browser in an
inconsistent state.

Therefor it is of an advantage to know that you have 2 reset facilities:

One is the use of the F5-key which does a refresh Browser

and

Two, select&copy your code
Then use the Reset Code button of the course-window,
then paste your code back in.

Addendum
General Notes:
Always refresh the browser after making corrections:
CTRL f5 ( if on Windows or Linux)
CMD r ( if on a MAC).
CTRL 0 to reset browser zoom


#9

Hi, it works now:)

def greeter (name)
return 'Hello ' +name
end

def by_three?(n)
if n % 3 == 0
return true
else
return false
end
end