When I run this program
def tng(x)
tn =( x * (x+1) )/2
return tn
end
i = 0
while tng (i) <= 500
i += 1
end
puts i
it gives me the error "undefined method `+' for true:TrueClass (NoMethodError)". I suppose this means that true is input to the tng(x) function, but why would this occur?
In Ruby spaces before parentheses are significant. Parser treats
while tng (i) <= 500
as:
while tng((i) <= 500)
the latter is evaluated to true hence the error.
Sidenote: do not use return as a last statement in methods, it’s returned automatically. Also, don’t use while loops, use iterators [unless you do perfectly understand why a generic loop is to be used here]:
def tng(x)
(x * (x + 1)) / 2
end
1.upto(Float::INFINITY).each do |i|
break i unless tng(i) <= 500
end
Related
I'm currently trying to implement a mathematic method to approximate
f(x) = 0. I've already implemented it in some languages and I want to do it in ruby now just for training.
But I have this error that I really does'nt understand
Here is my code
def fonction (x)
return (x ** 3) + 4 * (x ** 2) - 10
end
def derive (x)
return 3 * (x ** 2) + 8 * x
end
def newton(f, fPrime, n, u0)
if n == 0 then
return u0
else
uN = newton (f, fPrime, (n - 1), u0)
return uN - f(uN) / fPrime(uN)
end
end
for i in 0..6
puts (newton (fonction, derive, i, 2))
end
i think there is space on newton method call
uN = newton (f, fPrime, (n - 1), u0) # there is space after newton
also in this one
for i in 0..6
puts (newton (fonction, derive, i, 2)) # there is space after newton
end
try remove it, and you will see another error i guess, i try it on repl
I have this code:
def power(x, n)
if n == 1
return x
else
x = x * power(x, n-1)
end
end
power(4, 4)
power(2, 3)
power(10, 3)
power(25, 2)
power(6, 5)
It takes the first number and raises it to the 2nd numberth power. So it works for all of them, but I want to write the code in a way that it prints the results for all 5 of the power functions. How do I do this? I tried to modify with puts instead of return but I cannot get it to work.
You have a variable x which points to the result of the method call. You can print this and then return it from the function:
def power(x, n)
if n == 1
return x
else
x = x * power(x, n-1)
puts x
x
end
end
I made a method that takes two numbers and returns a calculated value rounded to three decimals. I'm curious to know how I can have numbers such as 1.141 to be rounded but numbers like 5.0 turned into integers (5).
code:
def calculateHypotenuse(a,b)
if (a <= 0 || b <= 0)
return raise
end
c = Math.sqrt((a * a) + (b * b))
return c.round(3)
end
not sure there is a built in function for floats, but a hackish way could be something like this.
def conditional_truncation(x)
x.truncate == x ? x.truncate : x
end
conditional_truncation(1.141)
=> 1.141
conditional_truncation(5.0)
=> 5
I am taking a lesson on codecademy in which I am currently stuck do not know how to proceed - it is regarding return values.
The instructions are:
Write a method that takes an integer as an argument, and returns that integer times ten. Call times_ten in your code after you define it and print out its return value.
What is given in the script is:
def times_ten(integer)
# your code here
end
# call times_ten here
This is the example it gives but I am having a hard time understanding:
def first_squares(number_of_squares)
squares = []
idx = 0
while idx < number_of_squares
squares.push(idx * idx)
idx = idx + 1
end
return squares
end
puts("How many square numbers do you want?")
number_of_squares = gets.to_i
squares = first_squares(number_of_squares)
idx = 0
while idx < squares.length
puts(squares[idx])
idx = idx + 1
end
Thanks for your help
It should be:
def ten_times(n)
n*10 # you don't have to use 'return' explicitly
end
ten_times(n) -- but put in an actual integer instead of n (or maybe you have to puts or print it, depending on what they want)
Your example is not really related to your outcome.
The example script should be like this:
def ten_times(integer)
# integer * 10 #for implicit return
return integer * 10 #for explicit return
end
print ten_times(any number you want goes in here)
You can run the following code at www.rubyplus.biz:
Implicit return:
def times_ten(integer)
integer * 10
end
p times_ten(1)
Explicit return:
def times_ten(integer)
return integer * 10
end
p times_ten(2)
I'm getting a strange error: "syntax error, unexpected $end, expecting kEND" and it points to the final line of my code.
New to Ruby and not sure what I'm doing wrong here. Any help would be fantastic. Thanks!
def add(x,y)
if(x > y)
c = x + y
return c
else
puts "Y is too big"
return
end
a = 4
b = 6
add(a,b)
BTW, you can refactor your if..end statement out completely if you prefer
def add(x,y)
return (x + y) if(x > y)
puts "Y is too big"
end
Corrected code (you are missing one end for the if-else):
def add(x,y)
if(x > y)
c = x + y
return c
else
puts "Y is too big"
return
end
end
a = 4
b = 6
add(a,b)
Both if statements and function definitions require an end statement to terminate them.
Try adding another end after your existing end and your problem should go away.
wap to input principle, rate, time and choice. if choice is 1 than calculate simple interest, if choice is 2 calculate compund interest and if it is other than 1 or 2 print "Enter valid choice"