Skip to content

Instantly share code, notes, and snippets.

@teckliew
teckliew / fizzBuzz.js
Created April 27, 2015 05:50
My first JS FizzBuzz (to 100).
var x = 0, num = 0;
while (x<100){
num++;
if((num%3) === 0 && (num%5) !== 0 ){
console.log("Fizz");
}else if((num%3) !== 0 && (num%5) === 0){
console.log("Buzz");
}else if((num%3) === 0 && (num%5) === 0){
console.log("FizzBuzz");
}else{
@teckliew
teckliew / checkerBoard.js
Created April 27, 2015 07:07
Creating a JS checker board grid
//size is the dimension of the square grid
var size = 8,
white = " ",
black = "#",
rowE = "",
rowO = "",
grid = "";
//even grid row
for ( var x = 0 ; x < size; x++){
if (x % 2 === 0){
@teckliew
teckliew / sumOfAllNum.js
Created June 17, 2015 19:51
The js solution to sum of all numbers from 1 to n
//my solution
function f(n){
return n > 0 && n % 1 === 0 ? ((1+n)*n)/2 : false;
};
//good examples
//1
function f(n){
return (parseInt(n) === n && n > 0) ? n*(n+1)/2 : false;
};
@teckliew
teckliew / XtoYofZ.js
Created June 17, 2015 19:54
Showing X to Y of Z Products. Given the page number, page size, and the total number of product.
//my solution
var paginationText = function(pageNumber, pageSize, totalProducts){
var numOfPages = Math.ceil(totalProducts/pageSize);
var initItem = pageNumber*pageSize-(pageSize-1);
var endItem;
if (pageNumber === numOfPages){
endItem = totalProducts;
}else{
endItem = pageSize*pageNumber;
};
@teckliew
teckliew / descendingOrder.js
Created June 17, 2015 19:58
Make a function that can take any non-negative integer as a argument and return it with it's digits in descending order.
//my example
function descendingOrder(n){
var digits = (""+n).split("").sort(function(a, b){return b-a});
digits = digits.join("");
return Number(digits);
}
//good examples
//1
function descendingOrder(n){
@teckliew
teckliew / toCurrency.js
Created June 17, 2015 20:00
a function that takes an integer in input and outputs a string with currency format. Ex. 123456 -> "123,456"
//my solution using REGEX
function toCurrency(price){
return price.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,");
}
//good example
function toCurrency(price){
return price.toString().replace(/(\d)(?=(\d{3})+$)/g, '$1,');
}
@teckliew
teckliew / ajax.js
Last active August 29, 2015 14:23
AJAX function with without jQuery. JS only.
var ajax = {
load : function() {
var xhr;
if(typeof XMLHttpRequest !== 'undefined') xhr = new XMLHttpRequest();
else {
var versions = ["MSXML2.XmlHttp.5.0",
"MSXML2.XmlHttp.4.0",
"MSXML2.XmlHttp.3.0",
"MSXML2.XmlHttp.2.0",
@teckliew
teckliew / 0_reuse_code.js
Last active August 29, 2015 14:26
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@teckliew
teckliew / shuffle.js
Last active October 6, 2015 22:45
Non-bias random array shuffle. For more about the Fisher–Yates shuffle, see the Wikipedia article and Jeff Atwood’s post, “The Danger of Naïveté” (2007). Sauce: http://bost.ocks.org/mike/shuffle/
function shuffle(array) {
var m = array.length, t, i;
// While there remain elements to shuffle…
while (m) {
// Pick a remaining element…
i = Math.floor(Math.random() * m--);
// And swap it with the current element.
@teckliew
teckliew / if.js
Last active October 8, 2015 20:01
alternatives to if's. if statement refactored.
//if statement
(if ( foo ) {
bar = "beta";
}
// and...
if ( foo ) {
bar();
})
//alternative