Skip to content

Instantly share code, notes, and snippets.

// Bonfire: Pig Latin
// Author: @tylerl-uxai
// Challenge: http://www.freecodecamp.com/challenges/bonfire-pig-latin
// Learn to Code at Free Code Camp (www.freecodecamp.com)
function translate(str) {
var regex = /[aeiou]/gi;
if (str[0].match(regex)){
return str + "way";
} else if (str[1].match(regex)){
// Bonfire: Search and Replace
// Author: @tylerl-uxai
// Challenge: http://www.freecodecamp.com/challenges/bonfire-search-and-replace
// Learn to Code at Free Code Camp (www.freecodecamp.com)
// Rewritten by myself with the help of https://github.com/FreeCodeCamp/FreeCodeCamp/wiki/Bonfire-Search-and-Replace
function myReplace(str, before, after) {
var i = str.indexOf(before);
if (str[i] == str[i].toUpperCase()){
// Bonfire: Where art thou
// Author: @tylerl-uxai
// Challenge: http://www.freecodecamp.com/challenges/bonfire-where-art-thou
// Learn to Code at Free Code Camp (www.freecodecamp.com)
// Rewritten myself with help from https://github.com/FreeCodeCamp/FreeCodeCamp/wiki/Bonfire-Where-art-thou
function where(collection, source) {
// speed test
var keys = Object.keys( source ); // returns the keys to the variable
// Bonfire: Roman Numeral Converter
// Author: @tylerl-uxai
// Challenge: http://www.freecodecamp.com/challenges/bonfire-roman-numeral-converter
// Learn to Code at Free Code Camp (www.freecodecamp.com)
// re-wrote by myself with the aid of http://wulkan.me/bonfire-roman-numeral-converter/
function convert(num) {
var romanNumerals = '',
decimals = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1],
numerals = ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I'];
// Bonfire: Diff Two Arrays
// Author: @tylerl-uxai
// Challenge: http://www.freecodecamp.com/challenges/bonfire-diff-two-arrays
// Learn to Code at Free Code Camp (www.freecodecamp.com)
// with the help of http://wulkan.me/bonfire-diff-two-arrays/ and https://github.com/pfilippi24
function diff(arr1, arr2) {
// Same, same; but different.
return arr1.concat(arr2).filter(function(val){
return !(arr1.indexOf(val)>=0 && arr2.indexOf(val) >=0);
function p = predict(theta, X)
%PREDICT Predict whether the label is 0 or 1 using learned logistic
%regression parameters theta
% p = PREDICT(theta, X) computes the predictions for X using a
% threshold at 0.5 (i.e., if sigmoid(theta'*x) >= 0.5, predict 1)
m = size(X, 1); % Number of training examples
% You need to return the following variables correctly
p = zeros(m, 1);
Running Gradient Descent ...
ans =
32.0727
Theta found by gradient descent: 0.000000 0.000000
For population = 35,000, we predict a profit of 0.000000
For population = 70,000, we predict a profit of 0.000000
function J = computeCost(X, y, theta)
%COMPUTECOST Compute cost for linear regression
% J = COMPUTECOST(X, y, theta) computes the cost of using theta as the
% parameter for linear regression to fit the data points in X and y
% Initialize some useful values
m = length(y); % number of training examples
% You need to return the following variables correctly
function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters)
%GRADIENTDESCENT Performs gradient descent to learn theta
% theta = GRADIENTDESENT(X, y, theta, alpha, num_iters) updates theta by
% taking num_iters gradient steps with learning rate alpha
% Initialize some useful values
m = length(y); % number of training examples
J_history = zeros(num_iters, 1);
for iter = 1:num_iters