Code Monkey home page Code Monkey logo

oxford-comma's Introduction

Oxford Comma List Formatter

Objectives

  1. Iterate over array elements.
  2. Use various methods to manipulate arrays and strings.
  3. Convert data types.

Converting Types

In Ruby, there are a few methods available to us for converting data types. For example, it is possible to convert a string to an array, an array into a string, or a range into an array, among other conversions. You can learn more about conversion here, but for now we'll just take a look at a few.

String to Array

The .split method will convert a string into an array. The .split method takes an argument of the character in the string on which you want to split it. For example, if we have a string that contains a comma-separated list of items:

"hippo,giraffe,monkey,horse"

We can call .split on it, with an argument of a , ("comma") to get an array of animal names:

"hippo,giraffe,monkey,horse".split(",")
  => ["hippo", "giraffe", "monkey", "horse"]

Range to Array

The .to_a method, when called on a range, can convert a range of numbers to an array:

(1..10).to_a
  => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Array to String

The .join method, when called on an array, will convert it into a string. This method takes in an optional string argument containing either a character or a set of characters that will be inserted between each array element, as they are assembled into a string.

For example, calling .join with no argument will create a string out of the array's elements by placing each one directly adjacent to the next one:

["a", "b", "c"].join
  => "abc"

But calling .join on the same array and giving it an argument of a " :-) " ("smiley face") will place an occurrence of that string, (i.e. the smiley face) between each letter:

["a", "b", "c"].join(" :-) ")
  => "a :-) b :-) c"

Instructions

  • Fork and clone this lab.
  • Write a method oxford_comma that takes an array of string elements as an argument and converts it into a string using the Oxford comma. For example, the array ["fiddleheads","okra","kohlrabi"] should get converted to the string "fiddleheads, okra, and kohlrabi".
    • Hint: You will need to refer to the section above about converting arrays into strings, but note that coding this method will involve a couple of extra challenges.
    • Hint: Remember, strings can be operated on very similarly to arrays. For instance, you can add elements to the end of strings with the << ("shovel") method just like you can with arrays.
  • Run learn test and read through the errors to see the specific cases your method will need to handle.
  • This might be a challenging lab, so take your time using Google and playing around with your code. Good luck and have fun!

Resources

oxford-comma's People

Contributors

annjohn avatar deniznida avatar fislabstest avatar fs-lms-test-bot avatar ihollander avatar kthffmn avatar lizbur10 avatar markedwardmurray avatar matbalez avatar maxwellbenton avatar sarogers avatar sophiedebenedetto avatar taoliu12 avatar timothylevi avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

oxford-comma's Issues

Spelling error

"Formatting" is misspelled in the lab tests section.

"#oxford_comma
returns a string without any additional fomatting when given a 1-element array"

Typos

Hi,

I think there is a typo in this line:
"The .join method, when called on a string, will convert it into an array."
Think it should read, " The .join method, when called on an array, will convert it into a string."

And a type on this line, "But calling .join on the same string and giving it an argument of a " :-) " "
should be "But calling .join on the same array and giving it an argument of a " :-) "

Who care about an Oxford Comma? I do, that's who!

def oxford_comma(array)
if array.length == 1
array.join

elsif array.length == 2
  array.join(" and ")

elsif array.length == 3
  array.join(",") 

end
end

  1. #oxford_comma adds commas plus a final 'and' when given a 3-element array
    Failure/Error: expect(oxford_comma(["kiwi", "durian", "starfruit"])).to eq("kiwi, durian, and starfruit")
    expected: "kiwi, durian, and starfruit"
    got: ["kiwi", "durian", "starfruit", "end"]
    Passing 2/4

Why is .join not working for my second elsif? I am returning an array, which is not what I want. I am trying to convert the array to a string. I am also trying to add commas, and an " and, " at the end of the array. The lab hints about the shovel method, however, that method can't insert the "and" where I need it. Is there like a splice or insert the ", and " into the end of the string? Any help would be appreciated!

In the array to string paragraph of the oxford comma module

you give an example of using the join method with a smiley face argument and show the following example:

["a", "b", "c"].join(" :-) ")
=> "a :-) b :-) c :-)"

In my opinion there should not be a :-) after the c in the string output because join does not input its argument after the final element of the array.

I feel that it should be the following:

["a", "b", "c"].join(" :-) ")
=> "a :-) b :-) c"

Hope that made sense. If not let me know and I will try to rephrase.

thanks

Lessons refactored

Not sure if this already exists here and I haven't found it. But it would be very nice and helpful if after we passed the lesson correctly that we were able to see how a more experienced programmer would write the program. That way we can refactor our code to see better ways of doing the same thing.

AAQ froze

Not sure if this is a bug that could be fixed or not, but I opened AAQ, asked my question, got matched with someone, but then it wouldn't allow me to text back. It would allow me to create text, but not send it. I refreshed the page, and was able to send text again, but now no response and don't think I'm "matched" to the person anymore. I will mark the issue "resolved" so I can try to get help again, but wanted that to be reported in case it happens to anyone else!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.