Skip to content

Instantly share code, notes, and snippets.

@Jiert
Jiert / Wat.js
Last active December 15, 2015 14:29
I found this in a code base
'00'.length
@Jiert
Jiert / JavaScript-Tabs.js
Last active November 29, 2023 11:40
Creating Tabs with Vanilla JavaScript
<!DOCTYPE html>
<html lang="en">
<head>
<title>Tabs</title>
<style>
.nav .active a { color: red; }
.tab-pane { display: none; }
.tab-pane.active { display: block; }
</style>
</head>
@Jiert
Jiert / coinify.js
Last active August 29, 2015 14:20
Coinify, find the lowest number of coins it would take to make X amount of cents
function coinify(cents){
var count = parseInt(cents / 50),
leftOver = cents % 50,
coins = [25, 10, 5, 1],
iterator = 0;
while (leftOver > 0){
count += parseInt( leftOver / coins[iterator] );
leftOver = leftOver % coins[iterator];
iterator ++;
@Jiert
Jiert / bad.js
Last active August 29, 2015 14:21
Beautiful
importSchedule.startHourOptions = this._buildOptions(_.chain(_.range(0, 24)).map(function(hour){
return {text: '00'.substring(0, '00'.length - hour.toString().length) + hour.toString(), value: hour};
}).value(), importSchedule.start_hour);
@Jiert
Jiert / app.js
Created August 20, 2015 21:30
Image Loading with Underscore
initialize: function(){
_.bindAll(this, 'render', 'onImgLoad');
this.mainImg = new Image();
this.mainImg.src = this.model.get('img');
this.userImg = new Image();
this.userImg.src = this.model.get('admin').img;
this.mainImg.onload = this.onImgLoad;
@Jiert
Jiert / converter.js
Created August 25, 2015 04:38
An interview test: Convert a string "1234" into an integer 1234 without using parseInt or Number
function converter(st){
var arrSt = st.split(''),
arrInt = [];
for (var i = 0; i < arrSt.length; i++){
arrInt.push(arrSt[i].charCodeAt() - 48);
}
// loop over arry again
@Jiert
Jiert / eval.js
Created August 25, 2015 20:01
I found this in a Shopify theme.
var data = eval('(' + XMLHttpRequest.responseText + ')');
@Jiert
Jiert / this.js
Last active August 28, 2015 04:14
Code from an interview question about 'this'
var foo = {
a: function () {
console.log(this);
}
};
foo.a();
// => foo
foo['a']();
@Jiert
Jiert / factory.js
Last active October 5, 2015 18:22
ES6 Factory Function example
const dog = () => {
const sound = 'woof'
return {
talk: () => console.log(sound)
}
}
const sniffles = dog()
sniffles.talk() // "woof"
@Jiert
Jiert / composition.js
Created October 5, 2015 18:22
Composition Example
const barker = (state) => ({
bark: () => console.log('Woof, I am ' + state.name)
})
const driver = (state) => ({
drive: () => state.position = state.position + state.speed
})
barker({ name: 'karo'}).bark()
// Woof, I am karo