Redacted! Advanced Questions!


#1



https://www.codecademy.com/en/courses/ruby-beginner-en-mzrZ6/0/6?curriculum_id=5059f8619189a5000201fbcb


The code runs smoothly; however, I am trying to tackle the following challenges, and failing miserably:

  1. What could you do to make sure your redactor redacts a word regardless of whether it's upper case or lower case?
  2. How could you make your program take multiple, separate words to REDACT?
  3. How might you make a new redacted string and save it as a variable, rather than just printing it to the console?

I attempted to address #1 by adding .downcase! to the user's input, as well as a few other places, but it's not working for me... Help?

number 2 and number 3 are completely over my head... please help... thank you!


puts "Enter string of words:"
text = gets.chomp.downcase!
puts "Enter word you'd like redacted:"
redact = gets.chomp.downcase!

words = text.split(" ")

words.each.downcase! do |word|
    if word != redact
    print word + " "
else
    print "REDACTED "
    end
end


#2

Forget code for a while and think instead about what actions that need to be carried out - how would you do it manually? You might even want to write that down, convince yourself of that if those instructions are followed then the desired result will be obtained, and only then describe those actions in code

If you believe that the actions that you mean to describe are the right things to do, then you've already determined that you didn't accurately write them in code which is then something you can investigate and/or search for how to carry out the individual actions

If you downcase both the text and the word to be redacted, then yes they would compare as equal regardless of original case. You would be losing information though, so you might rather want to only do it for the comparison.

2 Can be implemented by repeating the process for each word to be redacted

3 you'd need to store the partial results and then create a single value from that once everything has been processed. Arrays support that kind of thing.


#3

It already does that. It transforms the word to lowercase, then redacts it. It doesn't matter what case the word was before.

Simply enter multiple words to redact.

sampleVar = text


#4

Hey Shrike2 , thanks for the response!

I slightly modified my code, and now it is properly redacting REGARDLESS of capitalization! Thank you!

However, if you run my code, and enter multiple words at the redact prompt, it simply fails to redact any of them...
I was guessing I'd have to break it down into an array as I did the initial string, and then some how compare each component of the two arrays.... Not sure how to do that yet.

And I will have to play with the "sampleVar = text" solution, thanks!


#5

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