Javascript – Magic 8 Ball Script

This is a magic 8 ball program written in html and javascript. So it’s very lean fast and quick. You are free to use this script because it’s open source I made this solely for fun. So fork it. This is basically a random answer generator which uses random number and switch statement to be written. Have fun. 😉

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Magic 8 ball</title>
</head>
<body>
	<input type="text" placeholder="Ask your question?">
	<button onclick="myFunction()">Answer</button>
	<p id="demo"></p>

	<script>
function myFunction() {
	function getRandomArbitrary(min, max) {
	return Math.floor(Math.random() * (max - min) + min);
	}
	var num = getRandomArbitrary(1,6);

	switch(num) {
	    case 1:
	        answer = "Yes";
	        break;
	    case 2:
	        answer = "No";
	        break;
	    case 3:
	        answer = "Try Again";
	        break;
	    case 4:
	        answer = "Outlook not so good";
	        break;
	    case 5:
	        answer = "Most definitely!";
	        break;
	}
	document.getElementById("demo").innerHTML = answer;
}
	</script>
</body>
</html>

 

Magic 8 Ball Demo

jQuery – $.each vs $.map jquery

We are going to go over what are the difference between $.each and $.map in jquery. Both are like foreach loops, but utilized differently.

The each method is meant to be an immutable iterator, where as the map method can be used as an iterator, but is really meant to manipulate the supplied array and return a new array.

Another important thing to note is that the each function returns the original array while the mapfunction returns a new array. If you overuse the return value of the map function you can potentially waste a lot of memory.

<script>
    var cities = ['Paris', 'London', 'Orlando'];
    
    $.each(cities, function(index, city){
        var result = city + " " + index;
        console.log(result);
    });
    // paris 0
    // london 1
    // orlando 2

    $.map(cities, function(city, index){
        var result = city + " " + index;
        console.log(result);
        return result;
    });
    // paris 0
    // london 1
    // orlando 2

</script>

I made my first Game With TeamTreehouse and Unity Engine

I made my first game with the Teamtreehouse course. It’s basically a game based on C# and coding. He goes over how to write a game in the Unity Engine step by step. I added my own functionality to game such as quitting the game with esc. In addition, starting the game with up, and return key. Nick doesn’t go over that.

The course goes over programming, mixing, audio, and building the game. The only thing the class doesn’t go over is how to create 3D graphics, you would probably need a 3D artist to do that. The assets are provided such as the frog, bird, flies, map, and audio. This game is basically a pick up the coins kind of game, but there is an enemy the bird. The bird can eat you and you lose.

unityAudio

My setup for this course is two dual monitors and i7 computer. I would not be able to follow these courses as fast if I didn’t have dual monitors. Basically, I would have one side with the videos and one side with me following along and working.

So do I recommend this course and Teamtreehouse for programming? Yes, I’m fan. But don’t be afraid to supplement your learning with youtube, books, and other online courses. Learning never stops in the programming world. I can’t recommend enough to learn the core languages php, mysql, ruby, python, css, html, and javascript. Libraries and frameworks come and go.

Since, dabbled in other programming languages figuring out object orient programming was kind of tricky. I didn’t understand what was happening under the hood, but I used the scripts anyway and made it work. The text editor in Unity Engine is great. I didn’t have to debug much. Intellisense rocks.

Anyways. If you found this review awesome. Please use my referral link below to learn code first 2 weeks free:
http://referrals.trhou.se/jasonchan2

FrogGame
You can try out my game below:
Windows Build
Mac Build

Why Sass is Awesome?

Why is sass awesome?

It makes css work way easier and faster. You have to have ruby on rails installed and the sass gem installed.

First install sass with this command (you only need to do it once)
gem install sass

Type in CLI first navigate to correct director with cd

sass – watch .

to compile .scss into .css

Below is to install the bourbon library for css
gem install bourbon

bourbon library

scss advanced code

@mixin box($size, $color, $display: block) {
	width: $size;
	height: $size;
	background: $color;
	display: $display;
}

a {
	@include box($size: 10px, $color: blue);
}

// for the list don't forget ... example below members... for a list /w out is static
@mixin band($name, $members...) {
	@each $member in $members {
		.#{$name}.#{$member} {
			background: url("images/#{$name}/#{$member}.jpg");
		}
	}
}

div {

	margin: 0px 0 0 10px;
}

@include band(radiohead,thom, jonny, colin, phil);
@include band(nin, cuties);

It compiles into .css:

a {
  width: 10px;
  height: 10px;
  background: blue;
  display: block; }

div {
  margin: 0px 0 0 10px; }

.radiohead.thom {
  background: url("images/radiohead/thom.jpg"); }

.radiohead.jonny {
  background: url("images/radiohead/jonny.jpg"); }

.radiohead.colin {
  background: url("images/radiohead/colin.jpg"); }

.radiohead.phil {
  background: url("images/radiohead/phil.jpg"); }

.nin.cuties {
  background: url("images/nin/cuties.jpg"); }

Hello World in React Js

This is hello word app created with react.js This is my first simple script. Yes, I know it’s easier to do this in html. But I wanted to do this in JSX to make thing more complicated.

 

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Jsx</title>
</head>
<body>
	
	<! – container node – >
	<div id="jsx">
		
	</div>
	<! – container node – >
	<div id="dom">
		
	</div>
	<script src="https://fb.me/react-0.11.1.js"></script>
	<script src="https://fb.me/JSXTransformer-0.11.1.js"></script>
	<script type="text/jsx">
	/** @jsx React.DOM */

	var MessageBox = React.createClass({

		render: function(){
			return (
				<div className="container">
				<h1>Hi</h1>
				<SubMessage />
				</div>

				);
		}
	});

	var SubMessage = React.createClass({
		render: function(){
			return (
			<small>Sub message</small>
			);
		}
	});

	var reactComponentJSX = React.renderComponent(
		<MessageBox />,
		document.getElementById('jsx')
		);
	</script>
</body>
</html>

This is Example Code of php Inheritance

This is example code of php inheritance parents and child. extend is the keyword to inherit from the parent class.

 

<?php 

abstract class Shape {

	protected $color;

	public function __construct($color = 'blue')
	{
		$this->color = $color;
	}

	public function getColor()
	{
		return $this->color;
	}

	abstract protected function getArea();
	
}

class Square extends Shape {
	
	protected $length = 4;

	public function getArea()
	{
		return pow($this->length, 2);
	}
}

class Triangle extends Shape {

	protected $base = 4;

	protected $height = 7;

	public function getArea()
	{
		return .5 * $this->base * $this->height;
	}
}

class Circle extends Shape {

	protected $radius = 5;

	public function getArea()
	{
		return M_PI * pow($this->radius, 2);
	}
}

// long hand methods
// $circle = new Circle;

// echo $circle->getArea();

//shorthand
echo (new Circle)->getArea();

Building a Calculator With Angular Js

index.html

Angular js Simple Calculator Example Code

<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Example - example-example68-production</title>
  

  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.min.js"></script>
  

  
</head>
<body ng-app="">
  First Num<input type="number" ng-model="number1">
Second Num<input type="number" ng-model="number2">
  <button ng-click="count = count + number1" ng-init="count=0">
  Add
</button>
<button ng-click="count = count - number2" ng-init="count=0">
  Subtract
</button>
<button ng-click="count = count * number2" ng-init="count=0">
  multiply
</button>
<button ng-click="count = count / number2" ng-init="count=0">
  divide
</button>
<button ng-click="count = 0" ng-init="count=0">
  clear
</button>
<span>
  count: {{count}}
</span>
</body>
</html>

app.js

it('should check ng-click', function() {
  expect(element(by.binding('count')).getText()).toMatch('0');
  element(by.css('button')).click();
  expect(element(by.binding('count')).getText()).toMatch('1');
});

Gulp To Create One Javascript File to Speed Up Your Production Server

First install node.js
Type this in commandline

npm install gulp
npm install gulp-concat –save-dev
npm install gulp-uglify –save-dev
npm install gulp-rename –save-dev

'use strick';

