The if var<arr[i] isn’t checking var.length or arr[i].length
arr[i] evaluates to the string of each word. You need to call .length on it if you want the length value to compare.
var itself is also just a string. You need to do the same for it.
At first glance it looks like fixing this will make it work, but you might want to check and make sure every word is being evaluated when you do things like this in the future. For that I recommend writing
puts arr[1]
somewhere in the while loop and see what prints out on the command line. You can use puts to print most anything in Ruby to see what you’re working with.
Thank you, Sir and everybody else for your help.
I was finally able to get it to work according to my logic and although I’m not very sure if it checks for everything it needs to check - it does return true on all tests.
So happy camper here
def longest_word(sentence)
temp=sentence.split(" ")
i=0
bigW=nil
while(i<temp.length)
currentW=temp[i]
if bigW==nil
bigW=temp[i]
elsif bigW.length<currentW.length
bigW=currentW
end
i+=1
end
return currentW
end
@marin-cabac: Just, so that you don’t feel left out of @danielpclark and my conversation, I’ll try and comment a bit on your final code-snippet.
I know that you’re a beginner in the world of Ruby, so I’ll go through the stuff one at a time.
First off, to ease your readability, I propose that you begin to add spaces before and after =, <, >, etc.
Second, we very seldom use while and for loops in Ruby code. Usually we use the each on arrays instead. Say you want to just print out each element in an array. Here’s what that looks like, first with while, then for, and finally the “correct way” with each:
words = ["This", "is", "a", "sentence"]
i = 0
current_word = nil
while i < words.size
current_word = words[i]
puts current_word # => this outputs the word
i += 1
end
Using for we can get rid counting i ourselves:
words = ["This", "is", "a", "sentence"]
current_word = nil
for i in 0..words.size-1
current_word = words[i]
puts current_word # => this outputs the word
end
And finally, we can do it with the each method on the array, which is the “proper” way of doing it in Ruby:
words = ["This", "is", "a", "sentence"]
words.each do |current_word|
puts current_word # => this outputs the word
end
The each yields each element of the array to the variable between the two ||. Had we wanted the index for something, we could use each_with_index and then have both |current_word, i| in the vertical bars instead.
I hope that clears it up a bit regarding the loop constructs? Once you find yourself familiar with each you can look into other methods, like sort, max, and so on as well.