Skip to content

Instantly share code, notes, and snippets.

View shanelonergan's full-sized avatar
⌨️

Shane Lonergan shanelonergan

⌨️
View GitHub Profile
@shanelonergan
shanelonergan / streak-tracker.rb
Last active February 9, 2022 01:01
Example code for tracker number of consecutive days a User performs a Session
class User < ApplicationRecord
has_many :sessions, -> {order "created_at DESC"}
def streak
streak_count = 0
today = Time.now.to_date
dates_array = self.sessions.map do | session |
session.created_at.to_date
function getElementsByClassName2(classNameStr) {
const elements = [] // the array we will add matching elements to
const firstChildren = this.children // all the children of the element the function is called on
return elements
}
<html>
<body>
<div class='parent'>
<p class='hello'>hello world 1</p>
<p class='hello'>hello world 2</p>
<p class='hello'>hello world 3</p>
<p class='hello'>hello world 4</p>
</div>
</body>
</html>
const parent = document.getElementsByClassName('parent')
// => <div class='parent'></div>
const helloWorlds = document.getElementsByClassName('hello')
// => [ <p class='hello'>hello world 1</p>,
// <p class='hello'>hello world 2</p>,
// <p class='hello'>hello world 3</p>,
// <p class='hello'>hello world 4</p> ]
function getElementsByClassName2(classNameStr) {
const elements = []
const firstChildren = this.children
function checkChildren(child) {
if (child.classList.contains(classNameStr)) {
elements.push(child)
}
function getElementsByClassName2(classNameStr) {
function checkChildren(child) {
// check if the child has a matching class. If so, push the the elements array
if (child.classList.contains(classNameStr)) {
elements.push(child)
}
// check if that child has children of its own. If so, call checkChildren one each child
HTMLElement.prototype.getElementsByClassname2 = getElementsByClassName2
Time.now.to_date
=> 2019-10-15
Time.now
=> 2019-10-15 16:16:16 -0400
unique_dates = dates_array.uniq