var gulp = require('gulp'),
    concat = require('gulp-concat'),
    uglify = require('gulp-uglify'),
    rename = require('gulp-rename');

gulp.task("concatScripts", function(){
	//combine all these scripts
	gulp.src([
		'js/jquery.js', 
		'js/sticky/jquery.stick.js', 
		'js/main.js'])
	//create file
	.pipe(concat("app.js"))
	// put in folder destination
	.pipe(gulp.dest("js"));

});

gulp.task("minify", function(){
	gulp.src("js/app.js")
		.pipe(uglify())
		.pipe(rename("app.min.js"))
		.pipe(gulp.dest("js"));

});

gulp.task("default", ["hello"], function(){
	console.log("this is the default task");
});

Then type in gulp “task or functions”

https://github.com/osscafe/gulp-cheatsheet

Sticky Navigation Bar Souce Code

Demo

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Stick Nav</title>
	<style>
body {
	margin: 0 0 0 0 !important;
	padding: 0; 
}
.header {
	padding: 20px 0;
	background-color: #333;
}
.nav {
	padding: 5px 0;
	background-color: slategrey;
	position: -webkit-sticky;
	top: 0px;
	z-index: 1;
}
ul {
	list-style: none;
}
li {
	display: inline;
}
.header, .nav {
	text-align: center;
	color: #fff;

}
.content {
	width: 600px;
	margin: 10px auto 100px;
}
.sticky {
	position: fixed;
	width: 100%;
	left: 0;
	top: 0;
	z-index: 100;
	border-top: 0;
}
	</style>
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
	<script>
	$(document).ready(function() {
var stickyNavTop = $('.nav').offset().top;
 
var stickyNav = function(){
var scrollTop = $(window).scrollTop();
      
if (scrollTop > stickyNavTop) { 
    $('.nav').addClass('sticky');
} else {
    $('.nav').removeClass('sticky'); 
}
};
 
stickyNav();
 
$(window).scroll(function() {
    stickyNav();
});
});
</script>
</head>
<body>
<div class="wrapper">
	<div class="header">
		Header
	</div>
	<div class="nav">
		<ul>
			<li><a href="#">Home</a></li>
			<li><a href="#">Contact</a></li>
			<li><a href="#">About Me</a></li>
		</ul>
	</div>
	<div class="content">
		<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Temporibus, ad, facilis. Ut vero ab molestias totam, excepturi, at repellendus doloribus, nam vitae non optio obcaecati sequi ipsum, eaque impedit magnam. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Corporis laboriosam error voluptas laudantium repellendus earum blanditiis, tempora delectus dignissimos deleniti, ratione quas doloribus quia. Vel tempore iure est, eligendi sed.Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ut optio rem, accusantium expedita dignissimos eligendi quidem vero facilis consequatur eos similique, culpa laudantium voluptatum. Ex tempore soluta adipisci veritatis animi!Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptate eum beatae, omnis maiores consequatur similique impedit porro aliquam cupiditate, tenetur, at dolore in ex doloribus. Omnis iure aut rem vitae.</p>
		<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Temporibus, ad, facilis. Ut vero ab molestias totam, excepturi, at repellendus doloribus, nam vitae non optio obcaecati sequi ipsum, eaque impedit magnam. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Corporis laboriosam error voluptas laudantium repellendus earum blanditiis, tempora delectus dignissimos deleniti, ratione quas doloribus quia. Vel tempore iure est, eligendi sed.Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ut optio rem, accusantium expedita dignissimos eligendi quidem vero facilis consequatur eos similique, culpa laudantium voluptatum. Ex tempore soluta adipisci veritatis animi!Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptate eum beatae, omnis maiores consequatur similique impedit porro aliquam cupiditate, tenetur, at dolore in ex doloribus. Omnis iure aut rem vitae.</p>
		<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Temporibus, ad, facilis. Ut vero ab molestias totam, excepturi, at repellendus doloribus, nam vitae non optio obcaecati sequi ipsum, eaque impedit magnam. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Corporis laboriosam error voluptas laudantium repellendus earum blanditiis, tempora delectus dignissimos deleniti, ratione quas doloribus quia. Vel tempore iure est, eligendi sed.Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ut optio rem, accusantium expedita dignissimos eligendi quidem vero facilis consequatur eos similique, culpa laudantium voluptatum. Ex tempore soluta adipisci veritatis animi!Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptate eum beatae, omnis maiores consequatur similique impedit porro aliquam cupiditate, tenetur, at dolore in ex doloribus. Omnis iure aut rem vitae.</p>
		<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Temporibus, ad, facilis. Ut vero ab molestias totam, excepturi, at repellendus doloribus, nam vitae non optio obcaecati sequi ipsum, eaque impedit magnam. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Corporis laboriosam error voluptas laudantium repellendus earum blanditiis, tempora delectus dignissimos deleniti, ratione quas doloribus quia. Vel tempore iure est, eligendi sed.Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ut optio rem, accusantium expedita dignissimos eligendi quidem vero facilis consequatur eos similique, culpa laudantium voluptatum. Ex tempore soluta adipisci veritatis animi!Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptate eum beatae, omnis maiores consequatur similique impedit porro aliquam cupiditate, tenetur, at dolore in ex doloribus. Omnis iure aut rem vitae.</p>
		<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Temporibus, ad, facilis. Ut vero ab molestias totam, excepturi, at repellendus doloribus, nam vitae non optio obcaecati sequi ipsum, eaque impedit magnam. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Corporis laboriosam error voluptas laudantium repellendus earum blanditiis, tempora delectus dignissimos deleniti, ratione quas doloribus quia. Vel tempore iure est, eligendi sed.Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ut optio rem, accusantium expedita dignissimos eligendi quidem vero facilis consequatur eos similique, culpa laudantium voluptatum. Ex tempore soluta adipisci veritatis animi!Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptate eum beatae, omnis maiores consequatur similique impedit porro aliquam cupiditate, tenetur, at dolore in ex doloribus. Omnis iure aut rem vitae.</p>
	</div>
