Skip to content

Instantly share code, notes, and snippets.

View bobdobbalina's full-sized avatar

Mike Grunwald bobdobbalina

View GitHub Profile
@bobdobbalina
bobdobbalina / random.js
Last active January 20, 2021 19:12
Javascript: Returning randomly true or false with the same 50% of probability
var randomBoolean = Math.random() >= 0.5;
@bobdobbalina
bobdobbalina / dayInYear.js
Created January 20, 2021 18:53
Javascript: Returning the number of the day in the year
function dayInYear(date){
return Math.floor((date - new Date(date.getFullYear(), 0, 0)) / 1000 / 60 / 60 / 24);
}
dayInYear(new Date()); // 12
@bobdobbalina
bobdobbalina / clamping.css
Last active December 29, 2020 21:56
Truncating lines of text
/*
SAMPLE HTML
<div class="demo-grid">
<div class="demo-card">
<img width="200" height="200" src="https://placekitten.com/200/200?image=1" alt="" class="demo-img">
<h1 class="line-clamp demo-title">
Spill litter box, scratch at owner, destroy all furniture, especially couch
</h1>
</div>
@bobdobbalina
bobdobbalina / object-is-empty.js
Last active August 24, 2020 17:37
Javascript: Object is empty
Object.keys(myObject).length;
@bobdobbalina
bobdobbalina / array-last-items.js
Created July 21, 2020 14:04
Javascript: Get Last Items in an Array
let array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
console.log(array.slice(-1)); // Result: [9]
console.log(array.slice(-2)); // Result: [8, 9]
console.log(array.slice(-3)); // Result: [7, 8, 9]
@bobdobbalina
bobdobbalina / truncate-arrays.js
Last active July 21, 2020 13:58
Javascript: Truncate Array
let array = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ];
array = array.slice( 0, 4 );
console.log( array ); // Result: [0, 1, 2, 3]
// OR
let array = [0, 1, 2, 3, 4, 5, 6, 6, 8, 9]
array.length = 4
@bobdobbalina
bobdobbalina / string-number-cohersion.js
Created June 3, 2020 14:47
Javascript: String and Number Cohersion
// NUMBER TO STRING
//==========================
const val = 1 + '';
console.log( val ); // Result: "1"
console.log( typeof val ); // Result: "string"
// STRING TO NUMBER
//==========================
let int = '15';
@bobdobbalina
bobdobbalina / remove-items-from-array.js
Last active August 2, 2021 15:34
Javascript: Remove items from array
// If you know multiple values
const items = ['a', 'b', 'c', 'd', 'e', 'f'];
const valuesToRemove = [ 'c', 'd' ];
const filteredItems = items.filter(( item ) => !valuesToRemove.includes( item ));
// ["a", "b", "e", "f"]
@bobdobbalina
bobdobbalina / promises-complete.js
Last active June 3, 2020 14:34
Javascript: Wait until promises ar complete
const PromiseArray = [
Promise.resolve(100),
Promise.reject(null),
Promise.resolve("Data release"),
Promise.reject(new Error('Something went wrong'))];
Promise.all(PromiseArray)
.then(data => console.log('all resolved! here are the resolve values:', data))
.catch(err => console.log('got rejected! reason:', err))
@bobdobbalina
bobdobbalina / sort-number-arrays.js
Last active June 3, 2020 14:34
Javascript: Sort number arrays
[0, 10, 4, 9, 123, 54, 1].sort((a, b) => a - b);
>>> [0, 1, 4, 9, 10, 54, 123]