How can I DRY this series of conditional statements?

Dylan Richards

I often find myself checking multiple conditions. How can I cut down on the number of lines used to achieve the same effect?

def super_fizzbuzz(array)
    final = []
    for num in array
        if num % 15 == 0
            final << 'FizzBuzz'
        elsif num % 5 == 0
            final << 'Buzz'
        elsif num % 3 == 0
            final << 'Fizz'
        else
            final << num
        end
    end
    final
end
xdazz
def super_fizzbuzz(array)
  array.map do |num|
    a = []
    a << 'Fizz' if num % 3 == 0
    a << 'Buzz' if num % 5 == 0
    a.empty? ? num : a.join()
  end
end

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I write a conditional without any conditional statements or operators?

From Dev

How can I write a conditional without any conditional statements or operators?

From Dev

How can I DRY up conditional content_for in Rails view?

From Dev

How can I use JavaDocs in Eclipse to show conditional return statements?

From Dev

How do I rewrite a series of conditional statements with Q promises in node.js?

From Dev

How do I simplify a conditional with multiple or statements?

From Dev

How can I put this DRY code into a for loop?

From Dev

How can remove conditional statements while adding the common responsibility to the class?

From Dev

How can I avoid these case statements?

From Dev

How can I reduce this long list of if statements?

From Dev

How can I combine these jQuery statements?

From Dev

How can I call a constructor after statements?

From Dev

How can I refactor a set of ugly if statements?

From Dev

How can I combine these jQuery statements?

From Dev

How can I combine these two statements?

From Dev

How can I write regex for the following statements?

From Dev

How can I join these two select statements?

From Dev

How can I make this a prepared Statements code?

From Dev

How can I refactor these simple functions and make them more DRY?

From Dev

How can I pass variables between DRY templates and ncludes in Jekyll

From Dev

How can I DRY out this F# code? (Fluent Interface)

From Dev

How can I uphold the principle of DRY in a specialized subclass?

From Dev

How can I keep track of state DRY-ly?

From Dev

How can I refactor these simple functions and make them more DRY?

From Dev

How can i keep my code dry in ajax requests?

From Dev

How can i make these 3 lines of code more DRY

From Dev

How can I refactor this for loop to adhere to the DRY principle?

From Dev

How can I make my JavaScript code more DRY?

From Dev

how do i do conditional branching on a elements of a pandas series?

Related Related

  1. 1

    How can I write a conditional without any conditional statements or operators?

  2. 2

    How can I write a conditional without any conditional statements or operators?

  3. 3

    How can I DRY up conditional content_for in Rails view?

  4. 4

    How can I use JavaDocs in Eclipse to show conditional return statements?

  5. 5

    How do I rewrite a series of conditional statements with Q promises in node.js?

  6. 6

    How do I simplify a conditional with multiple or statements?

  7. 7

    How can I put this DRY code into a for loop?

  8. 8

    How can remove conditional statements while adding the common responsibility to the class?

  9. 9

    How can I avoid these case statements?

  10. 10

    How can I reduce this long list of if statements?

  11. 11

    How can I combine these jQuery statements?

  12. 12

    How can I call a constructor after statements?

  13. 13

    How can I refactor a set of ugly if statements?

  14. 14

    How can I combine these jQuery statements?

  15. 15

    How can I combine these two statements?

  16. 16

    How can I write regex for the following statements?

  17. 17

    How can I join these two select statements?

  18. 18

    How can I make this a prepared Statements code?

  19. 19

    How can I refactor these simple functions and make them more DRY?

  20. 20

    How can I pass variables between DRY templates and ncludes in Jekyll

  21. 21

    How can I DRY out this F# code? (Fluent Interface)

  22. 22

    How can I uphold the principle of DRY in a specialized subclass?

  23. 23

    How can I keep track of state DRY-ly?

  24. 24

    How can I refactor these simple functions and make them more DRY?

  25. 25

    How can i keep my code dry in ajax requests?

  26. 26

    How can i make these 3 lines of code more DRY

  27. 27

    How can I refactor this for loop to adhere to the DRY principle?

  28. 28

    How can I make my JavaScript code more DRY?

  29. 29

    how do i do conditional branching on a elements of a pandas series?

HotTag

Archive