</div>

</body>
</html>

How to Setup a squid3 proxy with username and password

This is a tutorial on how to set up a username and password on your squid 3 proxy for ubuntu.

How this proxy useful. It’s to tunnel all the though to your network to usa for example skype or anything in your native country while your in foreign country. Plus you can visit american websites with the ip ranges here.

Squid uses

Cache — The SQUID Proxy Server can Cache web files from all eb Requests and then transefer those chaced files locally if another host requests the same file
ACL Control — Access Control Lists allow you to specify whether certain websites can be accessed.
Bypass Web filters — You can connect to a Proxy Server on a different network to bypass Web Filers on your LAN

Squid Configuration Setup on Server Setup (Note: These steps are shown using Ubuntu 14.04 Server Edition. The basic steps are the same on all Linux distributions, but the actual commands may be slightly different. Please just use Google to find the correct commands if these do not work for you.):
To Install SQUID:

sudo apt-get install squid

To edit SQUID Configuration File:

sudo nano /etc/squid3/squid.conf

To Allow Access to the SQUID Server from Hosts Change:

http_access deny all

http_access allow all

Restart SQUID Service after any configuration changes: sudo service squid3 restart

To Configure Internet Explorer (Note: Proxy Server Configurations are set within Internet Explorer for the Entire System. Even if you use Chrome you still need to change the settings in IE) :
Open Internet Explorer and Go to Tools
Select Internet Options
Select Connections Tab
Click LAN Settings Button
Type in username and password if there is one.
Select Checkbox under Proxy Server Settings and enter the Proxy Servers IP Address, and the port number SQUID is using. (The port number default is 3128)
Click OK

Basic squid conf

sudo nano /etc/squid3/squid.conf instead of the super bloated default config file

auth_param basic program /usr/lib/squid3/basic_ncsa_auth /etc/squid3/passwords
auth_param basic realm proxy
acl authenticated proxy_auth REQUIRED
http_access allow authenticated
http_port 3128 # or whatever you like

Please note the basic_ncsa_auth program instead of the old ncsa_auth

Setting up a user

sudo htdigest -c /etc/squid3/passwords realm_you_like username_you_like

and enter a password twice for the chosen username then

sudo service squid3 restart