• Home
  • About
  • Projects
  • Blog
Electric Chance

Life of an iOS Developer + Eager Learner

  • Home
  • About
  • Projects
  • Blog

Le Deli Counter: Parte Un

Let me just say that if you're not doing the Flatiron School's Web Development course on their Learn platform, you should be! It's not like Codecademy (no shade to Codecademy. I absolutely love it and to this day it helps me so much). It's actually not like any other coding resource I've ever used. But talking about Learn is a completely different post. I just wanted to put it out there that it's pretty awesome.

Now. (Le) Deli Counter. It's a section in Learn where I must create three methods that helps a 'deli' keep track of customers. I just completed and passed the first method with a little help from a fellow Learner. Basically I was doing good until I got stuck when a string wasn't returning as I needed it to. It failed to print the right thing. I would get: 

The line is currently: 1. Logan
The line is currently: 1. Logan 2. Avi
The line is currently: 1. Logan 2. Avi 3. Spencer

When I was suppose to get:

The line is currently: 1. Logan 2. Avi 3. Spencer

And here was my incorrect code:

def line(katz_deli)

if katz_deli.count == 0 
puts "The line is currently empty."
else 

line = "The line is currently:"

katz_deli.each_with_index do |value, index|
puts line << " #{index + 1}. #{value}"
end
end
end

I wasn't sure how to get each output of my value and index (1. name 2. name...) to print together in the same string instead of separate lines.

Then a kind Learner came along and supplied this knowledge:

Your code is currently doing exactly what you don't want because the instruction is 'for each person in katz deli print line with person added to line' when it should be 'for each person in katz deli add person to line' and after that (i.e outside of the each-loop block 'print line' '''

That made everything clear and I ended up with this code, the code that passed:

def line(katz_deli)
  if katz_deli.count == 0
    puts "The line is currently empty."
  else
  line = "The line is currently:"
  katz_deli.each_with_index do |value, index|
    line << " #{index + 1}. #{value}"
end
    puts line
end
end

I just had to call 'line' after the block. Before this code I also tried assigning the #{index + 1}. #{value} into a variable (customers) then adding it to 'line' outside of the block, but that wouldn't work because by defining customers in the block I made it a local variable a.k.a unusable outside the block.

Things are always so much easier in retrospect. I have two methods left to complete. I really want to complete them without help. I'll post about how it goes.

Peace.

Saturday 03.19.16
Posted by LC
Newer / Older

Powered by Squarespace.