Skip to content

Instantly share code, notes, and snippets.

@AleksMeshkov
Created October 22, 2012 11:05
Show Gist options
  • Save AleksMeshkov/3931008 to your computer and use it in GitHub Desktop.
Save AleksMeshkov/3931008 to your computer and use it in GitHub Desktop.
Fun with algorythms
/**
* Author: Meshkov Aleksey (tomeshkov@gmail.com)
*/
// Compressed code (with http://jscompress.com/):
function buildGrid(a,b,c){a=a||"1";b=b||"0";c=Math.abs(c)||8;var d,e,f="";for(var g=0;g<c;g++){d=g%2?a:b;f+=d+" ";for(var h=0;h<c-1;h++){e=g%2?b:a;f+=e+" "}console.log(f);f=""}}
// Uncompressed code:
/**
* buildGrid: draws matix with the specified parameters
-> @evenChar
type: String
description: Any even character to draw with;
-> @oddChar
type: String
description: Any odd character to draw with;
-> @num
type: Number
description: Any positive number. Sets rows and cols length
*/
function buildGrid(evenChar, oddChar, num) {
// Define default params;
evenChar = evenChar || '1';
oddChar = oddChar || '0';
// We also want user to pass only positive numbers
num = Math.abs(num) || 8;
var ch1, ch2, row = '';
// loop for building rows
for(var i = 0; i < num; i++) {
ch1 = i % 2 ? evenChar : oddChar;
row += ch1 + ' ';
// loop for building columns
for(var j = 0; j < num - 1; j++) {
// invert if case
ch2 = i % 2 ? oddChar : evenChar;
row += ch2 + ' ';
}
// draw row
console.log(row);
//reset variable
row = '';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment