Skip to content

Instantly share code, notes, and snippets.

View tanyagupta's full-sized avatar

tanyagupta tanyagupta

  • Washington DC
View GitHub Profile
@tanyagupta
tanyagupta / program.js
Created December 5, 2015 19:50
MY FIRST I/O! (Exercise 3 of 13)
var fs = require('fs');
var file = fs.readFileSync(process.argv[2]);
var arr=file.toString().split('\n');
console.log(arr.length - 1);
@tanyagupta
tanyagupta / program.js
Created December 5, 2015 19:51
HELLO WORLD (Exercise 1 of 13)
console.log("HELLO WORLD");
@tanyagupta
tanyagupta / program.js
Created December 5, 2015 20:03
BABY STEPS (Exercise 2 of 13)
var sum=0;
var arr=process.argv;
for (var i=2; i<arr.length; i++){
sum=sum+Number(arr[i]);
}
console.log(sum);
@tanyagupta
tanyagupta / main_modular.js
Created December 5, 2015 22:15
MAKE IT MODULAR (Exercise 6 of 13)
var mod = require('./module1');
mod(process.argv[2], process.argv[3], function (err, listf) {
if (err) {
console.log('Error!')
} else {
for (var i = 0; i < listf.length; i++) {
console.log(listf[i]);
}
}
@tanyagupta
tanyagupta / http_test.js
Created December 5, 2015 23:04
HTTP CLIENT (Exercise 7 of 13)
var http = require('http')
http.get(process.argv[2], function (response) {
response.setEncoding('utf8')
response.on('data', console.log)
response.on('error', console.error)
})
@tanyagupta
tanyagupta / http_collect.js
Created December 6, 2015 21:48
HTTP COLLECT (Exercise 8 of 13)
var bl = require('bl');
var http = require('http');
var url = process.argv[2];
http.get(url, function(res)
{
res.setEncoding("utf8");
res.pipe(bl(function (err,data)
{
if(err)
@tanyagupta
tanyagupta / client_id_in_ga.js
Last active October 2, 2016 19:04
Tracking users using Google Analytics
/*
The most common way to track users in Google Analytics is by using cookies. But sometimes
there are platform and security limitations that will not allow cookies to be set or
stored for multiple sessions. The following code uses the localStorage property (available
now in most browsers) to provide an alternate way to the cookie-based solution. The code
is heavily commented to help even the most casual users understand the overall workings
of the code. Feel free to ignore this part if you know what you are doing.
This work is based on a Google example:
https://developers.google.com/analytics/devguides/collection/analyticsjs/cookies-user-id
@tanyagupta
tanyagupta / or_example_json_data.js
Last active November 4, 2016 17:17
Accessing heterogenous JSON data (using or) - Part 3
function test_or(){
var cars=[
{make: "Jeep",model: "Cherokee",trim:"Sport 4dr Front-wheel Drive",year:"2016",
wheel:{wheel_size:"17 x 7.5",wheel_metal:"Aluminum"}},
{make: "Jeep",model: "Wrangler",trim:"Sahara 2dr 4x4",year: "2015"}
]
for (var i in basic){
Logger.log(cars[i]["wheel"] || "") // Logger.log() is Google App Script's console.log
@tanyagupta
tanyagupta / smarter_noob_way_json_data_efficient.js
Last active November 4, 2016 17:19
Accessing heterogenous JSON data (smarter way) - Part 2
//Smarter way to access the data
function noob_smart_way(){
var cars=get_data_set("basic");
var format_string = "%s %s %s %s %s";
for (var i in cars){
Logger.log(format_string, cars[i]["model"] || "",cars[i]["trim"] || "",cars[i]["year"] || "",
cars[i]["wheel"] && cars[i]["wheel"]["wheel_size"] || "",
cars[i]["wheel"] && cars[i]["wheel"]["wheel_metal"] || "")
@tanyagupta
tanyagupta / noob_way_json_data_inefficient.js
Last active November 4, 2016 17:20
Accessing heterogenous JSON data (inefficient way) - Part 1
//Note that this is an INEFFICIENT way to solve the problem
function noob_way() {
// get test data from a test data utility
var cars=get_data_set("basic");
for (var i in cars){
if(cars[i]["model"]!==undefined) {
Logger.log(cars[i]["model"]); // Logger.log() is Google App Script's console.log