Skip to content

Instantly share code, notes, and snippets.

@codebubb
codebubb / arrayofwords.js
Created February 13, 2019 11:16
UpperCase an array of Words (using a for loop)
const arrayOfWords = ['the', 'quick', 'brown', 'fox'];
for (let i = 0; i < arrayOfWords.length; i += 1) {
arrayOfWords[i] = arrayOfWords[i][0].toUpperCase() + arrayOfWords[i].slice(1);
}
console.log(arrayOfWords); // ["The", "Quick", "Brown", "Fox"]
@codebubb
codebubb / post_curl.php
Created December 27, 2018 14:35
Send a cURL request with PHP rather than Postmant.
$data = array("name" => "James", "age" => "36");
$data_string = json_encode($data);
$ch = curl_init('http://localhost/post.php');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
let pp = this.proposalForm;
let $datefields = $('.date input').datepicker(
{
format: "dd/mm/yyyy"
}
);
$datefields.on('changeDate', function (e) {
let fcn = $(this).attr('formControlName');
let index = e;
@codebubb
codebubb / parse_params.js
Created April 15, 2016 13:31
function which takes a URL and extracts key/value pairs based on positions in array.
var parse_params = function(url){
var pairs = url.match(/\/?\??(.*)/)[1].split('&');
var out = [];
pairs.forEach(function(chunk){
var keyvalues = chunk.split('=');
for(var i=0; i<keyvalues.length; i++){
if(i % 2 === 0){
var obj = {};
obj[keyvalues[i]] = keyvalues[i + 1]
out.push(obj);
var sqlite3 = require('sqlite3').verbose();
var get_rows = function(query, callback){
var db = new sqlite3.Database('test.db');
db.serialize(function(){
db.all(query, function(err, rows){
callback(rows);
});
});
@codebubb
codebubb / ruby_mail.rb
Created February 1, 2016 08:50
Send secure mail with Ruby mail
# Make sure the mail gem is available first:
# gem install mail
require 'mail'
Mail.defaults do
delivery_method :smtp, {
:port => 465, # Change this if your provider requires a different port for SSL
:address => "<yourSMTPServer>",
:user_name => "<yourUserName>",
:password => "<yourPassword>",
// Bonfire: Everything Be True
// Author: @codebubb
// Challenge: http://www.freecodecamp.com/challenges/bonfire-everything-be-true
// Learn to Code at Free Code Camp (www.freecodecamp.com)
function every(collection, pre) {
// Is everyone being true?
return collection.every(function(e){
return Object.keys(e).indexOf(pre) !== -1 && e[pre];
});
// Bonfire: Binary Agents
// Author: @codebubb
// Challenge: http://www.freecodecamp.com/challenges/bonfire-binary-agents
// Learn to Code at Free Code Camp (www.freecodecamp.com)
function binaryAgent(str) {
return str.split(' ').map(function(e){
return String.fromCharCode(parseInt(e, 2));
}).reduce(function(a,b){
return a+b;
// Bonfire: Steamroller
// Author: @codebubb
// Challenge: http://www.freecodecamp.com/challenges/bonfire-steamroller
// Learn to Code at Free Code Camp (www.freecodecamp.com)
function steamroller(arr) {
var flattened = [];
function flatten(a){
return Array.isArray(a) ? a.forEach(flatten) : flattened.push(a);
}
// Bonfire: Smallest Common Multiple
// Author: @codebubb
// Challenge: http://www.freecodecamp.com/challenges/bonfire-smallest-common-multiple#
// Learn to Code at Free Code Camp (www.freecodecamp.com)
function smallestCommons(arr) {
var nums = range(arr[0], arr[1]);
return nums.reduce(lcm);
}