Skip to content

Instantly share code, notes, and snippets.

@itzsaga
Last active November 11, 2017 23:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save itzsaga/e7c74c8c2a5b53e4ada3166b5fb062fc to your computer and use it in GitHub Desktop.
Save itzsaga/e7c74c8c2a5b53e4ada3166b5fb062fc to your computer and use it in GitHub Desktop.
Example code for my Good Code vs. Bad Code Flatiron School study group.

Create a function that returns the sum of the two lowest positive numbers given an array of minimum 4 integers. No floats or empty arrays will be passed.

For example, when an array is passed like [19, 5, 42, 2, 77], the output should be 7.

[10, 343445353, 3453445, 3453545353453] should return 3453455.

Hint: Do not modify the original array.

# My Solution
def sum_two_smallest_numbers(numbers)
sorted = numbers.sort
sorted[0] + sorted[1]
end
# "Best Practice"
def sum_two_smallest_numbers(numbers)
numbers.min(2).reduce(:+)
end

Welcome. In this kata, you are asked to square every digit of a number.

For example, if we run 9119 through the function, 811181 will come out.

Note: The function accepts an integer and returns an integer

// My Solution
function squareDigits(num){
// Break the number up into an array
const digits = ('' + num).split('')
// Create a new empty array to store the squares
let squared = []
// Loop over array, square value & push into empty array
for (let i = 0; i < digits.length; i++) {
squared.push(digits[i] * digits[i])
}
// Join array and return the value
return Number(squared.join(''))
}
// "Best Practice"
function squareDigits(num){
return Number(('' + num).split('').map(function (val) { return val * val;}).join(''));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment