CodeCrack
CodeCrack

Reputation: 5363

Can't figure syntax error, unexpected end-of-input, expecting keyword_end error

I get:

Untitled 5.rb:16: syntax error, unexpected end-of-input, expecting
keyword_end

even though I closed the if/else statement and def with end:

def max_2_sum(int_collection)
  sum = 0
  if int_collection.empty?
    sum
  else if int_collection.count == 1
    int_collection.first
  else
    sum = int_collection.sort[-1] + int_collection.sort[-2] 
  end
end


int_collection = [4,3,9]    
puts max_2_sum(int_collection)

Upvotes: 0

Views: 502

Answers (2)

Rajarshi Das
Rajarshi Das

Reputation: 12320

your code should be like below

def max_2_sum(int_collection)
  sum = 0
  if int_collection.empty?
    sum
  elsif int_collection.count == 1
    int_collection.first
  else
     sum = int_collection.sort[-1] + int_collection.sort[-2] 
   end
 end

Upvotes: 0

tckmn
tckmn

Reputation: 59283

else if

is wrong. It should be

elsif

Upvotes: 2

Related Questions