Skip to content

Instantly share code, notes, and snippets.

View clarkj99's full-sized avatar

Clark Johnson clarkj99

View GitHub Profile
@clarkj99
clarkj99 / ransom_note_solution2.rb
Created March 1, 2020 05:05
Ransom Note Hash Table Solution
def checkMagazine(magazine, note)
mag_hash = {}
magazine.each do |word|
mag_hash[word] = mag_hash[word] ? mag_hash[word] + 1 : 1
end
answer ='Yes'
note.each do |word|
if !mag_hash[word] || mag_hash[word] <= 0
answer = 'No'
else
@clarkj99
clarkj99 / ransom_note_solution1.rb
Last active March 1, 2020 03:59
Ransom Note Count and Reduce Solution
def checkMagazine(magazine, note)
check = note.reduce(true) do |memo,word|
memo = (magazine.count(word) >= note.count(word)) ? memo : false
end
puts check ? "Yes" : "No"
end
@clarkj99
clarkj99 / ransom_note_starter.rb
Created March 1, 2020 03:29
Ransom Note Starter
#!/bin/ruby
require 'json'
require 'stringio'
# Complete the checkMagazine function below.
def checkMagazine(magazine, note)
end
@clarkj99
clarkj99 / handleKeep.jsx
Last active January 25, 2020 05:45
handleKeep camera upload fiunction
handleKeep = () => {
const formData = new FormData();
formData.append("camera", this.state.currentPhoto);
// Use an appropriate url
fetch(`${baseURL}/photo/${this.props.profile.id}`, {
method: "PATCH",
body: formData
})
.then(res => res.json())
@clarkj99
clarkj99 / handlePhoto.jsx
Created January 25, 2020 05:41
handlePhoto method for camera
handlePhoto = dataUri => {
this.setState({ currentPhoto: dataUri });
]};
@clarkj99
clarkj99 / uploadPhoto.jsx
Last active January 25, 2020 05:31
Upload a photo file
uploadPhoto = () => {
const formData = new FormData();
formData.append("file", this.state.newPhoto);
// configure your fetch url appropriately
fetch(`${baseURL}/photo/${this.props.profile.id}`, {
method: "PATCH",
body: formData
})
.then(res => res.json())
@clarkj99
clarkj99 / handleImageChange.jsx
Created January 25, 2020 05:24
handleImageChange input file handler
handleImageChange = e => {
if (e.target.files[0]) this.setState({ newPhoto: e.target.files[0] });
};
@clarkj99
clarkj99 / photos_controller.rb
Last active January 25, 2020 04:56
Update method for Active Storage
def update
if params[:file]
# The data is a file upload coming from <input type="file" />
@profile.avatar.attach(params[:file])
# Generate a url for easy display on the front end
photo = url_for(@profile.avatar)
elsif params[:camera]
# The data is Base64 and coming from the camera.
# Use that data to create a file for active storage.
blob = ActiveStorage::Blob.create_after_upload!(
@clarkj99
clarkj99 / moment2.jsx
Created January 24, 2020 21:21
Moment example 2
value = moment(
e.target.value,
moment.HTML5_FMT.DATETIME_LOCAL
).format();
@clarkj99
clarkj99 / datetime.jsx
Created January 24, 2020 21:13
Datetime-local example
<input
className="input"
name={props.field}
type="datetime-local"
value={moment(props.value).format(
moment.HTML5_FMT.DATETIME_LOCAL
)}
onChange={this.handleChange}
/>