Converting Between Symbols and Strings


#1


https://www.codecademy.com/courses/ruby-beginner-en-Qn7Qw/1/4?curriculum_id=5059f8619189a5000201fbcb#

"Looks like each symbolized string is not in the symbols array" is my error message


strings = ["HTML", "CSS", "JavaScript", "Python", "Ruby"]

# Add your code below!

symbols = []
strings.each do |word|
  $i = 0
  lowercase = word.downcase
  letters = lowercase.split("")
  while $i <= letters.length  do
    if letters[$i] == "s"
      symbolized = letters[$i].to_sym
      symbols.push symbolized
    end
    $i +=1
  end
end

print symbols

I'm getting an array with [:s, :s, :s] which is what the question is asking for


#2

Uh.. Are the strings to convert the ones on line 1?
Because [:s, :s, :s] doesn't really resemble those strings does it?

And if the only thing that's supposed to be iterated through is a list of strings, then what is the other loop in your code doing?

Why is there special mention of the string "s" in the code? Why is that significant?


#3

Here are the directions:

We have an array of strings we'd like to later use as hash keys, but we'd rather they be symbols.

Create a new variable, symbols, and store an empty array in it.
Use .each to iterate over the strings array.
For each s in strings, use .to_sym to convert s to a symbol and use .push to add that new symbol to symbols

My understanding is that we're supposed to push each "s" as a symbol to the symbols array am I miss interpreting this?
.


#4

There is no "s" in strings, none of the elements are equal to that value. Some elements do themselves contain that character, but that's not what they're saying. They're using s to refer to each element of strings

"For each s in strings" is analogous to:

strings.each do |s|

#5

ok, wow I was making this way more complicated I got it now thanks for your help


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.